Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions .github/actions/use-npmrc/action.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
name: 'Use .npmrc'
description: 'Writes the given .npmrc content to ~/.npmrc and warns about lockfile entries outside the cplace npm proxy'
description: 'Writes ~/.npmrc from JFROG_TOKEN (or the deprecated DOT_NPMRC) and warns about lockfile entries outside the cplace npm proxy'
inputs:
jfrog-token:
description: 'JFrog token (the JFROG_TOKEN secret); authenticates installs against the cplace-npm virtual'
required: false
default: ''
dot-npmrc:
description: 'Content of the .npmrc file'
required: true
description: 'Deprecated: content of a complete .npmrc; used only when jfrog-token is empty'
required: false
default: ''
runs:
using: 'composite'
steps:
- name: Write ~/.npmrc
shell: bash
env:
JFROG_TOKEN: ${{ inputs.jfrog-token }}
DOT_NPMRC: ${{ inputs.dot-npmrc }}
run: |
echo "$DOT_NPMRC" > ~/.npmrc
# PFM-ISSUE-34453 mitigation. npm's default `replace-registry-host=npmjs`
# rewrites a lockfile's registry.npmjs.org URLs onto the configured
# registry. npm 10.2.4 does that correctly and the entry resolves through
# the proxy; npm 11.3.0 DROPS that registry's path prefix, producing an
# E404 masked as *** because the prefix is the JFROG_URL secret. Measured
# 2026-08-14 on a runner and locally with the same one-package fixture.
#
# `never` makes npm fetch each `resolved` URL verbatim, which survives
# both versions - at a cost: an entry still on npmjs is then fetched FROM
# npmjs, outside the proxy. This line buys npm-11 compatibility and
# spends proxy routing; normalizing the lockfile buys both.
#
# This is a no-op on a lockfile that already resolves entirely through
# the proxy, so it is safe to leave in place, and it is removable once
# the warning below stops firing anywhere.
echo 'replace-registry-host=never' >> ~/.npmrc
# Generation, precedence and the PFM-ISSUE-34453 mitigation live in the
# script, where bats covers them.
run: bash "$GITHUB_ACTION_PATH/write-npmrc.sh"

- name: Warn on lockfile entries outside the cplace npm proxy
shell: bash
# Advisory only - never fails the build. The warnings are the inventory of
# lockfiles that still need normalizing; when none report, the mitigation
# above can be dropped.
# lockfiles that still need normalizing; when none report, the mitigation in
# write-npmrc.sh can be dropped.
#
# Invoked as `bash "<path>" || true`, not as a bare path. The script's own
# "every precondition is a silent success" contract cannot cover NOT BEING
# REACHABLE - a missing exec bit after checkout, a path containing spaces,
# or a runner where action_path is a backslashed Windows path. That is an
# exit 127 from the shell rather than from the script, and it would fail
# the consumer's job before `npm ci` in all seven workflows using this
# the consumer's job before `npm ci` in every workflow using this
# action. `continue-on-error:` is not honoured on composite steps, so
# `|| true` is the mechanism that actually works here.
run: bash "$GITHUB_ACTION_PATH/../../../tools/scripts/lockfile/warn-foreign-registry.sh" || true
136 changes: 136 additions & 0 deletions .github/actions/use-npmrc/write-npmrc.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bats
#
# Behavioural tests for write-npmrc.sh, the generator behind the use-npmrc
# composite. HOME is redirected so that the tests never touch the real ~/.npmrc.
#
# Run with: bats .github/actions/use-npmrc/

setup() {
WRITE="${BATS_TEST_DIRNAME}/write-npmrc.sh"
export HOME="${BATS_TEST_TMPDIR}"
NPMRC="${HOME}/.npmrc"
unset JFROG_TOKEN DOT_NPMRC
}

