Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle trailing slashes on root route and avoid crashes due to AssertionErrors thrown by odd requests #1960

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

nexdrew
Copy link

@nexdrew nexdrew commented Jul 3, 2023

Pre-Submission Checklist

  • Opened an issue discussing these changes before opening the PR
  • Ran the linter and tests via make prepush
  • Included comprehensive and convincing tests for changes

Issues

Closes:

Summarize the issues that discussed these changes

The issues demonstrate how restify can crash the node process when using server.pre(restify.pre.sanitizePath()) and a request with trailing slashes on root is received. I experienced this via intruder.io scanning against my production API application that uses restify. Please see the repro case here as referenced in #1959.

Changes

What does this PR do?

This PR makes 3 basic changes, each with at least one new unit test:

  1. Changes the sanitizePath middleware in lib/plugins/pre/prePath.js to return a root url (/) instead of an empty string in the case where the actual url consists of more than one slash e.g. // or ///. This helps avoid numbers 2 and 3 below.

  2. Changes the lookup method in lib/router.js to return undefined when req.getUrl().pathname is falsy. This helps avoid calling this._registry.lookup(req.method, pathname) which will result in an AssertionError in the case that the pathname is falsy, for whatever reason.

  3. Changes the defaultRoute method in lib/router.js to expect/handle a falsy pathname by skipping registry lookup (which would throw an AssertionError) and raising the standard ResourceNotFoundError (which results in 404 response) with the message "Requested path does not exist". This, along with number 2, allows restify to gracefully handle a falsy pathname for whatever reason.

All changes are covered by unit tests, namely:

  1. sanitizePath in test/plugins/plugins.test.js
  2. GH-1959: lookup returns undefined when no handler found in test/router.test.js
  3. GH-1959: route handles 404 when pathname invalid in test/router.test.js

This is an attempt to make restify more robust and avoid crashes due to odd requests, if possible.

Please let me know if you have any questions or if you'd like me to change anything. Thanks!

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.

2 participants