Skip to content

feat(npm): cap redirect following in the npm postinstall downloader #213

Closed

Description

@nc9

Part of #164. Found while reviewing #212 (the install.sh half of #165).

httpsGet in npm/scripts/postinstall.js follows redirects by recursing on res.headers.location with no depth limit:

if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
  return httpsGet(res.headers.location).then(resolve).catch(reject);
}

install.sh now caps its chain at 3 hops (#165). This path has no cap at all, so a server that returns a redirect loop keeps the postinstall recursing until the process runs out of stack or the 120s socket timeout fires on some hop, whichever lands first. Real release assets take exactly one hop.

The scheme half of #165 is already covered here by accident rather than by intent: the function is hardcoded to https.get, which throws ERR_INVALID_PROTOCOL on an http: location. Worth making that explicit at the same time so it cannot regress into a generic get.

There is no TLS-version floor either, but Node negotiates TLS 1.2+ by default on every version the package supports, so that half needs nothing.

Acceptance Criteria

  • httpsGet follows at most 3 redirects and rejects with a clear error beyond that
  • a redirect to a non-https location is rejected explicitly, not by relying on https.get throwing
  • a relative Location header still resolves correctly against the current URL
  • npm/scripts/postinstall.test.ts covers the cap, the non-https rejection, and a single normal redirect

Activity

  1. added
    enhancement
    security
    P3
    size: XS
    on Sep 23, 2026
  2. nc9 commented on Oct 11, 2026

    @nc9
    ContributorAuthor

    Checked against current main (ad95fb2): STILL TRUE. httpsGet in npm/scripts/postinstall.js:39-44 still recurses on res.headers.location with no depth limit and no explicit scheme check.

    One addition for criterion 3: a relative Location does not resolve today either. The raw header value goes straight to https.get, and a path-only string like /foo is not a valid URL to it. Criterion 3 therefore needs new code, not just a test. Resolving with new URL(location, url) before the scheme check covers both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3enhancementsecuritysize: XS

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions