Skip to content

fix(auth): honor APIFY_TOKEN env var over stored login token#1293

Open
l2ysho wants to merge 1 commit into
masterfrom
claude/apify-token-permissions-bug-15c25d
Open

fix(auth): honor APIFY_TOKEN env var over stored login token#1293
l2ysho wants to merge 1 commit into
masterfrom
claude/apify-token-permissions-bug-15c25d

Conversation

@l2ysho

@l2ysho l2ysho commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

What

Makes the APIFY_TOKEN environment variable take precedence over the token stored by apify login. It was being silently ignored, so:

  • APIFY_TOKEN=<customer_token> apify run failed with ApifyApiError: Insufficient permissions for the Actor run.
  • APIFY_TOKEN=<customer_token> apify actors ls returned Actors from the logged-in account, not the token's account.

Why

Two independent causes produced the same symptom:

  1. resolveToken (src/lib/utils.ts) — the single choke point behind getLoggedClient / getApifyClientOptions (~50 commands) resolved only an explicitly-passed token, then fell straight through to the stored token. It never consulted process.env.APIFY_TOKEN. This gap is longstanding (predates the keyring refactor, which faithfully preserved it).

  2. apify run (src/commands/run.ts) — the stored token was injected into the child env after process.env ({ ...process.env, ...localEnvVars }), overwriting an inherited APIFY_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 --token arg → APIFY_TOKEN env var → stored login token.

  • resolveToken returns process.env.APIFY_TOKEN before falling back to the stored token.
  • apify run only injects the stored token when APIFY_TOKEN isn't already inherited — leaving fix: prevent modifying local input.json file #1042's input-key redirect behavior fully intact.

Tests

  • Local unit tests (test/local/lib/credentials.test.ts) — precedence at 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 inherited APIFY_TOKEN distinct from the stored token and asserts the spawned Actor sees the inherited one.

Reviewer notes

🤖 Generated with Claude Code

`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>
@l2ysho
l2ysho requested a review from DaveHanns as a code owner July 21, 2026 14:01
@github-actions github-actions Bot added this to the 145th sprint - Tooling team milestone Jul 21, 2026
@github-actions github-actions Bot added t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics. labels Jul 21, 2026
@l2ysho l2ysho added adhoc Ad-hoc unplanned task added during the sprint. t-builders Issues owned by the Builders team. and removed t-tooling Issues with this label are in the ownership of the tooling team. labels Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-builders Issues owned by the Builders team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants