Skip to content

fix(token): throw on reserved token names instead of silently corrupting morgan - #370

Open
Samiullah-2004 wants to merge 1 commit into
expressjs:masterfrom
Samiullah-2004:fix-reserved-token-name
Open

fix(token): throw on reserved token names instead of silently corrupting morgan#370
Samiullah-2004 wants to merge 1 commit into
expressjs:masterfrom
Samiullah-2004:fix-reserved-token-name

Conversation

@Samiullah-2004

Copy link
Copy Markdown

Fixes #265

Calling morgan.token('token', fn) (or 'format'/'compile') silently
overwrote morgan's own internal functions, since token() just does
morgan[name] = fn. This meant registering a token named "token" would
destroy morgan.token itself, causing every subsequent .token() call
to fail with an unrelated TypeError, with no indication of the real cause.

This PR:

  • Adds a check in token() that throws a clear TypeError when a
    reserved name (token, format, compile) is used
  • Adds regression tests, including one that verifies morgan.token
    remains a valid function after a rejected registration (the exact
    failure mode from the original bug report)
  • Documents the reserved names in the Readme, as requested by
    @dougwilson in the original issue thread

All 91 existing tests still pass.

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.

Adding a token named token causes all subsequent tokens to break.

1 participant