Skip to content

Fix 404 for rules, templates, folders and silence in subfolders (Express 5 routes) - #13

Open
siccous wants to merge 1 commit into
johnsusek:masterfrom
BTLzdravtech:fix/express5-wildcard-routes
Open

Fix 404 for rules, templates, folders and silence in subfolders (Express 5 routes)#13
siccous wants to merge 1 commit into
johnsusek:masterfrom
BTLzdravtech:fix/express5-wildcard-routes

Conversation

@siccous

@siccous siccous commented Jul 16, 2026

Copy link
Copy Markdown

Note

AI disclosure: This fix was developed with the help of an AI assistant (Claude Code by Anthropic). The AI analyzed the regression, authored the code changes and this PR description. The changes were reviewed and the fix was verified end-to-end against a running server by a human maintainer before submission.

Problem

Since the Express 4 to 5 migration (02cc6f1), any rule, template, folder or silence target located inside a subfolder returns 404.

Express 5 (path-to-regexp v8) removed the :id* repeat syntax, and the migration replaced rules/:id* with rules/:id, which matches only a single path segment. So GET /rules/myfolder/myrule no longer matches any route and Express returns 404 before the handler even runs. The request.originalUrl.split('/') parsing added to the handlers in that commit is unreachable for those paths, and buggy even for single-segment paths (query strings leak into the rule path and segments are never URL-decoded).

Fix

  • Use Express 5 named wildcards, which match one or more segments: rules/*id, templates/*id, folders/:type/*path, silence/*path.
  • In the handlers, read the path from the wildcard param, which Express 5 provides as an array of already-decoded segments: request.params.id.join('/'). This replaces the originalUrl parsing and also fixes the query-string and URL-decoding issues.

Testing

Verified end-to-end against a running server:

  • GET /rules listing still works, as do single-segment rules (GET /rules/myrule).
  • GET /rules/dir/subdir/myrule and GET /templates/tdir/mytemplate return the YAML instead of 404.
  • PUT /folders/rules/newdir/sub, POST /rules/newdir/sub/newrule, DELETE /rules/newdir/sub/newrule and DELETE /folders/rules/newdir all work on nested paths.
  • POST /silence/dir/subdir/myrule invokes elastalert with the correctly resolved --rule .../rules/dir/subdir/myrule.yaml.
  • Query strings and URL-encoded segments (/rules/dir/my%20rule) now resolve correctly.
  • npm run lint passes.

🤖 Generated with Claude Code

Express 5 removed the ':id*' repeat syntax, and the migration in
02cc6f1 replaced it with ':id', which matches only a single path
segment. Any rule, template, folder or silence target inside a
subfolder no longer matched its route and returned 404.

Use Express 5 named wildcards (rules/*id, templates/*id,
folders/:type/*path, silence/*path) and read the path from the
wildcard param array instead of parsing request.originalUrl, which
also leaked query strings and skipped URL decoding.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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