Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from TremayneChrist/protection_improvements
Browse files Browse the repository at this point in the history
Prevent access from other protected objects
  • Loading branch information
TremayneChrist authored Sep 3, 2016
2 parents d03e19a + 75b971d commit 2b0c91b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions protect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

'use strict';

var callDepth = 0; // Stores the call depth of the current execution

var locked = function () {
return !callDepth;
};

// The protect object
var protect = function (_O) {

var callDepth = 0; // Stores the call depth of the current execution

var locked = function () {
return !callDepth;
};

var O = _O.prototype || _O; // Protect the prototype, if there is one

Object.keys(O).forEach(function (key) {
Expand Down
20 changes: 20 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ describe('ProtectJS', function () {
testObj._function = function () {};
expect(testObj._function).to.be.undefined;
});

it('Should NOT allow PRIVATE property access from other objects', function () {
var obj2 = {
fn: function () {
return testObj._function();
},
prop: function () {
return testObj._number + testObj._string;
}
};

// Unprotected
expect(obj2.fn).to.throw();
expect(obj2.prop()).to.be.NaN;

// Protected
protect(obj2);
expect(obj2.fn).to.throw();
expect(obj2.prop()).to.be.NaN;
});
};

describe('Literal Objects', function () {
Expand Down

0 comments on commit 2b0c91b

Please sign in to comment.