Skip to content

Commit

Permalink
Improve trusted-suppress-native-method scriptlet
Browse files Browse the repository at this point in the history
Add support for the `stack` parameter.
  • Loading branch information
gorhill committed Dec 11, 2024
1 parent f14257d commit 7ed3470
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/js/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ function objectPruneFn(
? safe.String_split.call(rawNeedlePaths, / +/)
: [];
if ( stackNeedleDetails.matchAll !== true ) {
if ( matchesStackTrace(stackNeedleDetails, extraArgs.logstack) === false ) {
if ( matchesStackTraceFn(stackNeedleDetails, extraArgs.logstack) === false ) {
return;
}
}
Expand Down Expand Up @@ -531,13 +531,13 @@ function objectFindOwnerFn(

builtinScriptlets.push({
name: 'matches-stack-trace.fn',
fn: matchesStackTrace,
fn: matchesStackTraceFn,
dependencies: [
'get-exception-token.fn',
'safe-self.fn',
],
});
function matchesStackTrace(
function matchesStackTraceFn(
needleDetails,
logLevel = ''
) {
Expand Down Expand Up @@ -1169,13 +1169,13 @@ function abortOnStackTrace(
let v = owner[chain];
Object.defineProperty(owner, chain, {
get: function() {
if ( matchesStackTrace(needleDetails, extraArgs.log) ) {
if ( matchesStackTraceFn(needleDetails, extraArgs.log) ) {
throw new ReferenceError(getExceptionToken());
}
return v;
},
set: function(a) {
if ( matchesStackTrace(needleDetails, extraArgs.log) ) {
if ( matchesStackTraceFn(needleDetails, extraArgs.log) ) {
throw new ReferenceError(getExceptionToken());
}
v = a;
Expand Down Expand Up @@ -3447,6 +3447,7 @@ builtinScriptlets.push({
requiresTrust: true,
fn: trustedSuppressNativeMethod,
dependencies: [
'matches-stack-trace.fn',
'proxy-apply.fn',
'safe-self.fn',
],
Expand All @@ -3458,9 +3459,8 @@ function trustedSuppressNativeMethod(
stack = ''
) {
if ( methodPath === '' ) { return; }
if ( stack !== '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('trusted-suppress-native-method', methodPath, signature, how);
const logPrefix = safe.makeLogPrefix('trusted-suppress-native-method', methodPath, signature, how, stack);
const signatureArgs = safe.String_split.call(signature, /\s*\|\s*/).map(v => {
if ( /^".*"$/.test(v) ) {
return { type: 'pattern', re: safe.patternToRegex(v.slice(1, -1)) };
Expand All @@ -3478,6 +3478,7 @@ function trustedSuppressNativeMethod(
return { type: 'exact', value: undefined };
}
});
const stackNeedle = safe.initPattern(stack, { canNegate: true });
proxyApplyFn(methodPath, function(context) {
const { callArgs } = context;
if ( signature === '' ) {
Expand All @@ -3499,6 +3500,11 @@ function trustedSuppressNativeMethod(
}
}
}
if ( stackNeedle.matchAll !== true ) {
if ( matchesStackTraceFn(stackNeedle) === false ) {
return context.reflect();
}
}
if ( how === 'debug' ) {
debugger; // eslint-disable-line no-debugger
return context.reflect();
Expand Down

0 comments on commit 7ed3470

Please sign in to comment.