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

Prevent Server Errors from Malformed URL Requests #915

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ app.use((req, res, next) => {
next()
})

// Ensure URL is properly encoded to prevent decoding errors and malformed requests
app.use((req, res, next) => {
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ive tested both, URL.canParse returns true because it only checks if the URL is syntactically valid. It doesn’t validate percent-encoded sequences, so malformed encodings (e.g., %AD) pass even though decodeURIComponent would fail.

decodeURIComponent(req.url); // Validate the URL
next();
} catch (err) {
console.error('Malformed URL detected:', req.url);
return res.status(400).send('Bad Request: Malformed URL');
}
});

// no ending slashes for SEO reasons
// https://github.com/epicweb-dev/epic-stack/discussions/108
app.get('*', (req, res, next) => {
Expand Down