fix(openscience): stamp queued usage with its account so a flush can't bill the wrong one#130
Merged
Conversation
…t bill the wrong one Queued usage rows carried no account identity, so flushPendingUsage sent every row under whatever session was active at startup. If usage was queued under account A (or while the session was transiently unreadable) and account B later logged in, the flush billed B for A's usage. dropUsageQueue only runs on explicit logout, not a silent account swap. Stamp each queued row with a stable account tag (user_id, else a short hash of the api_key — never the raw key). On flush, a row tagged for a DIFFERENT account is kept in the queue rather than sent (it flushes when that account is active); legacy/untagged rows stay best-effort. Pure shouldFlushForAccount helper is unit-tested. (#14)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| * api_key (never the raw key, which must not sit in the queue file). */ | ||
| function accountTag(session: OpenScienceSession): string { | ||
| if (session.user_id) return session.user_id | ||
| return "k:" + createHash("sha256").update(session.api_key).digest("hex").slice(0, 16) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Audit finding #14 (High, billing). Queued usage rows carried no account identity, so
flushPendingUsagesent every row under whatever session was active at startup. If usage was queued under account A (e.g. while the session file was transiently unreadable) and account B later logged in, the startup flush billed B for A's usage — anddropUsageQueueonly runs on an explicit logout, not a silent account swap.user_id, else a short SHA-256 of theapi_key— never the raw key, which must not sit in the queue file).Tests
test/openscience/usage-queue-account.test.tscovers the pureshouldFlushForAccountdecision (same account → flush, different → refuse, accountless → best-effort). Full backend suite (949) green.