-
Notifications
You must be signed in to change notification settings - Fork 0
fix: harden anonymous quotas and metadata origins #550
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c371f3f
fix: address 48-hour review findings
BigSimmo cc76ff4
Merge remote-tracking branch 'origin/main' into codex/fix-48h-review-…
BigSimmo 9cd4554
fix: harden metadata and navigation regressions
BigSimmo 6b280bd
test: align browser storage with scoped sessions
BigSimmo faac19b
fix: trust deployment-owned request identity
BigSimmo b2e60e0
test: match split-pane preview semantics
BigSimmo a035fa7
docs: clarify trusted request helpers
BigSimmo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** Build a useful-page link without retaining evidence pinned to a different page. */ | ||
| export function documentPageHref(documentId: string, page: number) { | ||
| const normalizedPage = Number.isFinite(page) ? Math.max(1, Math.trunc(page)) : 1; | ||
| const params = new URLSearchParams({ page: String(normalizedPage) }); | ||
| return `/documents/${encodeURIComponent(documentId)}?${params.toString()}#pdf-preview-section`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| export type MetadataBaseOptions = { | ||
| configuredSiteUrl?: string; | ||
| trustedDeploymentDomain?: string; | ||
| allowRequestOrigin?: boolean; | ||
| }; | ||
|
|
||
| /** Parse an absolute HTTP(S) URL without allowing invalid configuration to escape. */ | ||
| function httpUrl(value: string | undefined) { | ||
| const candidate = value?.trim(); | ||
| if (!candidate) return undefined; | ||
| try { | ||
| const url = new URL(candidate); | ||
| return url.protocol === "http:" || url.protocol === "https:" ? url : undefined; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** Resolve metadata from validated configuration, trusted deployment state, or an explicit dev fallback. */ | ||
| export function resolveMetadataBase(requestHeaders: Headers, options: MetadataBaseOptions = {}) { | ||
| const configuredUrl = httpUrl(options.configuredSiteUrl); | ||
| if (configuredUrl) return configuredUrl; | ||
|
|
||
| const deploymentDomain = options.trustedDeploymentDomain?.trim(); | ||
| const deploymentUrl = httpUrl( | ||
| deploymentDomain?.includes("://") ? deploymentDomain : deploymentDomain ? `https://${deploymentDomain}` : undefined, | ||
| ); | ||
| if (deploymentUrl) return deploymentUrl; | ||
|
|
||
| if (!options.allowRequestOrigin) return undefined; | ||
| const forwardedHost = requestHeaders.get("x-forwarded-host")?.split(",")[0]?.trim(); | ||
| const host = forwardedHost || requestHeaders.get("host")?.trim(); | ||
| if (!host) return undefined; | ||
| const forwardedProtocol = requestHeaders.get("x-forwarded-proto")?.split(",")[0]?.trim().toLowerCase(); | ||
| const protocol = forwardedProtocol === "http" || forwardedProtocol === "https" ? forwardedProtocol : "https"; | ||
|
BigSimmo marked this conversation as resolved.
|
||
| return httpUrl(`${protocol}://${host}`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { documentPageHref } from "../src/lib/document-viewer-navigation"; | ||
|
|
||
| describe("document viewer useful-page navigation", () => { | ||
| it("creates a destination-page URL without carrying an unrelated citation chunk", () => { | ||
| const href = documentPageHref("document/id", 3); | ||
|
|
||
| expect(href).toBe("/documents/document%2Fid?page=3#pdf-preview-section"); | ||
| expect(href).not.toContain("chunk="); | ||
| }); | ||
|
|
||
| it("normalizes invalid page numbers to the first page", () => { | ||
| expect(documentPageHref("document-id", 0)).toBe("/documents/document-id?page=1#pdf-preview-section"); | ||
| expect(documentPageHref("document-id", Number.NaN)).toBe("/documents/document-id?page=1#pdf-preview-section"); | ||
| expect(documentPageHref("document-id", Number.POSITIVE_INFINITY)).toBe( | ||
| "/documents/document-id?page=1#pdf-preview-section", | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { resolveMetadataBase } from "../src/lib/metadata-base"; | ||
|
|
||
| describe("production metadata origin", () => { | ||
| it.each(["http://clinical.test", "https://clinical.test"])( | ||
| "uses a configured HTTP(S) origin: %s", | ||
| (configuredOrigin) => { | ||
| expect(resolveMetadataBase(new Headers(), { configuredSiteUrl: configuredOrigin })?.href).toBe( | ||
| `${configuredOrigin}/`, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it("uses Railway's trusted deployment domain when configuration is absent", () => { | ||
| expect(resolveMetadataBase(new Headers(), { trustedDeploymentDomain: "clinical-kb.up.railway.app" })?.href).toBe( | ||
| "https://clinical-kb.up.railway.app/", | ||
| ); | ||
| }); | ||
|
|
||
| it("uses request headers only when the caller explicitly allows a development fallback", () => { | ||
| const requestHeaders = new Headers({ | ||
| host: "internal.test:3000", | ||
| "x-forwarded-host": "clinical.example.org, proxy.internal", | ||
| "x-forwarded-proto": "https, http", | ||
| }); | ||
|
|
||
| expect(resolveMetadataBase(requestHeaders, { allowRequestOrigin: true })?.href).toBe( | ||
| "https://clinical.example.org/", | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to Railway's trusted domain when the configured value is malformed", () => { | ||
| const options = { | ||
| configuredSiteUrl: "not a valid URL", | ||
| trustedDeploymentDomain: "clinical-kb.up.railway.app", | ||
| }; | ||
|
|
||
| expect(() => resolveMetadataBase(new Headers(), options)).not.toThrow(); | ||
| expect(resolveMetadataBase(new Headers(), options)?.href).toBe("https://clinical-kb.up.railway.app/"); | ||
| }); | ||
|
|
||
| it("does not trust request-controlled host headers in production", () => { | ||
| const requestHeaders = new Headers({ | ||
| host: "internal.test:3000", | ||
| "x-forwarded-host": "attacker.example", | ||
| "x-forwarded-proto": "https", | ||
| }); | ||
|
|
||
| expect(resolveMetadataBase(requestHeaders)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.