Skip to content

fix: create new Error instead of mutating in redactToken#2088

Open
augchan42 wants to merge 1 commit into
telegraf:v4from
augchan42:fix/redact-token-new-error
Open

fix: create new Error instead of mutating in redactToken#2088
augchan42 wants to merge 1 commit into
telegraf:v4from
augchan42:fix/redact-token-new-error

Conversation

@augchan42

Copy link
Copy Markdown

Fix issue where redactToken was mutating the original error object and rethrowing it. Now creates a new Error to avoid potential side effects.

@MKRhere

MKRhere commented Apr 10, 2026

Copy link
Copy Markdown
Member

By creating a new error, you lose the original stack trace.

According to the spec [1], Error#message should be a writable property. I think an issue must be raised in runtimes that break from the spec. Meanwhile, it might be okay to have a way to opt-out of redaction with an option like dangerouslyLogToken or similar. Thoughts?

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message

@augchan42 augchan42 force-pushed the fix/redact-token-new-error branch from 96d01c5 to be19aee Compare April 10, 2026 14:52
@augchan42

Copy link
Copy Markdown
Author

Thanks for your comment. I just noticed my telegram bot was crashing when encountering this so this was done to avoid that crash.

I've updated the fix to preserve it:

function redactToken(error: Error): never {
  const message = error.message.replace(
    /\/(bot|user)(\d+):[^/]+\//,
    '/$1$2:[REDACTED]/'
  )
  const newError = new Error(message)
  newError.stack = error.stack
  throw newError
}

I think this would be easier for users.

@MKRhere

MKRhere commented Apr 11, 2026

Copy link
Copy Markdown
Member

And you've tested that overwriting Error#stack instead of Error#message does not error for you?

@augchan42

augchan42 commented Apr 11, 2026 via email

Copy link
Copy Markdown
Author

@siakinnik

Copy link
Copy Markdown

Hi! Upstream is dead since last year, so I've started a community fork telegraf-hardened. I'd love to have your PR there: https://github.com/siakinnik/telegraf-hardened/issues/1

@Leask

Leask commented May 5, 2026

Copy link
Copy Markdown

Included in #2092 via commit 6b7664f. The branch also preserves fetch error names in commit 327d7d3 so abort-related errors remain identifiable.

Tracking PR: #2092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants