Block common hacker paths with 404 response #1155
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Bots and hackers frequently probe web applications with common attack paths looking for vulnerabilities:
/.env(environment variables)/wp-admin,/wp-login.php(WordPress installations)/phpmyadmin(database admin panels)/admin,/administrator(admin panels).php,.asp,.jsp(which Serlo doesn't use)These requests were reaching the database through the alias query system in
redirectToCurrentAlias, causing unnecessary database load despite Serlo not having any content at these paths.Solution
Added an early check in the
redirectToCurrentAliasfunction that returns a 404 response for common hacker paths before querying the database:The check is performed in
redirectToCurrentAlias(), returningnew Response('Not Found', { status: 404 })immediately for blocked paths and preventing the database query.Impact
✅ Reduces unnecessary database queries from bot/hacker probes
✅ Returns proper 404 HTTP status for invalid paths
✅ No impact on legitimate traffic - all existing tests pass
✅ Case-insensitive matching (blocks both
/WP-ADMINand/wp-admin)✅ Comprehensive test coverage with 30 new tests
✅ Minimal code footprint (66 lines of production code)
Examples
Blocked paths (return 404, will NOT reach database):
/.env→ 404/wp-admin→ 404/phpmyadmin→ 404/test.php→ 404/admin/login.asp→ 404Allowed paths (work normally):
/23591/mathematics-homepage/mathe/1385/zahlen-und-größen/user/meFixes #797
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.