Skip to content

Commit

Permalink
improve toString stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
mcolella14 committed Feb 16, 2021
1 parent 4948cb5 commit dfe8ee4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pyppeteer_stealth/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// We try to use a known "anchor" line for that and strip it with everything above it.
// If the anchor line cannot be found for some reason we fall back to our blacklist approach.

const stripWithBlacklist = stack => {
const stripWithBlacklist = (stack, stripFirstLine = true) => {
const blacklist = [
`at Reflect.${trap} `, // e.g. Reflect.get or Reflect.apply
`at Object.${trap} `, // e.g. Object.get or Object.apply
Expand All @@ -52,16 +52,16 @@
err.stack
.split('\n')
// Always remove the first (file) line in the stack (guaranteed to be our proxy)
.filter((line, index) => index !== 1)
.filter((line, index) => !(index === 1 && stripFirstLine))
// Check if the line starts with one of our blacklisted strings
.filter(line => !blacklist.some(bl => line.trim().startsWith(bl)))
.join('\n')
)
}

const stripWithAnchor = stack => {
const stripWithAnchor = (stack, anchor) => {
const stackArr = stack.split('\n')
const anchor = `at Object.newHandler.<computed> [as ${trap}] ` // Known first Proxy line in chromium
anchor = anchor || `at Object.newHandler.<computed> [as ${trap}] ` // Known first Proxy line in chromium
const anchorIndex = stackArr.findIndex(line =>
line.trim().startsWith(anchor)
)
Expand All @@ -74,6 +74,16 @@
return stackArr.join('\n')
}

// Special cases due to our nested toString proxies
err.stack = err.stack.replace(
'at Object.toString (',
'at Function.toString ('
)
if ((err.stack || '').includes('at Function.toString (')) {
err.stack = stripWithBlacklist(err.stack, false)
throw err
}

// Try using the anchor method, fallback to blacklist if necessary
err.stack = stripWithAnchor(err.stack) || stripWithBlacklist(err.stack)

Expand Down

0 comments on commit dfe8ee4

Please sign in to comment.