Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #4087: Create a proxy object to hide properties. (#4086)
Browse files Browse the repository at this point in the history
Properties can only be hidden if they are non-configurable & writable.
If a property is non-configurable, readonly, it is impossible to hide as a TypeError will be thrown.
  • Loading branch information
Brandon-T authored Aug 25, 2021
1 parent 38caef0 commit cd8ec53
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -5002,11 +5002,11 @@
D0FCF7E91FE44DA2004A7995 /* MainFrame */,
595E0EDA21CAD97C00813D49 /* CookieControl.js */,
0AE50853261C63D70099C6A3 /* BraveSearchHelper.js */,
F930CDAB227000F200A23FE1 /* U2F.js */,
F99505FE22937E3900CC6543 /* U2F-low-level.js */,
5E3477E822D7771700B0D5F8 /* ResourceDownloader.js */,
5E4845BF22DE381200372022 /* WindowRenderHelper.js */,
5EB57D0122FDC0CB00A07325 /* FullscreenHelper.js */,
F930CDAB227000F200A23FE1 /* U2F.js */,
F9B23EE123F61173000EB3D8 /* PaymentRequest.js */,
5E4E078224A0E4D700B01720 /* YoutubeAdblock.js */,
0A64384D24FD4E43000E80A3 /* ArchiveIsCompat.js */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,82 @@ if (!window.__firefox__) {
enumerable: false,
configurable: false,
writable: false,
value: {
userScripts: {},
includeOnce: function(userScript, initializer) {
if (!__firefox__.userScripts[userScript]) {
__firefox__.userScripts[userScript] = true;
if (typeof initializer === 'function') {
initializer();
}
return false;
}
value: (function() {
'use strict';

let userScripts = {};
let values = {};
let includeOnce = function(userScript, initializer) {
if (!userScripts[userScript]) {
userScripts[userScript] = true;
if (typeof initializer === 'function') {
initializer();
}
return false;
}

return true;
}
}
return true;
};

let proxy = new Proxy({}, {
get(target, property, receiver) {
const descriptor = Object.getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return Reflect.get(target, property, receiver);
}

if (property === 'includeOnce') {
return includeOnce;
}

return Reflect.get(values, property, receiver);
},

set(target, name, value, receiver) {
if (name === 'includeOnce') {
return false;
}

const descriptor = getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return false;
}

return Reflect.set(values, name, value, receiver);
},

defineProperty(target, property, descriptor) {
if (descriptor && !descriptor.configurable) {
if (descriptor.set && !descriptor.get) {
return false;
}

if (!descriptor.writable) {
return Reflect.defineProperty(target, property, descriptor);
}
}

return Reflect.defineProperty(values, property, descriptor);
},

getOwnPropertyDescriptor(target, property) {
const descriptor = Object.getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return descriptor;
}

return Object.getOwnPropertyDescriptor(values, property);
},

ownKeys(target) {
var keys = [];
keys = keys.concat(Object.keys(target));
keys = keys.concat(Object.getOwnPropertyNames(target));
return keys;
}
});

return proxy;
})()
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,82 @@ if (!window.__firefox__) {
enumerable: false,
configurable: false,
writable: false,
value: {
userScripts: {},
includeOnce: function(userScript, initializer) {
if (!__firefox__.userScripts[userScript]) {
__firefox__.userScripts[userScript] = true;
if (typeof initializer === 'function') {
initializer();
}
return false;
}
value: (function() {
'use strict';

let userScripts = {};
let values = {};
let includeOnce = function(userScript, initializer) {
if (!userScripts[userScript]) {
userScripts[userScript] = true;
if (typeof initializer === 'function') {
initializer();
}
return false;
}

return true;
}
}
return true;
};

let proxy = new Proxy({}, {
get(target, property, receiver) {
const descriptor = Object.getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return Reflect.get(target, property, receiver);
}

if (property === 'includeOnce') {
return includeOnce;
}

return Reflect.get(values, property, receiver);
},

set(target, name, value, receiver) {
if (name === 'includeOnce') {
return false;
}

const descriptor = getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return false;
}

return Reflect.set(values, name, value, receiver);
},

defineProperty(target, property, descriptor) {
if (descriptor && !descriptor.configurable) {
if (descriptor.set && !descriptor.get) {
return false;
}

if (!descriptor.writable) {
return Reflect.defineProperty(target, property, descriptor);
}
}

return Reflect.defineProperty(values, property, descriptor);
},

getOwnPropertyDescriptor(target, property) {
const descriptor = Object.getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return descriptor;
}

return Object.getOwnPropertyDescriptor(values, property);
},

ownKeys(target) {
var keys = [];
keys = keys.concat(Object.keys(target));
keys = keys.concat(Object.getOwnPropertyNames(target));
return keys;
}
});

