|
| 1 | +// test/transform.proto-bypass.test.js |
| 2 | +// Tests for prototype pollution bypass fix in transform() (CVE-2023-26132 bypass) |
| 3 | + |
| 4 | +var assert = require('assert'); |
| 5 | +var dottie = require('../dottie'); |
| 6 | + |
| 7 | +describe('dottie.transform - prototype pollution bypass prevention', function () { |
| 8 | + |
| 9 | + // === __proto__ at non-first positions === |
| 10 | + |
| 11 | + it('should block __proto__ at second position in keys', function () { |
| 12 | + var flat = { 'user.__proto__.isAdmin': true, 'user.name': 'guest' }; |
| 13 | + var result = dottie.transform(flat); |
| 14 | + |
| 15 | + // The isAdmin property should NOT be reachable via prototype chain |
| 16 | + assert.strictEqual( |
| 17 | + result.user === undefined || result.user.isAdmin === undefined, true, |
| 18 | + '__proto__ bypass in transform keys should be blocked' |
| 19 | + ); |
| 20 | + |
| 21 | + // Global Object.prototype must remain clean |
| 22 | + assert.strictEqual(({}).isAdmin, undefined, |
| 23 | + 'Object.prototype must not be polluted'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should block __proto__ at third position in keys', function () { |
| 27 | + var flat = { 'a.b.__proto__.polluted': true }; |
| 28 | + var result = dottie.transform(flat); |
| 29 | + assert.strictEqual(({}).polluted, undefined, |
| 30 | + 'Object.prototype must not be polluted'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should still block __proto__ at first position (original fix)', function () { |
| 34 | + var flat = { '__proto__.polluted': true }; |
| 35 | + var result = dottie.transform(flat); |
| 36 | + assert.strictEqual(({}).polluted, undefined, |
| 37 | + 'Object.prototype must not be polluted'); |
| 38 | + }); |
| 39 | + |
| 40 | + // === constructor and prototype keys === |
| 41 | + |
| 42 | + it('should block constructor-based pollution in transform keys', function () { |
| 43 | + var flat = { 'a.constructor.prototype.polluted': true }; |
| 44 | + var result = dottie.transform(flat); |
| 45 | + assert.strictEqual(({}).polluted, undefined, |
| 46 | + 'constructor-based pollution must be blocked'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should block prototype key in transform keys', function () { |
| 50 | + var flat = { 'a.prototype.polluted': true }; |
| 51 | + var result = dottie.transform(flat); |
| 52 | + assert.strictEqual(({}).polluted, undefined, |
| 53 | + 'prototype-based pollution must be blocked'); |
| 54 | + }); |
| 55 | + |
| 56 | + // === Legitimate transforms should still work === |
| 57 | + |
| 58 | + it('should transform normal dotted keys correctly', function () { |
| 59 | + var flat = { |
| 60 | + 'user.name': 'Alice', |
| 61 | + 'user.email': 'alice@example.com', |
| 62 | + 'user.settings.theme': 'dark' |
| 63 | + }; |
| 64 | + var result = dottie.transform(flat); |
| 65 | + assert.strictEqual(result.user.name, 'Alice'); |
| 66 | + assert.strictEqual(result.user.email, 'alice@example.com'); |
| 67 | + assert.strictEqual(result.user.settings.theme, 'dark'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments