|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { load } from '../src/index.js'; |
| 3 | + |
| 4 | +describe('Security', () => { |
| 5 | + describe('Prototype Pollution Prevention', () => { |
| 6 | + it('should not allow __proto__ pollution', () => { |
| 7 | + const toml = ` |
| 8 | +[__proto__] |
| 9 | +polluted = true |
| 10 | +`; |
| 11 | + |
| 12 | + const result = load(toml); |
| 13 | + |
| 14 | + const testObj = {}; |
| 15 | + expect('polluted' in testObj).toBe(false); |
| 16 | + expect('polluted' in Object.prototype).toBe(false); |
| 17 | + |
| 18 | + expect(result).toHaveProperty('__proto__'); |
| 19 | + expect(result['__proto__']).toHaveProperty('polluted', true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should not allow constructor pollution', () => { |
| 23 | + const toml = ` |
| 24 | +[constructor] |
| 25 | +[constructor.prototype] |
| 26 | +polluted = true |
| 27 | +`; |
| 28 | + |
| 29 | + const result = load(toml); |
| 30 | + |
| 31 | + const testObj = {}; |
| 32 | + expect('polluted' in testObj).toBe(false); |
| 33 | + expect('polluted' in Object.prototype).toBe(false); |
| 34 | + |
| 35 | + expect(result).toHaveProperty('constructor'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should reproduce the authentication bypass scenario', () => { |
| 39 | + const toml = ` |
| 40 | +[__proto__] |
| 41 | +isAdmin = true |
| 42 | +`; |
| 43 | + |
| 44 | + // Simulate the vulnerable authentication function |
| 45 | + const isAdmin = (user: Record<string, unknown>) => { |
| 46 | + return user.isAdmin === true; |
| 47 | + }; |
| 48 | + |
| 49 | + // Parse the malicious TOML |
| 50 | + load(toml); |
| 51 | + |
| 52 | + // Create a user object that should not be admin |
| 53 | + const user = { username: 'foo' }; |
| 54 | + |
| 55 | + // This should return false (user is not admin) |
| 56 | + // If prototype pollution occurred, this would return true |
| 57 | + expect(isAdmin(user)).toBe(false); |
| 58 | + expect('isAdmin' in user).toBe(false); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle dotted __proto__ keys safely', () => { |
| 62 | + const toml = ` |
| 63 | +[table.__proto__] |
| 64 | +polluted = true |
| 65 | +`; |
| 66 | + |
| 67 | + const result = load(toml); |
| 68 | + |
| 69 | + const testObj = {}; |
| 70 | + expect('polluted' in testObj).toBe(false); |
| 71 | + expect('polluted' in Object.prototype).toBe(false); |
| 72 | + |
| 73 | + expect('table' in result).toBe(true); |
| 74 | + expect('__proto__' in result['table']).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle inline table __proto__ safely', () => { |
| 78 | + const toml = ` |
| 79 | +obj = { __proto__ = { polluted = true } } |
| 80 | +`; |
| 81 | + |
| 82 | + const result = load(toml); |
| 83 | + |
| 84 | + const testObj = {}; |
| 85 | + expect('polluted' in testObj).toBe(false); |
| 86 | + expect('polluted' in Object.prototype).toBe(false); |
| 87 | + |
| 88 | + expect(result).toHaveProperty('obj'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should not allow pollution via array of tables syntax', () => { |
| 93 | + const toml = ` |
| 94 | +[[__proto__]] |
| 95 | +polluted = true |
| 96 | +`; |
| 97 | + const result = load(toml); |
| 98 | + |
| 99 | + expect('polluted' in Object.prototype).toBe(false); |
| 100 | + expect('polluted' in Array.prototype).toBe(false); |
| 101 | + |
| 102 | + expect(result).toHaveProperty('__proto__'); |
| 103 | + expect(Array.isArray(result['__proto__'])).toBe(true); |
| 104 | + expect(result['__proto__'][0]).toEqual({ polluted: true }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle deeply nested __proto__ keys safely', () => { |
| 108 | + const toml = ` |
| 109 | +[a.b.__proto__.c] |
| 110 | +polluted = true |
| 111 | +`; |
| 112 | + const result = load(toml); |
| 113 | + |
| 114 | + const testObj = {}; |
| 115 | + expect('polluted' in testObj).toBe(false); |
| 116 | + |
| 117 | + const resultObj = result as Record< |
| 118 | + string, |
| 119 | + Record<string, Record<string, Record<string, unknown>>> |
| 120 | + >; |
| 121 | + expect(resultObj.a.b['__proto__'].c).toEqual({ polluted: true }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should treat "prototype" as a regular key', () => { |
| 125 | + const toml = ` |
| 126 | +[prototype] |
| 127 | +polluted = true |
| 128 | +`; |
| 129 | + const result = load(toml); |
| 130 | + |
| 131 | + const testObj = {}; |
| 132 | + expect('polluted' in testObj).toBe(false); |
| 133 | + |
| 134 | + expect(result).toHaveProperty('prototype'); |
| 135 | + expect(result['prototype']).toEqual({ polluted: true }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments