|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | | - |
3 | | -// We cannot easily import the generate-version script (it runs main() immediately), |
4 | | -// so we extract and test the core logic: VERSION_REGEX and JSON.stringify defense. |
5 | | - |
6 | | -const VERSION_REGEX = /^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.\-]+)?(\+[a-zA-Z0-9.\-]+)?$/; |
| 2 | +import { VERSION_REGEX, validateVersion } from '../version/version-validation.ts'; |
7 | 3 |
|
8 | 4 | describe('generate-version: VERSION_REGEX validation', () => { |
9 | 5 | it('accepts standard semver', () => { |
@@ -47,25 +43,14 @@ describe('generate-version: VERSION_REGEX validation', () => { |
47 | 43 | }); |
48 | 44 | }); |
49 | 45 |
|
50 | | -describe('generate-version: JSON.stringify defense-in-depth', () => { |
51 | | - it('produces safe code even if a value somehow contains quotes', () => { |
52 | | - const malicious = "1.0.0'; process.exit(1); //"; |
53 | | - const generated = `const version = ${JSON.stringify(malicious)};\n`; |
54 | | - // The output should use escaped double-quoted string, not break out |
55 | | - expect(generated).toContain('"1.0.0'); |
56 | | - expect(generated).not.toContain("'1.0.0'; process.exit(1)"); |
57 | | - // Should be parseable JS (using const instead of export for Function() compat) |
58 | | - expect(() => new Function(generated)).not.toThrow(); |
| 46 | +describe('generate-version: validateVersion', () => { |
| 47 | + it('throws for invalid versions', () => { |
| 48 | + expect(() => validateVersion('version', "1.0.0'; process.exit(1); //")).toThrow( |
| 49 | + /Invalid version in package\.json/, |
| 50 | + ); |
59 | 51 | }); |
60 | 52 |
|
61 | | - it('JSON.stringify properly escapes backslashes and control characters', () => { |
62 | | - const tricky = '1.0.0\n";process.exit(1);//'; |
63 | | - const serialized = JSON.stringify(tricky); |
64 | | - // The newline should be escaped as \\n, and the quote should be escaped |
65 | | - expect(serialized).toContain('\\n'); |
66 | | - expect(serialized).toContain('\\"'); |
67 | | - // The resulting assignment should be valid JS |
68 | | - const code = `const v = ${serialized};`; |
69 | | - expect(() => new Function(code)).not.toThrow(); |
| 53 | + it('does not throw for valid versions', () => { |
| 54 | + expect(() => validateVersion('version', '1.0.0-alpha.1+meta')).not.toThrow(); |
70 | 55 | }); |
71 | 56 | }); |
0 commit comments