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
22 changes: 22 additions & 0 deletions packages/nodejs-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,28 @@ await hyphen.env.load('production', {
});
```

The service does not add a reserved name to `process.env`. Node.js, OpenSSL, the
dynamic loader, and the command shell read these names to load code, to find
programs and modules, or to trust certificates. The comparison ignores the case
of the name. These names are reserved:

- `NODE_OPTIONS`, `NODE_PATH`, `NODE_REPL_EXTERNAL_MODULE`, and
`NODE_COMPILE_CACHE`
- `NODE_EXTRA_CA_CERTS` and `NODE_TLS_REJECT_UNAUTHORIZED`
- `OPENSSL_CONF`, `OPENSSL_ENGINES`, `OPENSSL_MODULES`, `SSL_CERT_DIR`, and
`SSL_CERT_FILE`
- `PATH`, `PATHEXT`, `COMSPEC`, and `BASH_ENV`
- Each name that starts with `LD_` or `DYLD_`
- `HYPHEN_API_KEY`

The service sends one `warn` event that lists the names that it did not add. The
event does not contain the values. The returned object contains the reserved
names and their values. Other names, for example `NODE_ENV`, are not reserved.

The list contains only the names that these programs read. A different
program can read a different name. Give write access to your ENV data only to
persons who can change the code of the application.

The service reads missing configuration from `HYPHEN_API_KEY`,
`HYPHEN_ORGANIZATION_ID`, `HYPHEN_PROJECT_ID`, and
`HYPHEN_APPLICATION_ID`, with `HYPHEN_APP_ID` supported as a compatibility
Expand Down
47 changes: 46 additions & 1 deletion packages/nodejs-sdk/src/env-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ const environmentNamePattern = /^[a-z0-9_-]+$/;
const base64UrlPattern = /^[A-Za-z0-9_-]+={0,2}$/;
const utf8Decoder = new TextDecoder("utf-8", { fatal: true });

/**
* `load` does not write these names to `process.env`. The Node.js runtime,
* OpenSSL, and the command shell read them to load code, to find programs and
* modules, or to trust certificates. `HYPHEN_API_KEY` is the SDK credential.
*/
const reservedNames = new Set([
"BASH_ENV",
"COMSPEC",
"HYPHEN_API_KEY",
"NODE_COMPILE_CACHE",
"NODE_EXTRA_CA_CERTS",
"NODE_OPTIONS",
"NODE_PATH",
"NODE_REPL_EXTERNAL_MODULE",
"NODE_TLS_REJECT_UNAUTHORIZED",
"OPENSSL_CONF",
"OPENSSL_ENGINES",
"OPENSSL_MODULES",
"PATH",
"PATHEXT",
"SSL_CERT_DIR",
"SSL_CERT_FILE",
]);

/** The dynamic loader reads each name that starts with one of these prefixes. */
const reservedNamePrefixes = ["DYLD_", "LD_"];

class SafeEnvError extends Error {}

export class Env extends BaseService {
Expand Down Expand Up @@ -103,11 +130,20 @@ export class Env extends BaseService {
);
}

const override = options?.override ?? true;
const skippedNames: string[] = [];
for (const [name, value] of entries) {
if ((options?.override ?? true) || process.env[name] === undefined) {
if (isReservedName(name)) {
skippedNames.push(name);
} else if (override || process.env[name] === undefined) {
process.env[name] = value;
}
}
if (skippedNames.length > 0) {
this.warn(
`Hyphen ENV did not add these reserved names to process.env: ${skippedNames.join(", ")}.`,
);
}

return variables;
}
Expand Down Expand Up @@ -204,6 +240,15 @@ function normalizeEnvironmentName(environment: string): string {
return name;
}

function isReservedName(name: string): boolean {
// Windows ignores the case of a variable name.
const upperName = name.toUpperCase();
return (
reservedNames.has(upperName) ||
reservedNamePrefixes.some((prefix) => upperName.startsWith(prefix))
);
}

function validateKey(key: {
secretKeyId: unknown;
secretKey: unknown;
Expand Down
85 changes: 85 additions & 0 deletions packages/nodejs-sdk/test/env-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const testVariableNames = [
"NAMED_ONLY",
"NEW_VALUE",
"ONLY_NAMED",
"ORDINARY_NAME",
"QUOTED",
"SETTER_KEY",
"SHARED",
Expand Down Expand Up @@ -287,6 +288,90 @@ describe("Env", () => {
expect(process.env.SHARED).toBe("remote");
});

describe("reserved names", () => {
test.each([
"BASH_ENV",
"COMSPEC",
"DYLD_INSERT_LIBRARIES",
"HYPHEN_API_KEY",
"LD_PRELOAD",
"NODE_COMPILE_CACHE",
"NODE_EXTRA_CA_CERTS",
"NODE_OPTIONS",
"NODE_PATH",
"NODE_REPL_EXTERNAL_MODULE",
"NODE_TLS_REJECT_UNAUTHORIZED",
"OPENSSL_CONF",
"OPENSSL_ENGINES",
"OPENSSL_MODULES",
"PATH",
"PATHEXT",
"SSL_CERT_DIR",
"SSL_CERT_FILE",
"ld_library_path",
"node_options",
"Path",
])("does not write the reserved name %s to process.env", async (name) => {
const before = process.env[name];
const service = createService({ decryptionKey });
vi.spyOn(service.log, "warn").mockImplementation(() => undefined);
vi.spyOn(service, "get").mockResolvedValue(
mockEnvResponse(`${name}=remote\nORDINARY_NAME=written\n`),
);

await expect(service.load()).resolves.toEqual({
[name]: "remote",
ORDINARY_NAME: "written",
});
expect(process.env[name]).toBe(before);
expect(process.env.ORDINARY_NAME).toBe("written");

await service.load("default", { override: false });
expect(process.env[name]).toBe(before);
});

test.each(["LDAP_URL", "NODE_CONFIG", "NODE_ENV", "PATHNAME"])(
"writes the name %s, which is not reserved",
async (name) => {
const service = createService({ decryptionKey });
const warnListener = vi.fn();
service.on("warn", warnListener);
vi.spyOn(service, "get").mockResolvedValue(
mockEnvResponse(`${name}=remote\n`),
);

await service.load();

expect(process.env[name]).toBe("remote");
expect(warnListener).not.toHaveBeenCalled();
},
);

test("reports each skipped name and no value", async () => {
const service = createService({ decryptionKey });
const logSpy = vi
.spyOn(service.log, "warn")
.mockImplementation(() => undefined);
const warnListener = vi.fn();
service.on("warn", warnListener);
vi.spyOn(service, "get").mockResolvedValue(
mockEnvResponse(
"NODE_OPTIONS=--first-secret\nORDINARY_NAME=written\nLD_PRELOAD=second-secret\n",
),
);

await service.load();

expect(warnListener).toHaveBeenCalledOnce();
const [message] = warnListener.mock.calls[0];
expect(message).toContain("NODE_OPTIONS");
expect(message).toContain("LD_PRELOAD");
expect(message).not.toContain("ORDINARY_NAME");
expect(message).not.toContain("secret");
expect(logSpy).toHaveBeenCalledExactlyOnceWith(message);
});
});

test("uses an explicit key without requiring a project or requesting the key store", async () => {
const service = new Env({
apiKey,
Expand Down
Loading