diff --git a/package-lock.json b/package-lock.json index 451dc08..e006d47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -631,6 +631,7 @@ "integrity": "sha512-rDS5/31E9HfPl/CIzGrn0DOlvBbXFseQ5URJ9sYMfstbKLD/c6Gm9vmRzRGDdAXyOIL4zmO37lc9RIwYqVruZw==", "dev": true, "license": "MIT", + "peer": true, "funding": { "url": "https://github.com/sponsors/oxc-project" } @@ -661,6 +662,7 @@ "os": [ "android" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -678,6 +680,7 @@ "os": [ "android" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -695,6 +698,7 @@ "os": [ "darwin" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -712,6 +716,7 @@ "os": [ "darwin" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -729,6 +734,7 @@ "os": [ "freebsd" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -746,6 +752,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -763,6 +770,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -780,6 +788,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -797,6 +806,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -814,6 +824,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -831,6 +842,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -848,6 +860,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -865,6 +878,7 @@ "os": [ "openharmony" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -882,6 +896,7 @@ "os": [ "win32" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -899,6 +914,7 @@ "os": [ "win32" ], + "peer": true, "engines": { "node": "^20.19.0 || >=22.12.0" } @@ -908,7 +924,8 @@ "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.1.tgz", "integrity": "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==", "dev": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/@types/chai": { "version": "5.2.3", @@ -955,7 +972,6 @@ "integrity": "sha512-U2+DNr+wSjpsTS/wZGYHq7GcwfuSmKiKvoPvK22zwTlRhU91yOniN4qRR5KhIjvif7ysw/dz/hKmfDH0Ris4aA==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "undici-types": "~6.21.0" } @@ -1005,7 +1021,6 @@ "integrity": "sha512-zYvrmj9Yxd63UGaXw+kdt6A0F0s0qveJyuatIM77bYC2DE4pgmg7a50u8LR7PRtXd0x+h+Tl3eXabGm06SWd3Q==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@typescript-eslint/scope-manager": "8.70.0", "@typescript-eslint/types": "8.70.0", @@ -1235,7 +1250,6 @@ "integrity": "sha512-lGq+9yr1/GuAWaVYIHRjvvySG5/4VfKIvC8EWxStPdcDh/Ka7FG3twP6v4d5BkravUilhIAsG4Qj83t02LWUPQ==", "dev": true, "license": "MIT", - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -1392,6 +1406,7 @@ "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", "dev": true, "license": "Apache-2.0", + "peer": true, "engines": { "node": ">=8" } @@ -1422,7 +1437,6 @@ "integrity": "sha512-P7a6UEEqb9G95MYAtqkmsTbVXIYyzIfl6NGOIJk162PaahFxFyeGcrlXYFSiagECg4sEm8IseJdZBKR3rx6MsQ==", "dev": true, "license": "MIT", - "peer": true, "workspaces": [ "packages/*" ], @@ -1738,6 +1752,7 @@ "os": [ "darwin" ], + "peer": true, "engines": { "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } @@ -1880,7 +1895,6 @@ "integrity": "sha512-CYDD3SOtsHtyXeEORYRx2qBtpDJFjRTGXUtmNEMGyzYOKj1TE3tycdlho7kA1Ufx9OYWZzg52QFBGALTirzDSw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@keyv/serialize": "^1.1.1" } @@ -1916,6 +1930,7 @@ "integrity": "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==", "dev": true, "license": "MPL-2.0", + "peer": true, "dependencies": { "detect-libc": "^2.0.3" }, @@ -1953,6 +1968,7 @@ "os": [ "android" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -1974,6 +1990,7 @@ "os": [ "darwin" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -1995,6 +2012,7 @@ "os": [ "darwin" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2016,6 +2034,7 @@ "os": [ "freebsd" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2037,6 +2056,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2058,6 +2078,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2079,6 +2100,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2100,6 +2122,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2121,6 +2144,7 @@ "os": [ "linux" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2142,6 +2166,7 @@ "os": [ "win32" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2163,6 +2188,7 @@ "os": [ "win32" ], + "peer": true, "engines": { "node": ">= 12.0.0" }, @@ -2232,6 +2258,7 @@ } ], "license": "MIT", + "peer": true, "bin": { "nanoid": "bin/nanoid.cjs" }, @@ -2377,6 +2404,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "nanoid": "^3.3.18", "picocolors": "^1.1.1", @@ -2465,6 +2493,7 @@ "integrity": "sha512-hx/Pv0N1haXRb11qkfnK5MXB/iqr7i0yjWQqmO9uHqZpBgQSqzc8UsSnEpalsh+j1I8qQ2CkXAkJC8Br3dKSlg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@oxc-project/types": "=0.150.0", "@rolldown/pluginutils": "^1.0.0" @@ -2562,6 +2591,7 @@ "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", "dev": true, "license": "BSD-3-Clause", + "peer": true, "engines": { "node": ">=0.10.0" } @@ -2649,7 +2679,6 @@ "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", "dev": true, "license": "Apache-2.0", - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/tests/generator-text-binary-classification.test.ts b/tests/generator-text-binary-classification.test.ts index 06e3624..adc4868 100644 --- a/tests/generator-text-binary-classification.test.ts +++ b/tests/generator-text-binary-classification.test.ts @@ -16,7 +16,7 @@ * Depends on SW-014 (template registry metadata completeness tests). */ -import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { describe, expect, it } from 'vitest'; import { generateProjectFromTemplate } from '../src/generator/template-generation.js'; @@ -618,3 +618,241 @@ describe('classification: mixed text and binary files in one template', () => { ); })); }); + +// --------------------------------------------------------------------------- +// 5. Path injection protection — only approved substitutions occur +// --------------------------------------------------------------------------- + +describe('security: path injection protection', () => { + it('never substitutes placeholders in directory paths during template traversal', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(join(templateRoot, '{{projectName}}-dir'), { + recursive: true, + }); + writeFileSync( + join(templateRoot, '{{projectName}}-dir', 'file.txt'), + textBuffer('content\n'), + ); + + generateProjectFromTemplate(context(root), { templateRoot }); + + // Directory name must NOT be substituted — it stays literal + expect( + existsSync(join(root, 'my-project', '{{projectName}}-dir', 'file.txt')), + ).toBe(true); + expect( + existsSync(join(root, 'my-project', 'my-project-dir', 'file.txt')), + ).toBe(false); + })); +}); + +// --------------------------------------------------------------------------- +// 6. Content injection protection — substituted values cannot introduce tokens +// --------------------------------------------------------------------------- + +describe('security: content injection protection', () => { + it('replaces projectName literally without re-interpreting matched patterns', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + writeFileSync( + join(templateRoot, 'config.txt'), + textBuffer('name: {{projectName}}\ntemplate: {{templateId}}\n'), + ); + + generateProjectFromTemplate(context(root), { + templateRoot, + }); + + // The projectName is substituted literally; no second-pass interpretation occurs + expect(readFileSync(join(root, 'my-project', 'config.txt'), 'utf8')).toBe( + 'name: my-project\ntemplate: basic-app\n', + ); + })); + + it('replaces templateId literally without re-interpreting token-like strings', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + writeFileSync( + join(templateRoot, 'meta.txt'), + textBuffer('template: {{templateId}}\n'), + ); + + generateProjectFromTemplate( + { + ...context(root), + template: { templateId: 'basic-app' }, + }, + { templateRoot }, + ); + + expect(readFileSync(join(root, 'my-project', 'meta.txt'), 'utf8')).toBe( + 'template: basic-app\n', + ); + })); + + it('does not expand multiple substitution passes on the same content', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + writeFileSync( + join(templateRoot, 'nested-tokens.txt'), + textBuffer('value={{projectName}}\n'), + ); + + generateProjectFromTemplate(context(root, 'app'), { + templateRoot, + }); + + // Single-pass: {{projectName}} is replaced with 'app' + // Not double-passed to anything else + expect(readFileSync(join(root, 'app', 'nested-tokens.txt'), 'utf8')).toBe( + 'value=app\n', + ); + })); + + it('preserves literal token-like strings in substituted output without further interpretation', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + writeFileSync( + join(templateRoot, 'example.txt'), + textBuffer('project={{projectName}}\ntemplate={{templateId}}\n'), + ); + + generateProjectFromTemplate(context(root), { templateRoot }); + + const output = readFileSync( + join(root, 'my-project', 'example.txt'), + 'utf8', + ); + // If a second substitution pass occurred, any tokens in the output would + // be replaced. This verifies that does not happen. + expect(output).toBe('project=my-project\ntemplate=basic-app\n'); + expect(output).not.toContain('{{'); + })); + + it('rejects attempts to use unknown tokens even if they look like valid identifiers', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + writeFileSync( + join(templateRoot, 'bad.txt'), + textBuffer('secret={{secretKey}}\n'), + ); + + expect(() => + generateProjectFromTemplate(context(root), { templateRoot }), + ).toThrow( + "Template contains unsupported substitution token '{{secretKey}}'.", + ); + + // No partial output is left + expect(existsSync(join(root, 'my-project'))).toBe(false); + })); +}); + +// --------------------------------------------------------------------------- +// 7. Unknown placeholder invariants — only projectName and templateId are permitted +// --------------------------------------------------------------------------- + +describe('security: unknown placeholder invariants', () => { + it('never substitutes a token that is not in the hardcoded allowlist', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + writeFileSync( + join(templateRoot, 'vars.txt'), + textBuffer( + 'name={{projectName}}\nid={{templateId}}\nuser={{userName}}\n', + ), + ); + + expect(() => + generateProjectFromTemplate(context(root), { templateRoot }), + ).toThrow( + "Template contains unsupported substitution token '{{userName}}'.", + ); + + expect(existsSync(join(root, 'my-project'))).toBe(false); + })); + + it('rejects {{version}}, {{timestamp}}, {{random}} and other dynamic tokens', () => + withTempDirectory(async (root1) => { + await withTempDirectory((root2) => { + const templateRoot = join(root1, 'template'); + mkdirSync(templateRoot); + + // A template using dynamic tokens that are not in the hardcoded allowlist + writeFileSync( + join(templateRoot, 'manifest.txt'), + textBuffer('generated={{timestamp}}\n'), + ); + + expect(() => + generateProjectFromTemplate(context(root1), { templateRoot }), + ).toThrow("unsupported substitution token '{{timestamp}}'"); + + writeFileSync( + join(templateRoot, 'manifest.txt'), + textBuffer('version={{version}}\n'), + ); + + expect(() => + generateProjectFromTemplate(context(root2), { templateRoot }), + ).toThrow("unsupported substitution token '{{version}}'"); + }); + })); +}); + +// --------------------------------------------------------------------------- +// 8. Binary files are protected from injection via substitution +// --------------------------------------------------------------------------- + +describe('security: binary content protection', () => { + it('never applies text-substitution rules to binary files', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + + // Binary file with injected-looking content + const binary = Buffer.concat([ + Buffer.from([0x00]), + Buffer.from('malicious: {{projectName}}={{userName}}\n', 'utf8'), + Buffer.from([0xff]), + ]); + writeFileSync(join(templateRoot, 'payload.bin'), binary); + + generateProjectFromTemplate(context(root), { templateRoot }); + + // Binary is copied exactly as-is; substitution rules never apply + const result = readFileSync(join(root, 'my-project', 'payload.bin')); + expect(result).toEqual(binary); + // Both tokens remain in the output + expect(result.toString('utf8')).toContain('{{projectName}}'); + expect(result.toString('utf8')).toContain('{{userName}}'); + })); + + it('distinguishes binary from text even if the filename suggests text', () => + withTempDirectory((root) => { + const templateRoot = join(root, 'template'); + mkdirSync(templateRoot); + + // Binary content with a .txt extension + const binary = Buffer.concat([ + Buffer.from([0xff]), + Buffer.from('{{projectName}}={{unknownToken}}\n', 'utf8'), + ]); + writeFileSync(join(templateRoot, 'data.txt'), binary); + + generateProjectFromTemplate(context(root), { templateRoot }); + + // Classification is byte-based, not extension-based. + // The file contains 0xFF (NUL at the start), so it's binary. + const result = readFileSync(join(root, 'my-project', 'data.txt')); + expect(result).toEqual(binary); + // No substitution or error — binary path is taken + })); +});