|
| 1 | +import { describe, expect, it } from '@rstest/core'; |
| 2 | +import { normalizeCommonOptions } from '../../src/utils/options'; |
| 3 | + |
| 4 | +describe('normalizeCommonOptions --env parsing', () => { |
| 5 | + it('builds a nested object from dotted keys', () => { |
| 6 | + const opts: any = { env: ['app.name=demo', 'app.debug=true'] }; |
| 7 | + normalizeCommonOptions(opts, 'build'); |
| 8 | + expect(opts.env.app.name).toBe('demo'); |
| 9 | + expect(opts.env.app.debug).toBe('true'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('sets RSPACK_BUILD and RSPACK_BUNDLE on build action', () => { |
| 13 | + const opts: any = { env: [] }; |
| 14 | + normalizeCommonOptions(opts, 'build'); |
| 15 | + expect(opts.env.RSPACK_BUILD).toBe(true); |
| 16 | + expect(opts.env.RSPACK_BUNDLE).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it('keeps env as a plain object so config functions can call Object methods', () => { |
| 20 | + const opts: any = { env: ['app.name=demo', 'flag=true'] }; |
| 21 | + normalizeCommonOptions(opts, 'build'); |
| 22 | + // User configs commonly do `env.hasOwnProperty(...)` or `env instanceof Object`. |
| 23 | + expect(opts.env instanceof Object).toBe(true); |
| 24 | + expect(Object.prototype.hasOwnProperty.call(opts.env, 'flag')).toBe(true); |
| 25 | + expect(opts.env.hasOwnProperty('app')).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('prototype-pollution hardening', () => { |
| 29 | + afterEach(() => { |
| 30 | + // Defensive cleanup so a regression in this file cannot leak pollution |
| 31 | + // into sibling test files in the same worker. |
| 32 | + delete (Object.prototype as any).RSPACK_BUILD; |
| 33 | + delete (Object.prototype as any).polluted; |
| 34 | + delete (Object.prototype as any).injected; |
| 35 | + }); |
| 36 | + |
| 37 | + it('rejects __proto__ in dotted env path without polluting Object.prototype', () => { |
| 38 | + const opts: any = { |
| 39 | + env: ['__proto__.polluted=yes', '__proto__.RSPACK_BUILD=owned'], |
| 40 | + }; |
| 41 | + normalizeCommonOptions(opts, 'build'); |
| 42 | + |
| 43 | + expect(({} as any).polluted).toBeUndefined(); |
| 44 | + expect(({} as any).RSPACK_BUILD).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('rejects constructor.prototype.x payloads', () => { |
| 48 | + const opts: any = { env: ['constructor.prototype.injected=1'] }; |
| 49 | + normalizeCommonOptions(opts, 'build'); |
| 50 | + expect(({} as any).injected).toBeUndefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not allow attacker to spoof reserved RSPACK_BUILD via prototype', () => { |
| 54 | + const opts: any = { env: ['__proto__.RSPACK_BUILD=owned'] }; |
| 55 | + normalizeCommonOptions(opts, 'build'); |
| 56 | + // Legitimate write must win — not the attacker-controlled "owned" string. |
| 57 | + expect(opts.env.RSPACK_BUILD).toBe(true); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments