Skip to content

fix(scripts): detect env reads inside a for-of over a literal-name array (#8652) - #8694

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
RealDiligent:fix/critical-issue-env-reference-array-loop-8652
Jul 25, 2026
Merged

fix(scripts): detect env reads inside a for-of over a literal-name array (#8652)#8694
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
RealDiligent:fix/critical-issue-env-reference-array-loop-8652

Conversation

@RealDiligent

Copy link
Copy Markdown
Contributor

Problem

Closes #8652.

scripts/gen-selfhost-env-reference.ts recognized computed env[X] access only via a string literal, the envString(env, "X") helper, or a helper whose name argument is a literal at the call site. It had no case for a var name sourced from iterating a local array of string literals.

src/selfhost/preflight.ts reads four critical secret tokens only through such a loop:

const CRITICAL_SECRET_VARS = ["GITHUB_WEBHOOK_SECRET", "LOOPOVER_API_TOKEN", "LOOPOVER_MCP_TOKEN", "INTERNAL_JOB_TOKEN", "SELFHOST_SETUP_TOKEN"] as const;
for (const name of CRITICAL_SECRET_VARS) { const value = nonBlank(env[name]); ... }

env[name] is a computed access whose argument is the loop variable, not a string literal, so it was invisible to the element-access branch. The four tokens above (SELFHOST_SETUP_TOKEN is separately read literally elsewhere, so it was already covered) were missing from the operator-facing self-host env reference — exactly the high-risk secrets preflight’s own comment says bypass real checks silently if left weak.

Fix

Extend the scan generically:

  • A pre-pass (collectLiteralStringArrays) collects every locally-declared const NAME = ["A", "B", ...] whose initializer is an array of only string literals, unwrapping a trailing as const.
  • A new ForOfStatement branch surfaces the array’s names when the loop iterates such an array with a single identifier loop variable whose body reads env[loopVar].

Generalizes to any such array — no var name is special-cased. The regenerated apps/loopover-ui/src/lib/selfhost-env-reference.ts adds exactly the four tokens (GITHUB_WEBHOOK_SECRET, INTERNAL_JOB_TOKEN, LOOPOVER_API_TOKEN, LOOPOVER_MCP_TOKEN) and nothing else.

Tests

A fixture-driven regression test in test/unit/selfhost-env-reference-script.test.ts drives the new array-loop path directly, asserting the two positive names are detected, plus negative shapes that exercise every new branch: a literal-name array whose loop never reads env, a destructured loop variable, an assignment-target loop (no const), a non-identifier iterable (Object.keys(env)), an unknown iterable identifier, and numeric/empty arrays. npm run selfhost:env-reference -- --check passes; git diff --check clean.

@RealDiligent
RealDiligent requested a review from JSONbored as a code owner July 25, 2026 23:39
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

gen-selfhost-env-reference.ts recognized computed env[X] access only via a string literal, the
envString(env, "X") helper, or a helper whose name argument is a literal at the call site. It had
no case for a name sourced from iterating a local array of string literals, so the four critical
secret tokens src/selfhost/preflight.ts reads only through

  for (const name of CRITICAL_SECRET_VARS) { const value = nonBlank(env[name]); ... }

(GITHUB_WEBHOOK_SECRET, LOOPOVER_API_TOKEN, LOOPOVER_MCP_TOKEN, INTERNAL_JOB_TOKEN) were absent from
the operator-facing self-host env reference -- exactly the high-risk secrets that table exists to warn
operators about.

Extend the scan: a pre-pass collects every locally-declared const array of only string literals
(unwrapping a trailing 'as const'), and a new for-of branch surfaces the array's names when the loop
iterates it with a single identifier loop variable whose body reads env[loopVar]. Generalizes to any
such array -- no var name is special-cased. Regenerated reference adds exactly the four tokens.

Test: a fixture-driven regression test drives the new array-loop path directly, with positive and
negative shapes (non-env loop, destructured loop var, non-identifier iterable, unknown identifier,
numeric/empty arrays) exercising every new branch.
@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.76%. Comparing base (c5cc6c4) to head (0bcea95).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8694   +/-   ##
=======================================
  Coverage   93.76%   93.76%           
=======================================
  Files         797      797           
  Lines       79454    79454           
  Branches    24070    24070           
=======================================
  Hits        74504    74504           
  Misses       3565     3565           
  Partials     1385     1385           
Flag Coverage Δ
backend 95.03% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 25, 2026
@loopover-orb

loopover-orb Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Tip

✅ LoopOver review result - approve/merge recommended

Review updated: 2026-07-25 23:55:23 UTC

3 files · 1 AI reviewer · no blockers · readiness 100/100 · CI green · clean

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
This PR closes a real gap: preflight.ts's CRITICAL_SECRET_VARS loop reads env[name] via a computed access whose argument is a loop identifier, which none of the existing branches (property access, string-literal element access, destructuring, or literal-arg helper calls) could see. The fix adds a targeted pre-pass (collectLiteralStringArrays) plus a ForOfStatement branch that resolves the loop back to concrete names only when the loop variable is a single identifier and the body reads env[loopVar] directly, and the regenerated reference file adds exactly the four missing tokens. The test fixture is well-constructed, covering the positive path and six distinct negative shapes (destructuring, reassignment-target loop, call-expression iterable, undeclared iterable, non-string arrays) that map directly to the guards in the new code.

Nits — 5 non-blocking
  • scripts/gen-selfhost-env-reference.ts: the new ForOfStatement branch only checks `bodyReadsEnvByName` for a direct `env[loopVar]` read in the immediate loop body — if the body only aliases the loop var into another variable before reading env with it (e.g. `const key = name; env[key]`), it would silently stay invisible; worth a comment noting this known limitation similar to the existing helper-name-keyed caveats elsewhere in the file.
  • apps/loopover-ui/src/lib/selfhost-env-reference.ts is a large generated file (per the external size-smell note); this is expected for a generated artifact and not something to address in this PR.
  • scripts/gen-selfhost-env-reference.ts:192 nesting depth is a byproduct of walking nested AST checks in `envReadingForOfArrayLiterals`/`bodyReadsEnvByName`; consider early-return guards are already used well, so this is a very minor style note only.
  • Consider extending `bodyReadsEnvByName` (or documenting as a known gap) for the common alias-then-read pattern, since preflight-style loops could plausibly evolve to destructure or rename the loop variable before indexing env.
  • The inline comments in the diff (e.g. citing fix(scripts): selfhost-env-reference generator misses env reads inside a for-of loop over a literal-name array #8652 and preflight.ts) are a nice touch for future maintainers tracing why the ForOfStatement branch exists — keep that convention.

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #8652
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ✅ 25/25 PR body includes validation/test evidence.
Contributor workload ✅ 10/10 Author activity: 327 registered-repo PR(s), 134 merged, 37 issue(s).
Contributor context ✅ Confirmed Gittensor contributor RealDiligent; Gittensor profile; 327 PR(s), 37 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The PR adds a for-of-over-literal-array detection branch to the generator, regenerates the reference file with all four required tokens (GITHUB_WEBHOOK_SECRET, LOOPOVER_API_TOKEN, LOOPOVER_MCP_TOKEN, INTERNAL_JOB_TOKEN), and includes a fixture-driven regression test with a 2-element literal array exercising the new code path plus several negative cases.

Review context
  • Author: RealDiligent
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: Python, Ruby, TypeScript, Svelte, Cuda, JavaScript, Markdown, MDX
  • Official Gittensor activity: 327 PR(s), 37 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Keep the PR focused and include validation evidence before maintainer review.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask &lt;question&gt; answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat &lt;question&gt; answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@loopover-orb loopover-orb Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoopOver approves — the gate is satisfied and CI is green.

@loopover-orb
loopover-orb Bot merged commit 89c3e75 into JSONbored:main Jul 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(scripts): selfhost-env-reference generator misses env reads inside a for-of loop over a literal-name array

1 participant