#302·AI-Trader

Session token stays valid until the end of its expiry day on SQLite

Author: LudwigJMarxCreated Sep 21, 2026Updated Sep 21, 2026

Summary

_get_user_by_token compares user_tokens.expires_at against datetime('now'). SQLite renders that as 2026-09-21 18:01:31, while _create_user_session writes 2026-09-21T00:00:01Z. The comparison is lexicographic and T (0x54) sorts above the space (0x20), so a token keeps being accepted until the end of its expiry day, up to almost 24 hours past its own expires_at.

Where

service/server/services.py:88-93

python
cursor.execute("""
    SELECT u.*, t.token as session_token
    FROM users u
    JOIN user_tokens t ON t.user_id = u.id
    WHERE t.token = ? AND t.expires_at > datetime('now')
""", (token,))

expires_at is written by _create_user_session (services.py:105):

python
expires_at = (datetime.now(timezone.utc) + timedelta(days=7)).isoformat().replace("+00:00", "Z")

This is the only place in the codebase that compares an expiry in SQL. The password reset expiry is parsed and compared in Python (routes_agent.py:850) and is not affected.

Environment

Commit d03ff6c
Python 3.14.7
sqlite3 3.53.4
OS macOS 26.6.2, arm64
Backend SQLite (DB_PATH pointed at a scratch file)

Reproduction

Three tokens on one user, then _get_user_by_token on each:

now (UTC): 2026-09-21T18:01:31.331393+00:00

case                      expires_at                         accepted  expected
expired earlier today     2026-09-21T00:00:01Z                   True     False   <-- wrong
expired yesterday         2026-09-20T18:01:31.331393Z           False     False
valid, expires tomorrow   2026-09-22T18:01:31.331393Z            True      True

The first token expired 18 hours before the call and was still accepted. The two other cases behave correctly, so the lookup is not simply broken.

why, in sqlite:
  datetime('now') renders  '2026-09-21 18:01:31'
  the row stores           '2026-09-21T00:00:01Z'
  '2026-09-21T00:00:01Z' > '2026-09-21 18:01:31' -> True

Scope

Four endpoints authenticate through _get_user_by_token:

Route routes_users.py
GET /api/users/me 109
GET /api/users/points 123
GET /api/users/points/history 173
POST /api/users/points/transfer 196

The last one moves points between accounts, so the window is not read only. This is not an authentication bypass: it needs a token that was valid at some point. It extends that token's life by up to a day.

Backend difference

On PostgreSQL the same SQL is rewritten by database.py:182-190:

SELECT 1 WHERE t.expires_at > to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC',
                                      'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')

That produces the stored format, so the comparison is correct there. The defect is specific to the SQLite backend.

Relation to #296

Same root cause, different form. #296 covers datetime('now', '-N day') in eight window queries; this is the bare datetime('now') used for an expiry. PR #301 does not touch this call site, and the reason is in its description: a fix that translates the bare form would also hit datetime('now') as a column default in CREATE TABLE, which is a schema question rather than a two line change. Fixing this one call site by comparing against a parameter does not have that problem.

Not verified

  • No test covers _get_user_by_token; the reproduction calls it directly against a temporary SQLite database.
  • Not measured against the hosted instance, only against the code at d03ff6c.
  • The PostgreSQL rewrite was produced by calling database._replace_sqlite_datetime_functions on the literal SQL, not by executing it against a server.
  • The reproduction is ambiguous if it starts within about a minute of midnight UTC, because the stored timestamp and the boundary then fall on different days.

On the reporting channel

There is no SECURITY.md, gh api repos/HKUDS/AI-Trader/security-advisories returns [], and security_and_analysis reads as null for an unprivileged caller, so whether private reporting is enabled could not be determined. #257 has asked for a private channel since 2026-06-17 with no reply. This is filed publicly because it needs a token that was already issued and valid, and because the behaviour follows from two lines of public source. If you would rather receive it privately, enable private vulnerability reporting and I will move it.