-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: extract allowlist to lib/ with ALLOWLIST_FILE override #18
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| const fs = require('node:fs'); | ||
|
|
||
| const DEFAULT_ALLOWLIST = Object.freeze({ | ||
| allowedTags: Object.freeze([ | ||
| 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'blockquote', 'ul', 'ol', 'li', 'br', 'hr', | ||
| 'strong', 'em', 'u', 's', 'b', 'i', 'mark', 'sub', 'sup', | ||
| 'pre', 'code', 'kbd', 'samp', | ||
| 'table', 'thead', 'tbody', 'tr', 'td', 'th', | ||
| 'a', 'img', | ||
| 'dl', 'dt', 'dd', | ||
| ]), | ||
| allowedAttributes: Object.freeze({ | ||
| a: Object.freeze(['href', 'title', 'target']), | ||
| img: Object.freeze(['src', 'alt', 'width', 'height']), | ||
| code: Object.freeze(['class']), | ||
| }), | ||
| allowedSchemes: Object.freeze(['http', 'https', 'mailto']), | ||
| disallowedTagsMode: 'discard', | ||
| }); | ||
|
|
||
| function loadAllowlist({ path = process.env.ALLOWLIST_FILE } = {}) { | ||
| if (!path) return DEFAULT_ALLOWLIST; | ||
|
|
||
| let raw; | ||
| try { | ||
| raw = fs.readFileSync(path, 'utf8'); | ||
| } catch (err) { | ||
| throw new Error(`ALLOWLIST_FILE: cannot read "${path}": ${err.message}`); | ||
| } | ||
|
|
||
| let parsed; | ||
| try { | ||
| parsed = JSON.parse(raw); | ||
| } catch (err) { | ||
| throw new Error(`ALLOWLIST_FILE: invalid JSON in "${path}": ${err.message}`); | ||
| } | ||
|
|
||
| if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { | ||
| throw new Error('ALLOWLIST_FILE: top-level must be a JSON object'); | ||
| } | ||
| if (!Array.isArray(parsed.allowedTags)) { | ||
| throw new Error('ALLOWLIST_FILE: "allowedTags" must be an array'); | ||
| } | ||
| if ( | ||
| parsed.allowedAttributes !== undefined && | ||
| (typeof parsed.allowedAttributes !== 'object' || | ||
| Array.isArray(parsed.allowedAttributes) || | ||
| parsed.allowedAttributes === null) | ||
| ) { | ||
| throw new Error('ALLOWLIST_FILE: "allowedAttributes" must be an object'); | ||
| } | ||
| if (parsed.allowedSchemes !== undefined && !Array.isArray(parsed.allowedSchemes)) { | ||
| throw new Error('ALLOWLIST_FILE: "allowedSchemes" must be an array'); | ||
| } | ||
|
|
||
| return parsed; | ||
| } | ||
|
|
||
| module.exports = { DEFAULT_ALLOWLIST, loadAllowlist }; |
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,119 @@ | ||
| const fs = require("node:fs"); | ||
| const os = require("node:os"); | ||
| const path = require("node:path"); | ||
| const request = require("supertest"); | ||
| const { loadAllowlist, DEFAULT_ALLOWLIST } = require("../lib/allowlist"); | ||
|
|
||
| const writeFixture = (name, content) => { | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), "allowlist-test-")); | ||
| const file = path.join(dir, `${name}.json`); | ||
| fs.writeFileSync(file, typeof content === "string" ? content : JSON.stringify(content)); | ||
| return file; | ||
| }; | ||
|
|
||
| const removeFixture = (file) => { | ||
| fs.rmSync(path.dirname(file), { recursive: true, force: true }); | ||
| }; | ||
|
|
||
| describe("loadAllowlist", () => { | ||
| it("returns the default allowlist when no path is provided", () => { | ||
| expect(loadAllowlist({ path: undefined })).toBe(DEFAULT_ALLOWLIST); | ||
| expect(DEFAULT_ALLOWLIST.allowedTags).toContain("p"); | ||
| expect(DEFAULT_ALLOWLIST.allowedTags).not.toContain("script"); | ||
| }); | ||
|
|
||
| it("reads a JSON file when a path is provided", () => { | ||
| const file = writeFixture("allowlist", { | ||
| allowedTags: ["p", "em"], | ||
| allowedAttributes: {}, | ||
| allowedSchemes: ["http"], | ||
| disallowedTagsMode: "escape", | ||
| }); | ||
|
|
||
| try { | ||
| const loaded = loadAllowlist({ path: file }); | ||
| expect(loaded.allowedTags).toEqual(["p", "em"]); | ||
| expect(loaded.disallowedTagsMode).toBe("escape"); | ||
| } finally { | ||
| removeFixture(file); | ||
| } | ||
| }); | ||
|
|
||
| it("throws when the file cannot be read", () => { | ||
| expect(() => | ||
| loadAllowlist({ path: "/definitely/does/not/exist.json" }) | ||
| ).toThrow(/cannot read/); | ||
| }); | ||
|
|
||
| it("throws on malformed JSON", () => { | ||
| const file = writeFixture("bad", "not json"); | ||
| try { | ||
| expect(() => loadAllowlist({ path: file })).toThrow(/invalid JSON/); | ||
| } finally { | ||
| removeFixture(file); | ||
| } | ||
| }); | ||
|
|
||
| it("throws when allowedTags is not an array", () => { | ||
| const file = writeFixture("wrong-tags", { allowedTags: "not an array" }); | ||
| try { | ||
| expect(() => loadAllowlist({ path: file })).toThrow( | ||
| /allowedTags.*array/ | ||
| ); | ||
| } finally { | ||
| removeFixture(file); | ||
| } | ||
| }); | ||
|
|
||
| it("throws when the top-level is not an object", () => { | ||
| const file = writeFixture("array-top", ["p", "em"]); | ||
| try { | ||
| expect(() => loadAllowlist({ path: file })).toThrow( | ||
| /top-level must be a JSON object/ | ||
| ); | ||
| } finally { | ||
| removeFixture(file); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("ALLOWLIST_FILE integration", () => { | ||
| let originalEnv; | ||
|
|
||
| beforeEach(() => { | ||
| originalEnv = process.env.ALLOWLIST_FILE; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalEnv === undefined) delete process.env.ALLOWLIST_FILE; | ||
| else process.env.ALLOWLIST_FILE = originalEnv; | ||
| jest.resetModules(); | ||
| }); | ||
|
|
||
| it("a custom allowlist relaxes sanitization when set via env", async () => { | ||
| const file = writeFixture("relaxed", { | ||
| allowedTags: ["iframe"], | ||
| allowedAttributes: { iframe: ["src"] }, | ||
| allowedSchemes: ["https"], | ||
| disallowedTagsMode: "discard", | ||
| }); | ||
| process.env.ALLOWLIST_FILE = file; | ||
|
|
||
| let app; | ||
| jest.isolateModules(() => { | ||
| app = require("../server"); | ||
| }); | ||
|
|
||
| try { | ||
| const res = await request(app) | ||
| .post("/validate") | ||
| .send({ markdown: '<iframe src="https://example.com"></iframe>' }) | ||
| .set("Content-Type", "application/json"); | ||
|
|
||
| expect(res.body.safe).toBe(true); | ||
| expect(res.body.sanitized).toContain("<iframe"); | ||
| } finally { | ||
| removeFixture(file); | ||
| } | ||
| }); | ||
| }); | ||
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.