Skip to content

fix(github): guard the NaN expiry in mintInstallationToken - #10074

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/mint-token-nan-expiry-10026
Jul 31, 2026
Merged

fix(github): guard the NaN expiry in mintInstallationToken#10074
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/mint-token-nan-expiry-10026

Conversation

@shin-core

Copy link
Copy Markdown
Contributor

What & why

mintInstallationToken's local App-JWT path parsed GitHub's expires_at straight into the cache entry with no finiteness check:

const expiresAtMs = payload.expires_at ? Date.parse(payload.expires_at) : Date.now() + 50 * 60_000;

Date.parse returns NaN for a present-but-unparseable string, and a NaN expiresAtMs makes the cache-freshness comparison expiresAtMs - TOKEN_SAFETY_MARGIN_MS > Date.now() forever false. So a single malformed expires_at silently disables the installation-token cache: every job re-mints, re-triggering exactly the thundering-herd (and the GitHub secondary-rate-limit on the token endpoint) the cache exists to prevent.

The fix

Treat an unparseable expires_at identically to an absent one — fall back to Date.now() + 50 * 60_000:

const parsedExpiry = payload.expires_at ? Date.parse(payload.expires_at) : Number.NaN;
const expiresAtMs = Number.isFinite(parsedExpiry) ? parsedExpiry : Date.now() + 50 * 60_000;
  • Well-formed expires_at → still exactly Date.parse(payload.expires_at) (byte-identical).
  • Absent expires_at → still the Date.now() + 50 * 60_000 fallback.
  • Unparseable expires_at → now the same finite fallback instead of NaN.

No cache-lifetime constant changes (TOKEN_SAFETY_MARGIN_MS, the 50-minute fallback, all unchanged).

Tests (test/unit/github-app.test.ts)

  • Regression (named for the bug): after a mint whose expires_at is unparseable, a second createInstallationToken for the same installation issues no further /access_tokens request — asserted by the mint-call count (mints === 1), not just the token string (fails on main, which re-mints).
  • Well-formed expires_at: the cached token is reused on the second call (mint count 1) — unchanged behaviour.

Validation

  • npm run typecheck clean for this file (the only local errors are a pre-existing missing-release-please-dep phantom in unrelated release-tooling, present on bare main); the github-app suite green.
  • Diff coverage on src/github/app.ts is 100% (both Number.isFinite arms and both expires_at? arms).
  • git diff --check <base> HEAD clean; single-file source change + its test.

Closes #10026

`mintInstallationToken` parsed GitHub's `expires_at` with `Date.parse` and no
finiteness check. A present-but-unparseable value returns NaN, and a NaN
`expiresAtMs` makes `expiresAtMs - TOKEN_SAFETY_MARGIN_MS > Date.now()` forever
false — silently disabling the installation-token cache so every job re-mints,
re-triggering the thundering-herd (and the GitHub secondary-rate-limit) the cache
exists to prevent.

Treat an unparseable `expires_at` exactly like an absent one: fall back to
`Date.now() + 50 * 60_000`. A well-formed value still produces exactly
`Date.parse(payload.expires_at)`, and an absent one still uses the same fallback —
both byte-identical to before. No cache-lifetime constant changes.

Closes JSONbored#10026
@shin-core
shin-core requested a review from JSONbored as a code owner July 31, 2026 06:23
@superagent-security

Copy link
Copy Markdown
Contributor

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

@loopover-orb

loopover-orb Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Tip

✅ LoopOver review result - approve/merge recommended

Review updated: 2026-07-31 06:36:34 UTC

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

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
This is a correct, narrowly-scoped bug fix: the local App-JWT path in mintInstallationToken (src/github/app.ts:361-369) parsed a present-but-unparseable expires_at into NaN via Date.parse, and NaN silently defeats the cache-freshness check (expiresAtMs - MARGIN > Date.now() is forever false), forcing a re-mint on every call. The fix correctly falls through to Number.isFinite(parsedExpiry) ? parsedExpiry : Date.now() + 50*60_000, matching the existing absent-expires_at fallback exactly and leaving the well-formed path byte-identical. The included regression test directly exercises the real bug by stubbing expires_at as 'not-a-date' and asserting mint count stays at 1, which is a real (not fabricated) test of the fixed path, and a second test confirms well-formed behavior is unchanged.

Nits — 4 non-blocking
  • The `50 * 60_000` fallback duplicates the literal already used on the absent-expires_at branch just above it in the pre-existing code — consider extracting a named constant (e.g. `DEFAULT_TOKEN_TTL_MS`) shared by both branches, per the external brief's magic-number flag.
  • PR description doesn't explicitly link/close an eligible open issue in the repo-required format, though the code comment and test both reference github(app): guard the NaN expiry in mintInstallationToken so an unparseable expires_at cannot disable the installation-token cache #10026 — worth confirming that issue is open and this PR is tied to it per the repo's issue-linking requirement.
  • src/github/app.ts:368 — factor `50 * 60_000` into a shared constant used by both the absent and unparseable-expiry fallback paths to avoid future drift between them.
  • Consider a follow-up log line (warn-level) when the unparseable-expiry fallback path is hit, since a malformed expires_at from GitHub is itself signal worth surfacing, distinct from the silent NaN behavior being fixed here.

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 #10026
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: 64 registered-repo PR(s), 50 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor shin-core; Gittensor profile; 64 PR(s), 0 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The diff mirrors the broker-client guard exactly (Number.isFinite check with the same 50*60_000 fallback), preserving byte-identical behavior for well-formed and absent expires_at, and adds both a regression test asserting mint-call count for the unparseable case and a test confirming the well-formed path is unchanged.

Review context
  • Author: shin-core
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: not available
  • Official Gittensor activity: 64 PR(s), 0 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 added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.66%. Comparing base (eca3b61) to head (b82afd9).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10074      +/-   ##
==========================================
+ Coverage   79.57%   79.66%   +0.09%     
==========================================
  Files         282      283       +1     
  Lines       58664    58964     +300     
  Branches     6842     6955     +113     
==========================================
+ Hits        46682    46976     +294     
- Misses      11694    11695       +1     
- Partials      288      293       +5     
Flag Coverage Δ
backend 98.00% <100.00%> (?)

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

Files with missing lines Coverage Δ
src/github/app.ts 98.00% <100.00%> (ø)

@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 582aa25 into JSONbored:main Jul 31, 2026
8 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.

github(app): guard the NaN expiry in mintInstallationToken so an unparseable expires_at cannot disable the installation-token cache

1 participant