claude-usage icon indicating copy to clipboard operation
claude-usage copied to clipboard

Bookmarkable URLs (?range=all) return 404 because handler compares self.path literally

Open jakduch opened this issue 7 months ago • 3 comments

Summary

do_GET and do_POST in dashboard.py route by comparing self.path literally against "/", "/index.html", "/api/data", "/api/rescan". Because self.path includes the query string, any URL with parameters falls through to the 404 branch — including the bookmarkable URLs that the dashboard itself produces.

Reproduction

$ curl -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8080/
200
$ curl -o /dev/null -w '%{http_code}\n' 'http://127.0.0.1:8080/?range=all'
404
$ curl -o /dev/null -w '%{http_code}\n' 'http://127.0.0.1:8080/?range=30d&models=claude-opus-4-7'
404

The dashboard's range buttons call history.replaceState({}, '', '?range=' + r), so the moment a user clicks a range button, the URL becomes one of the 404 forms above. Reloading the page or sharing the link then breaks. ?range=all is also the natural URL someone would type when trying to bookmark the "all-time" view.

Cause

# dashboard.py do_GET
if self.path in ("/", "/index.html"):
    ...
elif self.path == "/api/data":
    ...
else:
    self.send_response(404)

self.path for GET /?range=all HTTP/1.1 is "/?range=all", not "/".

Suggested fix

Strip the query string before routing using urllib.parse.urlparse:

from urllib.parse import urlparse  # already in stdlib

def do_GET(self):
    path = urlparse(self.path).path
    if path in ("/", "/index.html"):
        ...
    elif path == "/api/data":
        ...
    else:
        self.send_response(404)

def do_POST(self):
    path = urlparse(self.path).path
    if path == "/api/rescan":
        ...
    else:
        self.send_response(404)

self.path is otherwise unused — the handlers don't read query parameters server-side; all filtering is client-side from /api/data.

Tests

tests/test_dashboard.py has coverage for / and /api/data but not for any URL with a query string. A regression test would fit cleanly there:

def test_index_with_query_string_returns_html(self):
    with urllib.request.urlopen(f"http://127.0.0.1:{self.port}/?range=30d") as resp:
        self.assertEqual(resp.status, 200)
        self.assertIn(b"Claude Code Usage Dashboard", resp.read())

PR coming.

Severity

High. The dashboard generates these URLs itself, so most users hit this the first time they click a range button and try to reload.

jakduch avatar Apr 25 '26 22:04 jakduch

Implemented the fix in commit e9937b2 on branch HaydenHaines:fix/bookmarkable-query-urls and opened PR #96: https://github.com/phuryn/claude-usage/pull/96

Summary:

  • Dashboard GET/POST handlers now route on urlparse(self.path).path, so query strings no longer fall through to 404.
  • Added regression coverage for /?range=30d, /api/data?range=all, and POST /api/rescan?source=dashboard.

Tests:

  • python -m pytest tests/test_dashboard.py
  • python -m pytest

Note: Direct push to phuryn/claude-usage origin/main failed with a 403 for my configured credentials, so I pushed the branch to the fork and opened the PR instead. Leaving this issue open until the PR is merged.

HaydenHaines avatar Apr 29 '26 14:04 HaydenHaines

[Claude & Codex] Fixed on the DEV branch (v1.1.0) by your own PR #81 — router now strips the query string via urlparse and you bundled regression tests for /?range=... and /api/data?_=cachebust. Closing-eligible.

phuryn avatar May 28 '26 18:05 phuryn

Closing as fixed in v1.1.0 — see the comment above. Please reopen if the dashboard is still blank after pulling DEV.

— Claude Code & Codex collab

phuryn avatar May 28 '26 18:05 phuryn