fix(auth): honor APIFY_TOKEN env var over stored login token#1293
Open
l2ysho wants to merge 1 commit into
Open
fix(auth): honor APIFY_TOKEN env var over stored login token#1293l2ysho wants to merge 1 commit into
l2ysho wants to merge 1 commit into
Conversation
`APIFY_TOKEN=<token> apify run` and other commands silently ignored the
env var and used the token from `apify login` instead, so runs failed with
"Insufficient permissions" and `apify actors ls` returned the wrong account.
Two independent causes, same symptom:
- `resolveToken` (src/lib/utils.ts) resolved only an explicit token arg then
the stored token, never `process.env.APIFY_TOKEN`. This choke point feeds
~50 commands via getLoggedClient/getApifyClientOptions. Precedence is now:
explicit `--token` > `APIFY_TOKEN` > stored login.
- `apify run` injected the stored token into the child env after `process.env`
(`{ ...process.env, ...localEnvVars }`), clobbering an inherited APIFY_TOKEN.
The merge order was flipped in #1042 so the input-key redirect vars win; that
flip also caught the token as collateral. Now the stored token only fills in
when APIFY_TOKEN isn't already inherited, leaving #1042's behavior intact.
Adds local unit tests for the precedence at getApifyClientOptions and an
[api] regression test proving `apify run` doesn't override an inherited token.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
Makes the
APIFY_TOKENenvironment variable take precedence over the token stored byapify login. It was being silently ignored, so:APIFY_TOKEN=<customer_token> apify runfailed withApifyApiError: Insufficient permissions for the Actor run.APIFY_TOKEN=<customer_token> apify actors lsreturned Actors from the logged-in account, not the token's account.Why
Two independent causes produced the same symptom:
resolveToken(src/lib/utils.ts) — the single choke point behindgetLoggedClient/getApifyClientOptions(~50 commands) resolved only an explicitly-passed token, then fell straight through to the stored token. It never consultedprocess.env.APIFY_TOKEN. This gap is longstanding (predates the keyring refactor, which faithfully preserved it).apify run(src/commands/run.ts) — the stored token was injected into the child env afterprocess.env({ ...process.env, ...localEnvVars }), overwriting an inheritedAPIFY_TOKEN. The merge order was intentionally flipped in fix: prevent modifying local input.json file #1042 so the CLI's input-key redirect vars win over inherited ones; that flip also caught the token as collateral damage.Change
Precedence is now, everywhere: explicit
--tokenarg →APIFY_TOKENenv var → stored login token.resolveTokenreturnsprocess.env.APIFY_TOKENbefore falling back to the stored token.apify runonly injects the stored token whenAPIFY_TOKENisn't already inherited — leaving fix: prevent modifying local input.json file #1042's input-key redirect behavior fully intact.Tests
getApifyClientOptions: env var wins over stored, explicit arg wins over env var, stored token used when neither is set.[api]regression test (test/local/commands/run.test.ts) — sets an inheritedAPIFY_TOKENdistinct from the stored token and asserts the spawned Actor sees the inherited one.Reviewer notes
apify runregression traces specifically to fix: prevent modifying local input.json file #1042 (2026-04-28).apify runstill sourcesproxy/userIdfrom the stored account; only the token honors the env override. Out of scope for this fix (the reported bug is token-only).🤖 Generated with Claude Code