Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Moddle): replicate set/get API for createAny elements #54

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/moddle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
isString,
isObject,
forEach
forEach,
set
} from 'min-dash';

import Factory from './factory.js';
Expand Down Expand Up @@ -142,11 +143,11 @@ Moddle.prototype.createAny = function(name, nsUri, properties) {
$instanceOf: function(type) {
return type === this.$type;
},
get: function(name) {
return this[name];
get: function(key) {
return this[key];
},
set: function(name, value) {
this[name] = value;
set: function(key, value) {
set(this, [ key ], value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

}
};

Expand Down
27 changes: 27 additions & 0 deletions test/spec/moddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,33 @@ describe('moddle', function() {
// then
expect(anyInstance.get('bar')).to.eql('BAR');
});


it('should prevent prototype pollution', function() {

// given
var anyInstance = model.createAny('other:Foo', 'http://other');

// when

expect(function() {
anyInstance.set('__proto__', { hacked() { console.log('hacked'); } });
}).to.throw('illegal key: __proto__');

});


it('should NOT allow array as key', function() {

// given
var anyInstance = model.createAny('other:Foo', 'http://other');

// when
expect(function() {
anyInstance.set([ 'path', 'to', 'key' ], 'value');
}).to.throw('illegal key type: object. Key should be of type number or string.');
});

});


Expand Down