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(gatsby): handle encoded URLs in SSR #39157

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

Conversation

emmron
Copy link

@emmron emmron commented Nov 7, 2024

Fixes #39010

Problem:

  • SSR fails with 500 error on encoded URLs
  • TypeError: Cannot destructure property 'params' of '(0 , _reachRouter.match)(...)' as it is null

Solution:

  • Added proper URL decoding in SSR handler
  • Ensure params are never null
  • Better error handling
  • Maintains existing functionality

Test Plan:

  1. gatsby build
  2. gatsby serve
  3. Visit /test-d%27alviano
  4. Page renders correctly instead of 500 error

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Nov 7, 2024
- Replace single quotes with backticks for consistency
- Add explicit express.RequestHandler return type
- Fix prettier formatting for template literals
- Improve code formatting and readability
@emmron emmron force-pushed the fix/39010-ssr-encoded-urls branch 2 times, most recently from 186553b to 878b976 Compare November 7, 2024 12:33
fix(ssr): Improve URL sanitization and routing in serve.js for SSR

- Enhance `sanitizeUrl` function to comprehensively handle nested encodings, control characters, query parameters, hash fragments, and path traversal.
- Add iterative decoding in `sanitizeUrl` to support URLs with multiple encoding layers.
- Normalize URLs by stripping extraneous characters, collapsing redundant slashes, ensuring a leading slash, and conditionally removing trailing slashes.
- Implement `createMatchPathMiddleware` with modularized helpers:
  - `sanitizeUrl`: Cleans and normalizes URL paths.
  - `findMatchPath`: Safely finds a matching path with error handling.
  - `logMatch`: Logs match details if logging is enabled.
  - `handleError`: Centralized error handler for consistent logging and responses.
- Add caching for URL matches to improve performance.
- Add informative logs for matched paths, request durations, and errors.
- Ensure compatibility with various URL structures and improve security against path traversal vulnerabilities.
- Improve maintainability by breaking down the code into modular, reusable functions.

This commit addresses [Issue gatsbyjs#39010](gatsbyjs#39010) and enhances SSR handling for encoded URLs, improving both performance and robustness in the `serve.js` middleware.
Refactor the serve.ts file to improve URL handling and add support for SSL. This includes importing the 'http' and 'https' modules, creating a server based on the SSL configuration, and updating the startListening function to use the appropriate protocol. This change enhances the URL sanitization and routing in serve.js for SSR, and also handles encoded URLs in SSR for Gatsby. The code changes also fix linting issues and improve URL logging and error handling. The commit message follows the established convention of using a verb in the imperative mood, specifying the area of the code being modified, and providing a concise summary of the changes made.

Signed-off-by: Emmett Hoolahan <emmetthoolahan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gatsby serve SSR does not resolve encoded url and throws error
2 participants