|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { extend } from '../../../../src/core/core.js'; |
| 3 | +import { Markup } from '../../../../src/framework/components/element/markup.js'; |
| 4 | + |
| 5 | +describe('Security: Prototype Pollution', function () { |
| 6 | + describe('Markup Parser', function () { |
| 7 | + it('should ignore sensitive keys like __proto__ during merging', function () { |
| 8 | + // [__proto__ polluted="true"]hello[/__proto__] |
| 9 | + const symbols = [ |
| 10 | + '[', '_', '_', 'p', 'r', 'o', 't', 'o', '_', '_', ' ', 'p', 'o', 'l', 'l', 'u', 't', 'e', 'd', '=', '"', 't', 'r', 'u', 'e', '"', ']', |
| 11 | + 'h', 'e', 'l', 'l', 'o', |
| 12 | + '[', '/', '_', '_', 'p', 'r', 'o', 't', 'o', '_', '_', ']' |
| 13 | + ]; |
| 14 | + |
| 15 | + // Ensure Object.prototype is clean before test |
| 16 | + expect({}.polluted).to.be.undefined; |
| 17 | + |
| 18 | + const results = Markup.evaluate(symbols); |
| 19 | + |
| 20 | + expect(results.tags).to.not.equal(null); |
| 21 | + results.tags.forEach((tag) => { |
| 22 | + if (tag) { |
| 23 | + expect(Object.prototype.hasOwnProperty.call(tag, '__proto__')).to.be.false; |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + expect({}.polluted).to.be.undefined; |
| 28 | + }); |
| 29 | + |
| 30 | + it('should ignore constructor and prototype keys', function () { |
| 31 | + // [constructor]test[/constructor] |
| 32 | + const symbols = [ |
| 33 | + '[', 'c', 'o', 'n', 's', 't', 'r', 'u', 'c', 't', 'o', 'r', ']', |
| 34 | + 't', 'e', 's', 't', |
| 35 | + '[', '/', 'c', 'o', 'n', 's', 't', 'r', 'u', 'c', 't', 'o', 'r', ']' |
| 36 | + ]; |
| 37 | + |
| 38 | + const results = Markup.evaluate(symbols); |
| 39 | + expect(results.tags).to.not.equal(null); |
| 40 | + results.tags.forEach((tag) => { |
| 41 | + if (tag) { |
| 42 | + expect(Object.prototype.hasOwnProperty.call(tag, 'constructor')).to.be.false; |
| 43 | + } |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('Core: extend utility', function () { |
| 49 | + it('should protect against prototype pollution', function () { |
| 50 | + const payload = JSON.parse('{"__proto__": {"vulnerable": "yes"}}'); |
| 51 | + const target = {}; |
| 52 | + |
| 53 | + const originalPrototype = Object.getPrototypeOf(target); |
| 54 | + extend(target, payload); |
| 55 | + |
| 56 | + expect({}.vulnerable).to.be.undefined; |
| 57 | + expect(target.vulnerable).to.be.undefined; |
| 58 | + expect(Object.getPrototypeOf(target)).to.equal(originalPrototype); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should protect against constructor/prototype pollution', function () { |
| 62 | + const payload = JSON.parse('{"constructor": {"prototype": {"vulnerable": "yes"}}}'); |
| 63 | + const target = {}; |
| 64 | + |
| 65 | + extend(target, payload); |
| 66 | + |
| 67 | + expect({}.vulnerable).to.be.undefined; |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments