Repository navigation
fix(router): enforce the redirects size cap before and while reading #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f5bbd90
7ceb6b1
650d4be
d552494
bba503e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,16 @@ | ||
| #!/usr/bin/env node | ||
| // Upload a laid-out version to the private archive under its immutable prefix. | ||
| import { createHash } from "node:crypto"; | ||
| import { readFile, readdir } from "node:fs/promises"; | ||
| import { readFile, readdir, stat } from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { DEV_KEY, lineKey, tagKey } from "@coder/docs-manifest"; | ||
| import { | ||
| DEV_KEY, | ||
| MAX_VERSION_REDIRECTS_BYTES, | ||
| VERSION_REDIRECTS_KEY, | ||
| lineKey, | ||
| readBlobTextWithLimit, | ||
| tagKey, | ||
| } from "@coder/docs-manifest"; | ||
| import { copy, get, put } from "@vercel/blob"; | ||
| import { contentTypeFor } from "../archive-layout-helpers.mjs"; | ||
| import { logBlobRetry, withBlobRetry } from "./blob-retry.mjs"; | ||
|
|
@@ -110,6 +117,9 @@ export function liveArchivePrefix({ items, plan }) { | |
| // `_publish.json` hashes are the untrusted build's claims. | ||
| const OBJECTS_KEY = "_objects.json"; | ||
|
|
||
| // Far above a real record (one short entry per file); refuses a runaway one. | ||
| const MAX_RECORDED_OBJECTS_BYTES = 16 * 1024 * 1024; | ||
|
|
||
| /** Written first by every upload, so cleanup can tell a prefix being re-uploaded from an idle orphan. */ | ||
| export const UPLOADING_KEY = "_uploading.json"; | ||
|
|
||
|
|
@@ -120,9 +130,12 @@ async function readRecordedObjects({ prefix, token, blobGet, onReuseSkipped }) { | |
| { key: `${prefix}/${OBJECTS_KEY}` }, | ||
| ); | ||
| if (!result?.stream) throw new Error("not found"); | ||
| const recorded = JSON.parse( | ||
| Buffer.from(await new Response(result.stream).arrayBuffer()).toString("utf8"), | ||
| const text = await readBlobTextWithLimit( | ||
| { stream: result.stream, size: result.blob?.size }, | ||
| MAX_RECORDED_OBJECTS_BYTES, | ||
| ); | ||
| if (text === null) throw new Error(`larger than ${MAX_RECORDED_OBJECTS_BYTES} bytes`); | ||
| const recorded = JSON.parse(text); | ||
| if (recorded?.version !== 1 || typeof recorded.files !== "object" || recorded.files === null) { | ||
| throw new Error("unrecognised format"); | ||
| } | ||
|
|
@@ -183,6 +196,14 @@ export async function uploadDir({ | |
| .toSorted(); | ||
| if (files.length === 0) | ||
| throw new Error(`upload: ${dir} has no files to upload (besides _publish.json)`); | ||
| if (files.includes(VERSION_REDIRECTS_KEY)) { | ||
| const { size } = await stat(path.join(dir, VERSION_REDIRECTS_KEY)); | ||
| if (size > MAX_VERSION_REDIRECTS_BYTES) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 650d4be: |
||
| throw new Error( | ||
| `upload: ${VERSION_REDIRECTS_KEY} is larger than ${MAX_VERSION_REDIRECTS_BYTES} bytes (${size})`, | ||
| ); | ||
| } | ||
| } | ||
| const reusable = | ||
| reuseFrom === undefined || reuseFrom === prefix | ||
| ? new Map() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import { analyticsEnabled, docsContentSecurityPolicy } from "@coder/docs-config"; | ||
| import { | ||
| MAX_VERSION_REDIRECTS_BYTES, | ||
| type ObjectPath, | ||
| type RouteTarget, | ||
| VERSION_REDIRECTS_KEY, | ||
| hasUnsafePathSegment, | ||
| objectPath, | ||
| readBlobTextWithLimit, | ||
| } from "@coder/docs-manifest"; | ||
| import { get as blobGet } from "@vercel/blob"; | ||
|
|
||
|
|
@@ -233,21 +235,34 @@ export async function serveNotFoundPage( | |
| return new Response(result.stream, { status: 404, headers }); | ||
| } | ||
|
|
||
| /** What the store holds at a version's `_redirects.json`. */ | ||
| export type VersionRedirectsRead = | ||
| | { kind: "absent" } | ||
| | { kind: "too-large"; reportedSize: number } | ||
| | { kind: "text"; text: string }; | ||
|
|
||
| /** | ||
| * The version's own redirects object, `_redirects.json` beside `_publish.json` | ||
| * under the deployment's archive prefix, as text; null when the prefix has none | ||
| * (published before the object existed). A store failure throws. | ||
| * under the deployment's archive prefix. An object over the size cap is | ||
| * reported as `too-large` without its body being read: the store's reported | ||
| * size is checked first, and the read itself stops at the cap in case the | ||
| * size is missing or wrong. A store failure throws. | ||
| */ | ||
| export async function readVersionRedirectsText( | ||
| deployment: string, | ||
| request: Request, | ||
| deps: ArchiveDeps, | ||
| ): Promise<string | null> { | ||
| ): Promise<VersionRedirectsRead> { | ||
| const prefix = prefixFor({ deployment, path: "/", versionId: "" }); | ||
| const key = prefix ? `${prefix}/${VERSION_REDIRECTS_KEY}` : VERSION_REDIRECTS_KEY; | ||
| const result = await readObject(key, { access: "private" }, request, deps); | ||
| if (!result?.stream) return null; | ||
| return new Response(result.stream).text(); | ||
| if (!result?.stream) return { kind: "absent" }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: this sequence (check the reported size, cancel, bounded read, treat
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 7ceb6b1: |
||
| const { stream, blob } = result; | ||
| const text = await readBlobTextWithLimit( | ||
| { stream, size: blob.size }, | ||
| MAX_VERSION_REDIRECTS_BYTES, | ||
| ); | ||
| return text === null ? { kind: "too-large", reportedSize: blob.size } : { kind: "text", text }; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fix, docs outside the diff:
apps/docs/README.md(around lines 247 to 260) doesn't mention that a publish now fails when_redirects.jsonis over 1 MiB, or that an_objects.jsonover 16 MiB now means a full upload instead of a reuse. One sentence for each would do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in d552494: the "Version redirects" section now says a normalized
_redirects.jsonover 1 MiB fails the version at sync and layout and again at upload, and theDOCS_UPLOAD_REUSEparagraph lists a record over 16 MiB (not read) among the cases that fall back to a plain upload.