return proxy;
})()
});
}
21 changes: 1 addition & 20 deletions Client/Frontend/UserContent/UserScripts/Playlist.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
// The below is needed because the script may not be web-packed into a bundle so it may be missing the run-once code

// MARK: - Include Once

if (!window.__firefox__) {
window.__firefox__ = {};
}

if (!window.__firefox__.includeOnce) {
window.__firefox__ = {};
window.__firefox__.includeOnce = function(key, func) {
var keys = {};
if (!keys[key]) {
keys[key] = true;
func();
}
};
}

// MARK: - Media Detection

window.__firefox__.includeOnce("$<Playlist>", function() {
window.__firefox__.includeOnce("Playlist", function() {
function is_nan(value) {
return typeof value === "number" && value !== value;
}
Expand Down
21 changes: 1 addition & 20 deletions Client/Frontend/UserContent/UserScripts/PlaylistDetector.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
// The below is needed because the script may not be web-packed into a bundle so it may be missing the run-once code

// MARK: - Include Once

if (!window.__firefox__) {
window.__firefox__ = {};
}

if (!window.__firefox__.includeOnce) {
window.__firefox__ = {};
window.__firefox__.includeOnce = function(key, func) {
var keys = {};
if (!keys[key]) {
keys[key] = true;
func();
}
};
}

// MARK: - Media Detection

window.__firefox__.includeOnce("$<PlaylistDetector>", function() {
window.__firefox__.includeOnce("PlaylistDetector", function() {
function is_nan(value) {
return typeof value === "number" && value !== value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,82 @@ if (!window.__firefox__) {
enumerable: false,
configurable: false,
writable: false,
value: {
userScripts: {},
includeOnce: function(userScript, initializer) {
if (!__firefox__.userScripts[userScript]) {
__firefox__.userScripts[userScript] = true;
if (typeof initializer === 'function') {
initializer();
}
return false;
}
value: (function() {
'use strict';

let userScripts = {};
let values = {};
let includeOnce = function(userScript, initializer) {
if (!userScripts[userScript]) {
userScripts[userScript] = true;
if (typeof initializer === 'function') {
initializer();
}
return false;
}

return true;
}
}
return true;
};

let proxy = new Proxy({}, {
get(target, property, receiver) {
const descriptor = Object.getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return Reflect.get(target, property, receiver);
}

if (property === 'includeOnce') {
return includeOnce;
}

return Reflect.get(values, property, receiver);
},

set(target, name, value, receiver) {
if (name === 'includeOnce') {
return false;
}

const descriptor = getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return false;
}

return Reflect.set(values, name, value, receiver);
},

defineProperty(target, property, descriptor) {
if (descriptor && !descriptor.configurable) {
if (descriptor.set && !descriptor.get) {
return false;
}

if (!descriptor.writable) {
return Reflect.defineProperty(target, property, descriptor);
}
}

return Reflect.defineProperty(values, property, descriptor);
},

getOwnPropertyDescriptor(target, property) {
const descriptor = Object.getOwnPropertyDescriptor(target, property);
if (descriptor && !descriptor.configurable && !descriptor.writable) {
return descriptor;
}

return Object.getOwnPropertyDescriptor(values, property);
},

ownKeys(target) {
var keys = [];
keys = keys.concat(Object.keys(target));
keys = keys.concat(Object.getOwnPropertyNames(target));
return keys;
}
});

return proxy;
})()
});
}

0 comments on commit cd8ec53

Please sign in to comment.