Repository navigation
fix(licensing): accept an already exchanged api_key on /license/activate - #2815
daniloleonecarneiro wants to merge 1 commit into
Conversation
The licensing success page lets the operator trade the authorization code for an api_key by hand, which burns the code. /license/activate then failed with "authorization code expired or already used" for both the code and the api_key. When the exchange fails, try the value as an api_key and store it only after the licensing server accepts it.
Reviewer's GuideImplementationAdd fallback activation using the submitted value as an API key after exchange failure, persisting it only once the licensing server accepts it.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/licensing/runtime.ts" line_range="533" />
<code_context>
- await completeActivation(rc, result.api_key, result.tier, result.customer_id);
+ // The code may already have been traded for an api_key outside the instance,
+ // which burns it; accept that api_key as well.
+ if (await activateWithApiKey(rc, code)) return;
+
+ throw exchangeErr ?? new Error('Invalid or expired code');
</code_context>
<issue_to_address>
**API keys leak through activation URLs**
When an operator submits an already-exchanged API key in the `code` query parameter, `activateWithCode` passes the raw query value to `activateWithApiKey`, which sends the reusable credential in a URL retained in browser history and access logs; anyone who reads those records can reuse the key.
Move API-key activation out of the query string and send the credential in a request body or another channel that does not expose it in URLs.
</issue_to_address>
### Comment 2
<location path="src/licensing/runtime.ts" line_range="406" />
<code_context>
+ try {
+ await activateInstance(rc);
+ } catch (err) {
+ rc.apiKey = '';
+ logger.debug(`Value is not a valid api_key either: ${readErrorMessage(err)}`);
+ return false;
</code_context>
<issue_to_address>
**Failed activations corrupt the active key**
When an active instance receives an invalid key, or overlapping activation requests include one accepted key and one rejected value, `activateWithApiKey` writes candidates to shared `rc.apiKey` and clears it on failure, so an existing key or an accepted request's key is lost; `markActivated` can then hash the wrong key and gated requests fail.
Keep each candidate separate from shared runtime state during activation, and update `rc.apiKey` and the active context only after that candidate is accepted.
Also at `src/licensing/runtime.ts:395`.
</issue_to_address>
### Comment 3
<location path="src/licensing/runtime.ts" line_range="414-417" />
<code_context>
+ rc.registerUrl = '';
+ rc.registerToken = '';
+ try {
+ await saveRuntimeData({ apiKey, tier: rc.tier, customerId: 0 });
+ } catch (err) {
+ logger.warn(`Could not save license: ${readErrorMessage(err)}`);
+ }
+
+ markActivated(rc, apiKey, rc.tier);
</code_context>
<issue_to_address>
**Failed saves still report activation success**
When the database rejects the API-key write, `activateWithApiKey` catches the `saveRuntimeData` error, then calls `markActivated` and returns success; the route reports a successful activation even though the license is lost on restart.
Do not mark the runtime active or report success when saving the API key fails; propagate an activation error instead.
</issue_to_address>
### Comment 4
<location path="src/licensing/runtime.ts" line_range="405" />
<code_context>
+ rc.apiKey = apiKey;
+ try {
+ await activateInstance(rc);
+ } catch (err) {
+ rc.apiKey = '';
+ logger.debug(`Value is not a valid api_key either: ${readErrorMessage(err)}`);
</code_context>
<issue_to_address>
**Licensing outages appear as invalid codes**
When the code exchange reports an expired code and the fallback API-key activation fails with a timeout or server error, `activateWithApiKey` discards the fallback error, so `activateWithCode` throws the earlier exchange error and the route returns 400 instead of a retryable 502.
Preserve and propagate the fallback activation error when it indicates a service failure, rather than returning the earlier exchange error.
</issue_to_address>
### Comment 5
<location path="src/licensing/runtime.ts" line_range="414" />
<code_context>
+ rc.registerUrl = '';
+ rc.registerToken = '';
+ try {
+ await saveRuntimeData({ apiKey, tier: rc.tier, customerId: 0 });
+ } catch (err) {
+ logger.warn(`Could not save license: ${readErrorMessage(err)}`);
</code_context>
<issue_to_address>
**Stored customer ID survives key replacement**
When runtimeConfig has a leftover customer_id row but no API key when this fallback succeeds, `saveRuntimeData` receives `customerId: 0`, but its customer-ID write runs only for positive values, so `loadRuntimeData` pairs the newly accepted key with the previous customer ID.
Clear the stored customer_id when saving a license whose customer ID is unknown.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 4 findings to address first, and if the fallback is wrong, a value accepted as an API key can activate the instance and be saved as its license credential. Reverting the code will not itself clear that saved credential or undo an activation that already occurred.
Blocking findings: src/licensing/runtime.ts:533, src/licensing/runtime.ts:406, src/licensing/runtime.ts:417, src/licensing/runtime.ts:405
|
Closing without merging. Accepting an already exchanged api_key on the public |
📋 Description
When a registration starts without
redirect_uri, the licensing success page shows the authorization code. If that code is traded for an api_key outside the instance (POST /v1/register/exchange), it is burnt, and/license/activate?code=then fails withauthorization code expired or already usedfor both the code and the resulting api_key.activateWithCodenow falls back to treating the value as an api_key when the exchange fails, the same waycompleteActivationalready resolves "code or key". The value is stored only after/v1/activateaccepts it, so a wrong code is never saved as the license. If both fail, the original exchange error is returned (still400 Invalid or expired code; network errors still502).🧪 Type of Change
🧪 Testing
Local licensing server (current
mainand the next version) + API bundle pointing to it, directGET /license/register, login, code traded by hand:develop/license/activate?code=<burnt code>/license/activate?code=<api_key from the manual exchange>/license/activate?code=<garbage>RuntimeConfigtsc --noEmit,eslint(pre-commit) andnpm run build+lint:check(pre-push)✅ Checklist
Summary by Sourcery
Bug Fixes:
/license/activateto activate with an API key that was already exchanged for the submitted authorization code, while preserving the existing exchange-error response if the value is not accepted.