Skip to content

Fix IndexDefect in route matching from unsafe path slicing#287

Draft
Copilot wants to merge 3 commits into
develfrom
copilot/fix-random-indexdefects
Draft

Fix IndexDefect in route matching from unsafe path slicing#287
Copilot wants to merge 3 commits into
develfrom
copilot/fix-random-indexdefects

Conversation

Copilot AI commented Jan 10, 2026

Copy link
Copy Markdown
Contributor

The route matcher performs slice operations like path[pathIndex .. ^1] when extracting path parameters. When pathIndex >= path.len (e.g., greedy params at path end, trailing slashes stripped by normalization), this throws IndexDefect: index out of bounds, the container is empty.

Changes

  • src/prologue/core/route.nim: Add bounds checks before all path slicing in matchTree function

    • Greedy parameter extraction: if pathIndex < path.len before path[pathIndex .. ^1]
    • Non-greedy parameter extraction: same check when separator not found
    • Non-greedy with separator: if pathIndex < newPathIndex before slicing
    • Set parameter to empty string when bounds exceeded
  • tests/mock/tmock_mocking/tmock_route.nim: Add test for greedy parameter with trailing slash edge case

# Before: IndexDefect when pathIndex >= path.len
ctx.request.pathParams[node.value] = path[pathIndex .. ^1]

# After: Safe extraction with bounds check
if pathIndex < path.len:
  ctx.request.pathParams[node.value] = path[pathIndex .. ^1]
else:
  ctx.request.pathParams[node.value] = ""

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • nim-lang.org
    • Triggering command: /usr/bin/curl curl REDACTED -sSf (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Random IndexDefects</issue_title>
<issue_description>I have a web app in prod an every once in a while an IndexDefect will occur.
Don't think it's my code and I'm a little unsure as how to diagnose?

ERROR IndexDefect: index out of bounds, the container is empty
Async traceback:
  /mnt/c/Users/.../Desktop/Projects/nimAuxChord_2/app.nim(131) app
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/application.nim(538) run
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/application.nim(533) run
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/beast/server.nim(31) serve
  /mnt/c/Users/.../.nimble/pkgs2/httpx-0.3.7-6e0fc3133fbd20530d53ab2792e93d3151387b10/httpx.nim(682) run
  /mnt/c/Users/.../.choosenim/toolchains/nim-2.0.0/lib/system.nim(568) eventLoop
  /mnt/c/Users/.../.nimble/pkgs2/httpx-0.3.7-6e0fc3133fbd20530d53ab2792e93d3151387b10/httpx.nim(478) processEvents
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/application.nim(534) :anonymous
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/application.nim(501) handleRequest
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/application.nim(460) handleContext (Async)
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/middlewaresbase.nim(48) switch (Async)
  /mnt/c/Users/.../.choosenim/toolchains/nim-2.0.0/lib/pure/asyncmacro.nim(293) :anonymous
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/middlewares/staticfile.nim(33) anonymous (Async)
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/middlewaresbase.nim(48) switch (Async)
  /mnt/c/Users/.../.choosenim/toolchains/nim-2.0.0/lib/pure/asyncmacro.nim(293) :anonymous
  /mnt/c/Users/.../Desktop/Projects/nimAuxChord_2/app.nim(71) anonymous (Async)
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/middlewaresbase.nim(51) switch (Async)
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/route.nim(506) findHandler
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/route.nim(483) findHandler
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/route.nim(473) findHandler
  /mnt/c/Users/.../.nimble/pkgs2/prologue-0.6.4-60e959a0e99a1e8fccca9d05440080651b74bccd/prologue/core/route.nim(138) ensureCorrectRoute
  /mnt/c/Users/.../.choosenim/toolchains/nim-2.0.0/lib/system/indices.nim(0) []
  /mnt/c/Users/.../.choosenim/toolchains/nim-2.0.0/lib/system/fatal.nim(53) sysFatal
Exception message: index out of bounds, the container is empty
task prod , "Build server in production mode":
    --stackTrace    : on  
    --lineTrace     : on    
    --forceBuild    : on  
    --threads       : off    
    --mm            : orc         
    --deepcopy      : on      
    --hints         : off      
    --outdir        : "."   
    
    # Defines
    # --define        : release       
    --define        : ssl           
    setCommand "c"  , "app.nim"
64 | proc blocklistMiddleware* :  HandlerAsync =
65 |     result = proc(ctx: Context) {.async.} =
66 |         if ctx.request.body.strip() in blocked:
67 |             resp abort()
68 |             return
69 |         
70 |         # do something before
71 |         await switch(ctx)
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 10, 2026 07:50
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix random IndexDefects occurring in production Jan 10, 2026
Copilot AI requested a review from ringabout January 10, 2026 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants