Skip to content
Merged
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
3 changes: 0 additions & 3 deletions .ado/build-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ extends:
value: --sanitize-handles
${{ else }}:
value: --no-sanitize-handles
# Use the anonymous PackageFeedProxy for npm restores.
- name: NPM_CONFIG_REGISTRY
value: https://packagefeedproxy.microsoft.io/npm/
- name: NPM_CONFIG_CACHE
value: $(Agent.TempDirectory)/.npmCache
# Fail instead of letting npx silently fetch a networked tool.
Expand Down
1 change: 1 addition & 0 deletions .ado/ci-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ variables:
- name: tags
value: production,externalfacing
- template: codeql-variables.yml
- template: npm-variables.yml

extends:
template: build-template.yml@self
Expand Down
12 changes: 12 additions & 0 deletions .ado/npm-variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# Package registry variables for the npm and Yarn restores in the pipelines.
#

variables:
# Route npm and Yarn through the anonymous PackageFeedProxy. The committed
# .npmrc and .yarnrc.yml files name no registry because Dependabot reads them
# and cannot reach the proxy. See "Package feeds" in .ado/scripts/README.md.
- name: NPM_CONFIG_REGISTRY
value: https://packagefeedproxy.microsoft.io/npm/
- name: YARN_NPM_REGISTRY_SERVER
value: https://packagefeedproxy.microsoft.io/npm/
1 change: 1 addition & 0 deletions .ado/pr-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ schedules:

variables:
- template: codeql-variables.yml
- template: npm-variables.yml

extends:
template: build-template.yml@self
Expand Down
4 changes: 2 additions & 2 deletions .ado/scripts/.npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# PackageFeedProxy supports anonymous restores; do not add auth here.
registry=https://packagefeedproxy.microsoft.io/npm/
# Do not add a registry or credentials. Dependabot reads this file and cannot
# reach PackageFeedProxy; see "Package feeds" in .ado/scripts/README.md.
omit-lockfile-registry-resolved=true
45 changes: 32 additions & 13 deletions .ado/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ We use **Prettier** and **ESLint** to maintain consistent code style and catch p

### Setup

From this directory (`.ado/scripts`), install the dependencies:
From this directory (`.ado/scripts`), install the dependencies after configuring
the [package feed](#package-feeds):

```bash
npm ci
Expand Down Expand Up @@ -58,32 +59,50 @@ From the repository root, validate package sources with the supported developer

## Package feeds

Local and CI npm/Yarn restores use the anonymous Microsoft PackageFeedProxy at
npm and Yarn restores use the anonymous Microsoft PackageFeedProxy at
`https://packagefeedproxy.microsoft.io/npm/`. Do not add credentials or a direct
public npm registry to repository configuration. NuGet restores and publishing
keep their existing feeds.

Each npm project's `.npmrc` selects the proxy, so ordinary `npm ci` and
`npm install` commands need no extra flags or environment variables. Yarn uses
the root `.yarnrc.yml` and the committed Yarn 4.13.0 release. CI also sets
`NPM_CONFIG_REGISTRY` to the same proxy.
The committed `.npmrc` and `.yarnrc.yml` files must not set a registry.
Dependabot reads them but cannot reach PackageFeedProxy, so a committed proxy
registry fails every update with "egress not allowlisted". Restores select the
proxy through their environment instead:

Dependabot keeps its existing update configuration. Do not add anonymous
- CI sets `NPM_CONFIG_REGISTRY` and `YARN_NPM_REGISTRY_SERVER` for all jobs
(`.ado/npm-variables.yml`).
- `.\dev` sets the same variables unless they are already defined.
- The Node-API test build passes the `HERMES_NPM_REGISTRY` CMake variable to
`npm ci`. It defaults to the proxy; to use your npm configuration instead,
set it to an empty value in the build tree's `CMakeCache.txt`.

For other npm and Yarn commands, configure the proxy once per user, from the
repository root:

```powershell
npm config set registry https://packagefeedproxy.microsoft.io/npm/ --location=user
node .yarn/releases/yarn-4.13.0.cjs config set --home npmRegistryServer https://packagefeedproxy.microsoft.io/npm/
```

Setting the two environment variables above works as well.

Dependabot resolves updates from the public npm registry. Do not add
`npm-registry` declarations to `dependabot.yml`: GitHub requires credentials for
those entries. Keep lockfiles registry-independent rather than pinning a
download host for Dependabot or other package-manager clients.
those entries.

Both npm and Yarn lockfiles must remain free of package download URLs so local
restores and Dependabot are not tied to a recorded download host.
npm and Yarn lockfiles must remain free of package download URLs, so the same
lockfile restores from both the public registry and the proxy.
npm's `omit-lockfile-registry-resolved=true` prevents registry URLs. Yarn can
record an encoded `__archiveUrl` when resolving through the proxy; remove that
binding before committing, without changing versions or checksums, and rerun
`yarn install --immutable` and `.\dev npm-source-lint`. The lint rejects these
bindings, including those pointing at the former ADO feed or `ms-feed-*` hosts.
bindings, committed registries, and committed credentials.

Keep Yarn's `npmMinimalAgeGate: "8d"` and Dependabot's
`cooldown.default-days: 8` aligned: the proxy has a roughly seven-day security
cooldown, plus a one-day margin in this repository.
cooldown, plus a one-day margin in this repository. Dependabot applies the
cooldown only to version updates, so a security update can propose a version
that the proxy does not serve yet.

## Configuration

Expand Down
149 changes: 107 additions & 42 deletions .ado/scripts/npm-source-lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
lintRepository,
} from "./npm-source-lint.ts";

const APPROVED_REGISTRY = "https://packagefeedproxy.microsoft.io/npm/";
const PACKAGE_FEED_PROXY = "https://packagefeedproxy.microsoft.io/npm/";
// The lint rejects the registry key whatever its value.
const OTHER_REGISTRIES = [
["PackageFeedProxy", PACKAGE_FEED_PROXY],
["the public npm registry", "https://registry.npmjs.org/"],
] as const;

function dependabot(...directories: string[]): string {
return `version: 2
Expand All @@ -30,7 +35,7 @@ ${directories.map((directory) => ` - "${directory}"`).join("\n")}
function validNpmFixture(): Record<string, string> {
return {
".github/dependabot.yml": dependabot("/project"),
"project/.npmrc": `registry=${APPROVED_REGISTRY}\nomit-lockfile-registry-resolved=true\n`,
"project/.npmrc": "omit-lockfile-registry-resolved=true\n",
"project/package.json": JSON.stringify({
name: "npm-fixture",
version: "1.0.0",
Expand Down Expand Up @@ -59,7 +64,8 @@ function validNpmFixture(): Record<string, string> {
function validYarnFixture(): Record<string, string> {
return {
".github/dependabot.yml": dependabot("/project"),
".yarnrc.yml": `nodeLinker: node-modules\nnpmRegistryServer: "${APPROVED_REGISTRY}"\nyarnPath: .yarn/releases/yarn-4.13.0.cjs\n`,
".yarnrc.yml":
"nodeLinker: node-modules\nyarnPath: .yarn/releases/yarn-4.13.0.cjs\n",
".yarn/releases/yarn-4.13.0.cjs": "module.exports = {};\n",
"project/package.json": JSON.stringify({
name: "yarn-fixture",
Expand Down Expand Up @@ -206,45 +212,120 @@ test("accepts a package-lock local source inside the repository", () => {
assertValid(files);
});

test("requires committed npm registry configuration", () => {
test("requires committed npm configuration", () => {
assertViolation(
removeFile(validNpmFixture(), "project/.npmrc"),
/committed npm registry configuration is missing/,
/committed npm configuration with omit-lockfile-registry-resolved=true is missing/,
);
});

test("rejects missing omit-lockfile-registry-resolved", () => {
const files = validNpmFixture();
files["project/.npmrc"] = `registry=${APPROVED_REGISTRY}\n`;
files["project/.npmrc"] = "save-exact=true\n";
assertViolation(files, /omit-lockfile-registry-resolved/);
});

test("rejects a missing npm registry setting", () => {
for (const [name, registry] of OTHER_REGISTRIES) {
test(`rejects a committed npm registry on ${name}`, () => {
const files = validNpmFixture();
files["project/.npmrc"] += `registry=${registry}\n`;
assertViolation(
files,
/\.npmrc:\d+: registry is "[^"]+"; required no committed registry/,
);
});

test(`rejects a committed scoped npm registry on ${name}`, () => {
const files = validNpmFixture();
files["project/.npmrc"] += `@example:registry=${registry}\n`;
assertViolation(
files,
/\.npmrc:\d+: @example:registry is "[^"]+"; required no committed registry/,
);
});

test(`rejects a committed Yarn registry on ${name}`, () => {
const files = validYarnFixture();
files[".yarnrc.yml"] += `npmRegistryServer: "${registry}"\n`;
assertViolation(
files,
/\.yarnrc\.yml:\d+: npmRegistryServer is "[^"]+"; required no committed registry/,
);
});

test(`rejects a committed scoped Yarn registry on ${name}`, () => {
const files = validYarnFixture();
files[".yarnrc.yml"] +=
`npmScopes:\n example:\n npmRegistryServer: "${registry}"\n`;
assertViolation(
files,
/\.yarnrc\.yml:\d+: npmScopes\.example\.npmRegistryServer is "[^"]+"; required no committed registry/,
);
});
}

test("rejects a registry in a .npmrc that no package project owns", () => {
const files = validNpmFixture();
files["project/.npmrc"] = "omit-lockfile-registry-resolved=true\n";
assertViolation(files, /registry.*required.*packagefeedproxy/);
files[".npmrc"] = `registry=${PACKAGE_FEED_PROXY}\n`;
assertViolation(
files,
/(^|\n)\.npmrc:1: registry is "[^"]+"; required no committed registry/,
);
});

test("rejects an unapproved npm registry", () => {
const files = validNpmFixture();
files["project/.npmrc"] = files["project/.npmrc"].replace(
APPROVED_REGISTRY,
"https://packages.example.com/npm/",
test("rejects a registry in a .yarnrc.yml that no Yarn project uses", () => {
const files = validYarnFixture();
files["tools/.yarnrc.yml"] = `npmRegistryServer: "${PACKAGE_FEED_PROXY}"\n`;
assertViolation(
files,
/tools\/\.yarnrc\.yml:1: npmRegistryServer is "[^"]+"; required no committed registry/,
);
assertViolation(files, /registry.*required.*packagefeedproxy/);
});

test("accepts a scoped npm registry on the approved proxy", () => {
const files = validNpmFixture();
files["project/.npmrc"] += `@example:registry=${APPROVED_REGISTRY}\n`;
assertValid(files);
test("reports a registry in a shared .yarnrc.yml once", () => {
const files = validYarnFixture();
files[".github/dependabot.yml"] = dependabot("/project", "/other");
files["other/package.json"] = files["project/package.json"];
files["other/yarn.lock"] = files["project/yarn.lock"];
files[".yarnrc.yml"] += `npmRegistryServer: "${PACKAGE_FEED_PROXY}"\n`;
const root = createFixture(files);
try {
const { errors } = lintRepository(root, { emit: false });
assert.equal(
errors.filter((error) => error.includes("npmRegistryServer is")).length,
1,
);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});

test("reports an invalid shared .yarnrc.yml once", () => {
const files = validYarnFixture();
files[".github/dependabot.yml"] = dependabot("/project", "/other");
files["other/package.json"] = files["project/package.json"];
files["other/yarn.lock"] = files["project/yarn.lock"];
files[".yarnrc.yml"] = "nodeLinker: [node-modules\n";
const root = createFixture(files);
try {
const { errors } = lintRepository(root, { emit: false });
assert.equal(
errors.filter((error) => error.startsWith(".yarnrc.yml: invalid YAML"))
.length,
1,
);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});

test("rejects an npm scoped registry override", () => {
test("rejects credentials in a .npmrc that no package project owns", () => {
const files = validNpmFixture();
files["project/.npmrc"] +=
"@example:registry=https://packages.example.com/npm/\n";
assertViolation(files, /@example:registry/);
files[".npmrc"] = "//example.test/:_authToken=secret\n";
assertViolation(
files,
/(^|\n)\.npmrc:1: committed npm credentials are forbidden/,
);
});

test("rejects committed npm credentials", () => {
Expand Down Expand Up @@ -279,7 +360,7 @@ for (const [name, registry] of [
"the old ADO feed",
"https://pkgs.dev.azure.com/ms/react-native/_packaging/react-native-public/npm/registry/",
],
["PackageFeedProxy", APPROVED_REGISTRY],
["PackageFeedProxy", PACKAGE_FEED_PROXY],
[
"an ms-feed host",
"https://ms-feed-25.pkgs.visualstudio.com/1es-public/_packaging/npm-public/npm/registry/",
Expand Down Expand Up @@ -318,37 +399,21 @@ test("rejects a Yarn archive URL recorded only in a descriptor", () => {
const files = validYarnFixture();
files["project/yarn.lock"] = files["project/yarn.lock"].replace(
'"example@npm:^1.0.0":',
`"example@npm:^1.0.0::__archiveUrl=${encodeURIComponent(`${APPROVED_REGISTRY}example/-/example-1.0.0.tgz`)}":`,
`"example@npm:^1.0.0::__archiveUrl=${encodeURIComponent(`${PACKAGE_FEED_PROXY}example/-/example-1.0.0.tgz`)}":`,
);
assertViolation(files, /__archiveUrl\) are forbidden/);
});

test("rejects a Yarn archive URL encoded inside a patch locator", () => {
const files = validYarnFixture();
const locator = `example@npm:1.0.0::__archiveUrl=${encodeURIComponent(`${APPROVED_REGISTRY}example/-/example-1.0.0.tgz`)}`;
const locator = `example@npm:1.0.0::__archiveUrl=${encodeURIComponent(`${PACKAGE_FEED_PROXY}example/-/example-1.0.0.tgz`)}`;
files["project/yarn.lock"] = files["project/yarn.lock"].replace(
'resolution: "example@npm:1.0.0"',
`resolution: "example@patch:${encodeURIComponent(locator)}#./example.patch"`,
);
assertViolation(files, /__archiveUrl\) are forbidden/);
});

test("rejects the wrong Yarn registry", () => {
const files = validYarnFixture();
files[".yarnrc.yml"] = files[".yarnrc.yml"].replace(
APPROVED_REGISTRY,
"https://registry.yarnpkg.com/",
);
assertViolation(files, /npmRegistryServer|registry\.yarnpkg\.com/);
});

test("rejects a Yarn scoped registry override", () => {
const files = validYarnFixture();
files[".yarnrc.yml"] +=
`npmScopes:\n example:\n npmRegistryServer: "https://packages.example.com/npm/"\n`;
assertViolation(files, /npmScopes\.example\.npmRegistryServer/);
});

test("rejects committed Yarn credentials", () => {
const files = validYarnFixture();
files[".yarnrc.yml"] += "npmAuthToken: secret\n";
Expand Down
Loading
Loading