@test "writes registry, auth token and mitigation when only JFROG_TOKEN is set" {
export JFROG_TOKEN='test-token'

run "${WRITE}"

[ "${status}" -eq 0 ]
[ "$(cat "${NPMRC}")" = "registry=https://cplace.jfrog.io/artifactory/api/npm/cplace-npm/
//cplace.jfrog.io/artifactory/api/npm/cplace-npm/:_authToken=test-token
replace-registry-host=never" ]
}

@test "prints neither the token nor an annotation when only JFROG_TOKEN is set" {
export JFROG_TOKEN='secret-token-value'

run "${WRITE}"

[ "${status}" -eq 0 ]
[[ "${output}" != *'secret-token-value'* ]]
[[ "${output}" != *'::'* ]]
}

@test "makes the written file readable by its owner only" {
[[ "$(uname -s)" != MINGW* && "$(uname -s)" != MSYS* ]] || skip 'NTFS has no Unix file modes'
export JFROG_TOKEN='test-token'

run "${WRITE}"

[ "${status}" -eq 0 ]
[ "$(stat -c %a "${NPMRC}")" = '600' ]
}

@test "uses JFROG_NPM_PROXY from lib.sh as the registry" {
# shellcheck source=tools/scripts/lockfile/lib.sh
source "${BATS_TEST_DIRNAME}/../../../tools/scripts/lockfile/lib.sh"
export JFROG_TOKEN='test-token'

run "${WRITE}"

[ "${status}" -eq 0 ]
[ "$(head -n 1 "${NPMRC}")" = "registry=${JFROG_NPM_PROXY}" ]
}

@test "prefers JFROG_TOKEN over DOT_NPMRC and says so in a notice" {
export JFROG_TOKEN='test-token'
export DOT_NPMRC='registry=https://legacy.example/npm/'

run "${WRITE}"

[ "${status}" -eq 0 ]
grep -q '_authToken=test-token' "${NPMRC}"
[[ "$(cat "${NPMRC}")" != *'legacy.example'* ]]
[[ "${output}" == *'::notice::'* ]]
[[ "${output}" != *'::warning::'* ]]
}

@test "writes DOT_NPMRC verbatim plus the mitigation, with a deprecation warning, when only it is set" {
export DOT_NPMRC='registry=https://legacy.example/npm/
//legacy.example/npm/:_auth=abc'

run "${WRITE}"

[ "${status}" -eq 0 ]
[ "$(cat "${NPMRC}")" = "registry=https://legacy.example/npm/
//legacy.example/npm/:_auth=abc
replace-registry-host=never" ]
[[ "${output}" == *'::warning::'* ]]
[[ "${output}" == *'DOT_NPMRC is deprecated'* ]]
}

@test "fails without writing a file when neither secret is set" {
run "${WRITE}"

[ "${status}" -ne 0 ]
[[ "${output}" == *'::error::'* ]]
[[ "${output}" == *'No npm credentials'* ]]
[ ! -e "${NPMRC}" ]
}

@test "treats empty strings as not set, as a composite passes an omitted input" {
export JFROG_TOKEN='' DOT_NPMRC=''

run "${WRITE}"

[ "${status}" -ne 0 ]
[[ "${output}" == *'::error::'* ]]
[[ "${output}" == *'No npm credentials'* ]]
[ ! -e "${NPMRC}" ]
}

@test "rejects a token containing a space, without printing it or writing a file" {
export JFROG_TOKEN='secret-part other-part'

run "${WRITE}"

[ "${status}" -ne 0 ]
[[ "${output}" == *'::error::'* ]]
[[ "${output}" != *'secret-part'* ]]
[ ! -e "${NPMRC}" ]
}

@test "rejects a token containing a newline, without printing it or writing a file" {
export JFROG_TOKEN='secret-part
other-part'

run "${WRITE}"

[ "${status}" -ne 0 ]
[[ "${output}" == *'::error::'* ]]
[[ "${output}" != *'secret-part'* ]]
[ ! -e "${NPMRC}" ]
}

@test "replaces an existing ~/.npmrc instead of appending to it" {
printf 'stale=content\n' >"${NPMRC}"
export JFROG_TOKEN='test-token'

run "${WRITE}"

[ "${status}" -eq 0 ]
[[ "$(cat "${NPMRC}")" != *'stale=content'* ]]
[ "$(wc -l <"${NPMRC}")" -eq 3 ]
}
70 changes: 70 additions & 0 deletions .github/actions/use-npmrc/write-npmrc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
#
# Writes ~/.npmrc for npm installs. Run by the use-npmrc composite.
#
# Inputs (environment only, never arguments, so that they never appear in a
# process listing): JFROG_TOKEN, DOT_NPMRC. An empty value counts as not set,
# because a composite receives an input that was not passed as ''.
#
# JFROG_TOKEN set -> generated file: the cplace-npm virtual as registry and the
# token as its _authToken. DOT_NPMRC, if also set, is
# ignored with a ::notice::.
# only DOT_NPMRC -> DEPRECATED: written verbatim, with a ::warning::.
# neither -> ::error::, exit 1, no file written.
#
# The token is written literally, never printed. See PFM-ISSUE-34294.

set -euo pipefail

LIB="$(dirname "${BASH_SOURCE[0]}")/../../../tools/scripts/lockfile/lib.sh"
readonly LIB
# shellcheck source=tools/scripts/lockfile/lib.sh
source "${LIB}"

main() {
local npmrc="${HOME}/.npmrc"
local token="${JFROG_TOKEN:-}" dot_npmrc="${DOT_NPMRC:-}"

umask 077
if [[ -n "${token}" ]]; then
if [[ "${token}" =~ [[:space:]] ]]; then
echo '::error::JFROG_TOKEN contains whitespace; check the repository secret.'
return 1
fi
printf 'registry=%s\n%s:_authToken=%s\n' \
"${JFROG_NPM_PROXY}" "${JFROG_NPM_PROXY#https:}" "${token}" >"${npmrc}"
if [[ -n "${dot_npmrc}" ]]; then
echo '::notice::DOT_NPMRC is passed but ignored, because JFROG_TOKEN is set. Remove the DOT_NPMRC line from this caller.'
fi
elif [[ -n "${dot_npmrc}" ]]; then
printf '%s\n' "${dot_npmrc}" >"${npmrc}"
echo '::warning::DOT_NPMRC is deprecated. Pass JFROG_TOKEN instead, see "JFrog and npm secrets" in the github-actions README.'
else
echo '::error::No npm credentials: pass the JFROG_TOKEN secret (see "JFrog and npm secrets" in the github-actions README).'
return 1
fi

# PFM-ISSUE-34453 mitigation. npm's default `replace-registry-host=npmjs`
# rewrites a lockfile's registry.npmjs.org URLs onto the configured
# registry. npm 10.2.4 does that correctly and the entry resolves through
# the proxy; npm 11.3.0 DROPS that registry's path prefix (the
# `/artifactory/api/npm/cplace-npm/` part of the `registry=` URL), producing
# an E404. In the generated file that URL is committed, not secret; in the
# DOT_NPMRC fallback it comes from the secret, so the E404 is masked as ***.
# Measured 2026-08-14 on a runner and locally with the same one-package
# fixture.
#
# `never` makes npm fetch each `resolved` URL verbatim, which survives
# both versions - at a cost: an entry still on npmjs is then fetched FROM
# npmjs, outside the proxy. This line buys npm-11 compatibility and
# spends proxy routing; normalizing the lockfile buys both.
#
# This is a no-op on a lockfile that already resolves entirely through
# the proxy, so it is safe to leave in place, and it is removable once
# the lockfile advisory in use-npmrc stops firing anywhere.
echo 'replace-registry-host=never' >>"${npmrc}"
# umask covers a new file only; a pre-existing ~/.npmrc keeps its mode.
chmod 600 "${npmrc}"
}

main "$@"
1 change: 0 additions & 1 deletion .github/workflow-templates/fe/fe-cleanup-snapshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ jobs:
JFROG_TOKEN: ${{ secrets.JFROG_TOKEN }}
JFROG_URL: ${{ secrets.JFROG_URL }}
JFROG_USER: ${{ secrets.JFROG_USER }}
DOT_NPMRC: ${{ secrets.DOT_NPMRC }}
2 changes: 1 addition & 1 deletion .github/workflow-templates/fe/fe-licenses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
FOSS_DIST: dist
CUSTOM_LICENSES_JSON: custom-licenses/custom-licenses.json
secrets:
DOT_NPMRC: ${{ secrets.DOT_NPMRC }}
JFROG_TOKEN: ${{ secrets.JFROG_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflow-templates/fe/fe-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
GHA_REF: ''
secrets:
DOT_NPMRC: ${{ secrets.DOT_NPMRC }}
JFROG_TOKEN: ${{ secrets.JFROG_TOKEN }}

# This job is only needed in case you are using Percy
e2e-tests:
Expand Down
1 change: 0 additions & 1 deletion .github/workflow-templates/fe/fe-pr-close.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ jobs:
JFROG_TOKEN: ${{ secrets.JFROG_TOKEN }}
JFROG_URL: ${{ secrets.JFROG_URL }}
JFROG_USER: ${{ secrets.JFROG_USER }}
DOT_NPMRC: ${{ secrets.DOT_NPMRC }}
1 change: 0 additions & 1 deletion .github/workflow-templates/fe/fe-pr-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ jobs:
JFROG_TOKEN: ${{ secrets.JFROG_TOKEN }}
JFROG_URL: ${{ secrets.JFROG_URL }}
JFROG_USER: ${{ secrets.JFROG_USER }}
DOT_NPMRC: ${{ secrets.DOT_NPMRC }}
2 changes: 1 addition & 1 deletion .github/workflow-templates/fe/fe-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
GHA_REF: ${{ github.event.pull_request.head.ref }}
secrets:
DOT_NPMRC: ${{ secrets.DOT_NPMRC }}
JFROG_TOKEN: ${{ secrets.JFROG_TOKEN }}

build:
needs: install-deps
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/fe-cleanup-snapshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ on:
required: true
JFROG_USER:
required: true
# deprecated, replaced by JFROG_TOKEN; used only when JFROG_TOKEN is not passed
DOT_NPMRC:
required: true
required: false

jobs:
build:
Expand All @@ -39,6 +40,7 @@ jobs:
- name: Use .npmrc
uses: collaborationFactory/github-actions/.github/actions/use-npmrc@master
with:
jfrog-token: ${{ secrets.JFROG_TOKEN }}
dot-npmrc: ${{ secrets.DOT_NPMRC }}

- name: Cleanup Snapshot Artifacts
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/fe-install-deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ on:
type: string
required: true
secrets:
JFROG_TOKEN:
required: false
# deprecated, replaced by JFROG_TOKEN; used only when JFROG_TOKEN is not passed
DOT_NPMRC:
required: true
required: false

jobs:
install-deps:
Expand All @@ -32,6 +35,7 @@ jobs:
- name: Use .npmrc
uses: collaborationFactory/github-actions/.github/actions/use-npmrc@master
with:
jfrog-token: ${{ secrets.JFROG_TOKEN }}
dot-npmrc: ${{ secrets.DOT_NPMRC }}

- name: Install modules
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/fe-licenses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ on:
required: false
type: string
secrets:
JFROG_TOKEN:
required: false
# deprecated, replaced by JFROG_TOKEN; used only when JFROG_TOKEN is not passed
DOT_NPMRC:
required: true
required: false

jobs:
check-licenses:
Expand All @@ -38,6 +41,7 @@ jobs:
- name: Use .npmrc
uses: collaborationFactory/github-actions/.github/actions/use-npmrc@master
with:
jfrog-token: ${{ secrets.JFROG_TOKEN }}
dot-npmrc: ${{ secrets.DOT_NPMRC }}

- name: Install modules
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/fe-pr-close.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ on:
required: true
JFROG_USER:
required: true
# deprecated, replaced by JFROG_TOKEN; used only when JFROG_TOKEN is not passed
DOT_NPMRC:
required: true
required: false

jobs:
remove-artifacts:
Expand All @@ -41,6 +42,7 @@ jobs:
- name: Use .npmrc
uses: collaborationFactory/github-actions/.github/actions/use-npmrc@master
with:
jfrog-token: ${{ secrets.JFROG_TOKEN }}
dot-npmrc: ${{ secrets.DOT_NPMRC }}

- name: Install modules
Expand Down
Loading