Skip to content

fix: protect method names from token registration collisions#359

Open
baima365-web wants to merge 1 commit into
expressjs:masterfrom
baima365-web:fix/token-reserved-name
Open

fix: protect method names from token registration collisions#359
baima365-web wants to merge 1 commit into
expressjs:masterfrom
baima365-web:fix/token-reserved-name

Conversation

@baima365-web

Copy link
Copy Markdown

Problem

Calling morgan.token('token', fn) overwrites the morgan.token() method itself, breaking all subsequent token registrations. This is because morgan serves as both the module export (with methods token, format, compile) and the token registry.

Example:

var logger = require('morgan');
logger.token('token', function (req, res) { return 'value' });
// morgan.token is now the custom function, not the registration method
logger.token('uuid', function (req, res) { return 'uuid' }); // TypeError: tokens.uuid is not a function

Fix

Make the token, format, and compile methods non-writable using Object.defineProperties. This prevents user token registrations from overwriting the module API while maintaining full backward compatibility.

Test plan

  • Existing built-in tokens work correctly
  • Custom token registration works
  • Compiled format functions resolve tokens correctly
  • Middleware logs correctly
  • Registering reserved names (token, format, compile) now throws instead of silently breaking

Prevent token registration from overwriting module methods (token,
format, compile) by making them non-writable. Previously, calling
morgan.token('token', fn) would overwrite the morgan.token() method,
breaking subsequent token registrations.

Fixes expressjs#265
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.

1 participant