|
2 | 2 | compileGetter as GETTER, |
3 | 3 | compileSetter as SETTER |
4 | 4 | } from 'core/utils/data'; |
| 5 | +import errors from 'core/errors'; |
5 | 6 | import variableWrapper from 'core/utils/variable_wrapper'; |
6 | 7 |
|
7 | 8 | const mockVariableWrapper = { |
@@ -480,6 +481,88 @@ QUnit.module('setter', () => { |
480 | 481 | }); |
481 | 482 |
|
482 | 483 |
|
| 484 | +QUnit.module('prototype pollution protection', { |
| 485 | + beforeEach: function() { |
| 486 | + sinon.spy(errors, 'log'); |
| 487 | + }, |
| 488 | + afterEach: function() { |
| 489 | + errors.log.restore(); |
| 490 | + delete Object.prototype['pp_dx']; |
| 491 | + delete Object.prototype['pp_dx2']; |
| 492 | + } |
| 493 | +}, () => { |
| 494 | + |
| 495 | + test('compileSetter logs error and skips assignment for __proto__ path fragment', function(assert) { |
| 496 | + SETTER('__proto__.pp_dx')({}, 'yes', { functionsAsIs: true }); |
| 497 | + |
| 498 | + assert.strictEqual(errors.log.calledWith('E0123', '__proto__'), true, 'should log E0123 for __proto__'); |
| 499 | + assert.strictEqual(({}).pp_dx, undefined, 'Object.prototype must not be polluted'); |
| 500 | + }); |
| 501 | + |
| 502 | + test('compileSetter logs error and skips assignment for constructor path fragment', function(assert) { |
| 503 | + SETTER('constructor.prototype.pp_dx')({}, 'yes', { functionsAsIs: true }); |
| 504 | + |
| 505 | + assert.strictEqual(errors.log.calledWith('E0123', 'constructor'), true, 'should log E0123 for constructor'); |
| 506 | + assert.strictEqual(({}).pp_dx, undefined, 'Object.prototype must not be polluted'); |
| 507 | + }); |
| 508 | + |
| 509 | + test('compileSetter logs error and skips assignment for prototype path fragment', function(assert) { |
| 510 | + const fn = function() {}; |
| 511 | + SETTER('prototype.pp_dx')(fn, 'yes', { functionsAsIs: true }); |
| 512 | + |
| 513 | + assert.strictEqual(errors.log.calledWith('E0123', 'prototype'), true, 'should log E0123 for prototype'); |
| 514 | + assert.strictEqual(fn.prototype.pp_dx, undefined, 'function prototype must not be modified'); |
| 515 | + }); |
| 516 | + |
| 517 | + test('compileSetter works normally for safe paths', function(assert) { |
| 518 | + const obj = { a: { b: 1 } }; |
| 519 | + SETTER('a.b')(obj, 42); |
| 520 | + |
| 521 | + assert.strictEqual(obj.a.b, 42, 'safe paths must still work'); |
| 522 | + assert.strictEqual(errors.log.called, false, 'should not log for safe paths'); |
| 523 | + }); |
| 524 | + |
| 525 | + test('compileGetter logs error and returns undefined for __proto__ path fragment', function(assert) { |
| 526 | + const result = GETTER('__proto__.pp_dx')({}); |
| 527 | + |
| 528 | + assert.strictEqual(errors.log.calledWith('E0123', '__proto__'), true, 'should log E0123 for __proto__'); |
| 529 | + assert.strictEqual(result, undefined, 'getter must return undefined for __proto__'); |
| 530 | + }); |
| 531 | + |
| 532 | + test('compileGetter logs error and returns undefined for constructor path fragment', function(assert) { |
| 533 | + const result = GETTER('constructor.prototype')(function() {}); |
| 534 | + |
| 535 | + assert.strictEqual(errors.log.calledWith('E0123', 'constructor'), true, 'should log E0123 for constructor'); |
| 536 | + assert.strictEqual(result, undefined, 'getter must return undefined for constructor'); |
| 537 | + }); |
| 538 | + |
| 539 | + test('compileGetter logs error and returns undefined for prototype path fragment', function(assert) { |
| 540 | + const result = GETTER('prototype.pp_dx')(function() {}); |
| 541 | + |
| 542 | + assert.strictEqual(errors.log.calledWith('E0123', 'prototype'), true, 'should log E0123 for prototype'); |
| 543 | + assert.strictEqual(result, undefined, 'getter must return undefined for prototype'); |
| 544 | + }); |
| 545 | + |
| 546 | + test('combineGetters logs error and skips __proto__ fragment, returns safe fields', function(assert) { |
| 547 | + const obj = { safe: 'value' }; |
| 548 | + const result = GETTER(['__proto__.pp_dx', 'safe'])(obj); |
| 549 | + |
| 550 | + assert.strictEqual(errors.log.calledWith('E0123', '__proto__'), true, 'should log E0123 for __proto__'); |
| 551 | + assert.strictEqual(({}).pp_dx, undefined, 'Object.prototype must not be polluted'); |
| 552 | + assert.deepEqual(result, { safe: 'value' }, 'safe field must still be returned'); |
| 553 | + }); |
| 554 | + |
| 555 | + test('combineGetters logs error and skips constructor fragment, returns safe fields', function(assert) { |
| 556 | + const obj = { safe: 'value' }; |
| 557 | + const result = GETTER(['constructor.prototype.pp_dx', 'safe'])(obj); |
| 558 | + |
| 559 | + assert.strictEqual(errors.log.calledWith('E0123', 'constructor'), true, 'should log E0123 for constructor'); |
| 560 | + assert.strictEqual(({}).pp_dx, undefined, 'Object.prototype must not be polluted'); |
| 561 | + assert.deepEqual(result, { safe: 'value' }, 'safe field must still be returned'); |
| 562 | + }); |
| 563 | +}); |
| 564 | + |
| 565 | + |
483 | 566 | QUnit.module('setter with wrapped variables', { |
484 | 567 | beforeEach: function() { |
485 | 568 | variableWrapper.inject(mockVariableWrapper); |
|
0 commit comments