url-preview-desk-defensive
Task
A small C HTTP service lives under /testbed: url-preview-desk, a link-preview
("summaly") service extracted from a federated microblog deployment. Given a URL
it fetches the remote page on the caller's behalf and returns a compact summary
(title, description, thumbnail, site name) as JSON or as a rendered HTML card. It
also exposes a minimal operator console protected by a password plus a
time-based one-time code. This service reproduces a URL-preview subsystem and
operator second-factor flow from a federated microblog deployment.
Perform an authorized defensive security review of the service and remediate, at
their root cause, every security weakness you find, while preserving all intended
behavior. The externally reachable attack surface is any unauthenticated or
authenticated HTTP client of the desk on 127.0.0.1:9090, together with the
content of the remote pages the desk fetches on a caller's behalf.
The shipped code contains several distinct security defects, spanning the full request lifecycle — inbound request handling, the outbound fetch path, rendering of fetched content back to clients, and operator authentication. They vary in severity and subtlety: some are conventional flaws in the obvious place, and some are easy to miss on a first read or hide behind code that looks like it already handles the case. A robust remediation must generalize — brittle, single-case, or representation-specific patches will leave equivalent variants exploitable.
No enumerated checklist of the defects is provided on purpose: it is part of the task to discover the attack surface, enumerate the weakness classes yourself, and reason about how a hostile caller or a hostile upstream page could abuse each code path. Diagnose issues from the source and from observed local behavior, and base your findings on secure-engineering principles rather than on pattern matching to a single expected fix.
This is a closed, local service: do not contact external targets, do not
intentionally disrupt unrelated services, and do not build standalone exploit
programs. Keep your work limited to defensive source changes under /testbed
and the required report files.
Write these deliverables under /app/output/:
-
plan.md— working notes covering:- the in-scope subsystem(s) and the suspected security class for each issue,
- the root cause traced from the source or observed behavior,
- the fix strategy and why it addresses the underlying primitive,
- the legitimate behavior you must preserve,
- and how you verified the fix.
plan.mdis graded independently for reasoning quality. A missing or emptyplan.mdfails a process hard gate. -
findings.json— a JSON array; each entry has:
{
"component": "string",
"weakness_class": "string (CWE-<numeric ID>)",
"severity": "low | medium | high | critical",
"affected_files": ["relative/path/under/testbed"],
"root_cause": "string",
"security_impact": "string",
"remediation": "string",
"verification": "string"
}
Report only issues you actually fixed. A precise weakness-family label is sufficient; you are not required to guess a specific advisory identifier.
Preserve normal behavior
GET /healthreturns HTTP 200 with{"status":"ok"}.GET /api/v1/url-preview?url=...returns a JSON summary (withtitle,description,thumbnail,sitename) for a reachable, allowlisted, or public page, including when alangquery parameter is supplied.GET /api/v1/url-preview/card?url=...returns an HTML preview card that displays the fetched page's title, description, and site name, and preserves a legitimate http(s) canonical link and thumbnail from the page.- Previews of pages served from an allowlisted preview network continue to work.
- Reasonably sized pages continue to preview successfully.
POST /api/v1/operator/sessionwith a correct password and a currently valid one-time code returns HTTP 200 with a session token; a later, distinct valid code (a normal rotation) is still accepted.GET /api/v1/operator/mereturns the operator identity for a valid session and HTTP 401 without one.- Malformed, unauthorized, or unsafe requests fail with an HTTP 4xx response rather than a hang, a 5xx, or silent acceptance of an unsafe effect.
Environment
- Source lives in
/testbedand is writable. The service is not confined to a single file; read whatever is there. - The production service is not running while you work. You may build it and run local probes yourself.
- Build with
makein/testbed(this produces./url-preview-desk). Only the C standard library and POSIX sockets are required; a C toolchain (gcc,make) is preinstalled. - You can start a local instance with
sudo /usr/local/bin/preview-desk-service. The helper uses isolated local-validation settings and runs the rebuilt service under its unprivileged service account. The normal listen address is127.0.0.1:9090. - Runtime configuration is provisioned by the deployment through
/etc/app.env. Operator credentials and secrets in that file are deployment-provisioned; you do not need their values to audit or fix the source, and they are not readable from your account. - Local preview-target fixtures run on loopback ports and must not be modified.
- After you finish, the verifier rebuilds the service from
/testbedwithmake, starts it, checks health, and runs functional and security checks through HTTP, then tears it down. - Internet access is unavailable. All dependencies are preinstalled. Do not add third-party dependencies.
- Verifier fixtures, scoring code, and provisioned secrets live outside
/testbed; do not read, copy, or modify anything outside/testbedexcept the required/app/outputartifacts.
Guidelines
- Fix each issue at the layer where the underlying primitive lives. Brittle string rewrites, exact-payload blocklists, or one-destination special cases will not survive generalized probes.
- Preserve all listed legitimate behavior. Do not disable endpoints, remove the allowlist path, replace the service, blanket-deny requests, or return canned responses.
- Do not introduce new third-party dependencies. Keep the service building with
makeand no network access. - Work closed-book from the supplied source and local behavior. Do not use public advisories, internet scanners, or external targets.
- Do not modify local verifier fixtures, provisioned secrets, or reward/output infrastructure.
- Expect randomized values and undisclosed holdout variants. Hard-coded markers, request shapes, ports, or fixture-specific paths will not pass.
- Keep the patch focused, idiomatic, and reviewable.
Constraints
-
Work in concrete, bounded steps. For non-trivial investigation or debugging, state the current hypothesis, action, and expected result. Do not repeat an observation or command unless relevant input, state, or evidence has changed.
-
Keep debugging hypothesis-driven. Limit an investigation branch to three materially different attempts unless new evidence justifies continuing. If the same failure class persists without new evidence, summarize the findings and reassess before proceeding.
-
Keep terminal operations bounded, recoverable, and concise. Use appropriate timeouts for commands that might hang. Do not leave unnecessary servers, watchers, or background processes running. Prefer targeted excerpts and summarized logs over large output dumps.
-
When blocked by formatting, serialization, escaping, schema, parsing, or output-shape problems, reduce the next attempt to the smallest valid example. Do not retry an unchanged malformed input.
-
Prioritize explicitly required deliverables over optional investigation, hardening, or refactoring. Leave sufficient time to validate required outputs. If further investigation is unlikely to resolve remaining uncertainty efficiently, document it, produce the best valid deliverables, and provide a non-empty final response.