-
Notifications
You must be signed in to change notification settings - Fork 0
fix: harden the inline bootstrap and API error logging #12
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** | ||
| * JSON.stringify for values interpolated into an inline <script>. "<" and the | ||
| * two Unicode line separators are the characters that can end the tag or the | ||
| * statement early, so they go out as escapes; the result is still valid JSON | ||
| * and valid JavaScript. | ||
| */ | ||
| export function jsLiteral(value: unknown): string { | ||
| return JSON.stringify(value) | ||
| .replace(/</g, "\\u003c") | ||
| .replace(/\u2028/g, "\\u2028") | ||
| .replace(/\u2029/g, "\\u2029"); | ||
| } |
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,57 @@ | ||
| /** | ||
| * Values interpolated into the pre-paint <script> in layout.tsx. The parser | ||
| * closes a script element at the first "</script" it sees, before JavaScript | ||
| * ever runs, so the serialiser has to make that sequence impossible. | ||
| */ | ||
|
|
||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { jsLiteral } from "@/lib/inline-script"; | ||
| import { logLine } from "@/app/api/_lib/http"; | ||
|
|
||
| function evaluate(source: string): unknown { | ||
| return new Function(`return ${source};`)(); | ||
| } | ||
|
|
||
| test("a closing script tag inside a string cannot end the element", () => { | ||
| const out = jsLiteral("</script><script>alert(1)</script>"); | ||
| assert.ok(!out.includes("</script"), out); | ||
| assert.ok(!out.includes("<"), out); | ||
| assert.equal(evaluate(out), "</script><script>alert(1)</script>"); | ||
| }); | ||
|
|
||
| test("the Unicode line separators are escaped, not emitted raw", () => { | ||
| const raw = "a\u2028b\u2029c"; | ||
| const out = jsLiteral(raw); | ||
| assert.ok(!out.includes("\u2028") && !out.includes("\u2029"), out); | ||
| assert.equal(evaluate(out), raw); | ||
| }); | ||
|
|
||
| test("the values the layout actually serialises round-trip unchanged", () => { | ||
| // The alias map is null-prototype in the source; the client gets a plain | ||
| // object, which is fine because the bootstrap reads it with hasOwnProperty. | ||
| const aliases = Object.assign(Object.create(null) as Record<string, string>, { fil: "tl", in: "id" }); | ||
| const samples: Array<[unknown, unknown]> = [ | ||
| ["probus.settings.v1", "probus.settings.v1"], | ||
| [["it", "en", "ar"], ["it", "en", "ar"]], | ||
| [aliases, { fil: "tl", in: "id" }], | ||
| [null, null], | ||
| [42, 42], | ||
| ]; | ||
| for (const [value, expected] of samples) { | ||
| assert.deepEqual(evaluate(jsLiteral(value)), expected); | ||
| } | ||
| }); | ||
|
|
||
| test("a log line cannot carry a newline from a request", () => { | ||
| const forged = "stop 123\n[api:auth] admin login ok\r\n"; | ||
| const out = logLine(new Error(forged)); | ||
| assert.ok(!/[\r\n]/.test(out), out); | ||
| assert.ok(out.startsWith("Error: stop 123"), out); | ||
| }); | ||
|
|
||
| test("a log line is capped so a huge message cannot flood the log", () => { | ||
| assert.ok(logLine("x".repeat(10_000)).length <= 2000); | ||
| assert.equal(logLine(undefined), "undefined"); | ||
| }); |
Oops, something went wrong.
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.