Skip to content

Commit 7a4f268

Browse files
ac1982morluto
authored andcommitted
fix(javascript): reject invalid package exports configurations
1 parent 2b4abbf commit 7a4f268

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎src/application/javascript/JavaScriptArtifactPathResolution.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,24 @@ const packageExport = (
496496
value: unknown,
497497
moduleKind: ResolveArtifactPathInput["moduleKind"],
498498
): PackageExportOutcome => {
499+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
500+
const keys = Object.keys(value);
501+
if (
502+
keys.some((key) => key.startsWith(".")) &&
503+
keys.some((key) => !key.startsWith("."))
504+
)
505+
return {
506+
status: "rejected",
507+
limitation:
508+
"The package exports configuration mixes subpath keys and condition keys.",
509+
};
510+
}
499511
const root =
500512
typeof value === "object" && value !== null && Object.hasOwn(value, ".")
501513
? Reflect.get(value, ".")
502514
: value;
503515
const flattened = exportTargets(root, packageExportConditions(moduleKind));
504-
if (flattened.kind === "invalid")
516+
if (flattened.kind === "invalid" || flattened.kind === "invalid-config")
505517
return { status: "rejected", limitation: flattened.limitation };
506518
const first =
507519
flattened.kind === "unmatched" ? undefined : flattened.values[0];
@@ -517,7 +529,8 @@ const packageExport = (
517529
type ExportTargets =
518530
| { readonly kind: "targets"; readonly values: readonly string[] }
519531
| { readonly kind: "unmatched" }
520-
| { readonly kind: "invalid"; readonly limitation: string };
532+
| { readonly kind: "invalid"; readonly limitation: string }
533+
| { readonly kind: "invalid-config"; readonly limitation: string };
521534

522535
const invalidExportTarget = (
523536
value: unknown,
@@ -570,6 +583,8 @@ const exportTargets = (
570583
let invalid: string | null = null;
571584
for (const entry of value) {
572585
const nested = exportTargets(entry, conditions);
586+
// Node's array fallback catches invalid targets, not invalid configuration.
587+
if (nested.kind === "invalid-config") return nested;
573588
if (nested.kind === "invalid") {
574589
invalid = nested.limitation;
575590
continue;
@@ -587,6 +602,14 @@ const exportTargets = (
587602
value,
588603
"must be a relative string, object, array, or null",
589604
);
605+
for (const key of Object.keys(value)) {
606+
const numeric = Number(key);
607+
if (String(numeric) === key && numeric >= 0 && numeric < 0xffff_ffff)
608+
return {
609+
kind: "invalid-config",
610+
limitation: `The package exports configuration contains numeric condition key ${JSON.stringify(key)}.`,
611+
};
612+
}
590613
for (const [condition, target] of Object.entries(value)) {
591614
if (!conditions.has(condition)) continue;
592615
const nested = exportTargets(target, conditions);

‎tests/boundary/javascript/packageConditionalExports.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,42 @@ const expectNodeAgreement = async (
122122
return outcome;
123123
};
124124
describe("ordered package exports conditions", () => {
125+
it.each([
126+
{ ".": "./default.cjs", import: "./specific.cjs" },
127+
{ default: "./default.cjs", "./feature": "./specific.cjs" },
128+
{ "0": "./specific.cjs", default: "./default.cjs" },
129+
{ ".": { "1.5": "./specific.cjs", default: "./default.cjs" } },
130+
{ node: [{ "4294967294": null }, "./array.cjs"], default: "./default.cjs" },
131+
])(
132+
"rejects invalid exports configuration %j without fallback",
133+
async (exportsMap) => {
134+
for (const moduleKind of ["import", "require"] as const) {
135+
const { node, node_error, outcome } = await compareWithNode(
136+
exportsMap,
137+
moduleKind,
138+
);
139+
expect(node).toBeNull();
140+
expect(node_error).toBe("ERR_INVALID_PACKAGE_CONFIG");
141+
expect(outcome).toMatchObject({
142+
resolved_path: null,
143+
resolution_status: "rejected",
144+
});
145+
expect(outcome.limitations[0]).toContain("exports configuration");
146+
}
147+
},
148+
);
149+
150+
it.each([
151+
{ "01": "./specific.cjs", default: "./default.cjs" },
152+
{ "4294967295": "./specific.cjs", default: "./default.cjs" },
153+
{ browser: { "0": null }, default: "./default.cjs" },
154+
["./default.cjs", { "0": null }],
155+
{ ".": "./default.cjs", "./feature": "./specific.cjs" },
156+
])("preserves valid or unvisited conditions in %j", async (exportsMap) => {
157+
for (const moduleKind of ["import", "require"] as const)
158+
await expectNodeAgreement(exportsMap, "default.cjs", moduleKind);
159+
});
160+
125161
it.each(["import", "require"] as const)(
126162
"matches Node when default precedes %s",
127163
async (moduleKind) => {

0 commit comments

Comments
 (0)