Skip to content

Commit 7c99212

Browse files
committed
fix(isolation): reject malformed bundle roots
Preserve the validator error contract by checking manifest and schema-version types before schema dispatch. Co-authored-by: OpenCode Agent <[email protected]>
1 parent d547ce1 commit 7c99212

3 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎integrations/isolation/docs/provider-input-contract.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,19 @@ Materializers validate staged bundles without copying contract logic:
7171
python3 scripts/validate_provider_input_contract.py \
7272
--schema-dir <released-patterns>/integrations/isolation/schemas \
7373
--credential-bindings <released-patterns>/integrations/isolation/schemas/provider-credential-bindings.json \
74-
--bundle <staged-provider>/provider-input.json
74+
--bundle <staged-provider-1>/provider-input.json \
75+
--bundle <staged-provider-2>/provider-input.json
7576
```
7677

78+
**CLI invocation rules:**
79+
80+
- **Repeated `--bundle` flags in ONE invocation** perform set-wide duplicate-provider checking across all candidates. Each distinct `--bundle` invocation with a single flag resets the uniqueness set, so pass every candidate as repeated `--bundle` flags in a single command to catch duplicates.
81+
- **Zero `--bundle` flags** select repository discovery and validate the shipped fixture and canonical kit bundles. This is discovery mode, not validation of an empty external provider set; see Consumer Requirements.
82+
7783
`--schema-dir` and `--credential-bindings` are trusted control-plane inputs.
7884
They MUST come from the pinned Patterns release and backend-owned credential
7985
configuration, never from bundle contents or provider-controlled metadata. A
80-
provider controls only the staged manifest path passed with `--bundle`.
86+
provider controls only the staged manifest path(s) passed with `--bundle`.
8187

8288
## Consumer Requirements
8389

‎scripts/tests/test_provider_input_contract.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,3 +1299,76 @@ def test_fixture_normalizers_are_directly_invocable(tmp_path):
12991299
catalog = load_json(out)
13001300
validate_catalog(catalog, provider_id, str(out))
13011301
assert out.read_bytes() == (fixture_dir / "snapshot.json").read_bytes()
1302+
1303+
1304+
# ---------------------------------------------------------------------------
1305+
# validate_bundle — malformed JSON root type guards
1306+
# ---------------------------------------------------------------------------
1307+
1308+
1309+
@pytest.mark.parametrize("root_value", [None, [], "string", 42, True])
1310+
def test_runtime_validator_rejects_non_object_root_direct_api(tmp_path, root_value):
1311+
"""Direct validate_bundle raises ContractError for non-object JSON roots."""
1312+
bundle_path = tmp_path / "bundle.json"
1313+
bundle_path.write_text(json.dumps(root_value))
1314+
with pytest.raises(ContractError, match="bundle root must be an object"):
1315+
validate_bundle(bundle_path, {"acme-ai": "ACME_AI_API_KEY"})
1316+
1317+
1318+
@pytest.mark.parametrize("root_value", [None, [], "string", 42, True])
1319+
def test_validator_cli_rejects_non_object_root(tmp_path, root_value):
1320+
"""CLI subprocess exits 1 with no Traceback for non-object JSON roots."""
1321+
bundle_path = tmp_path / "bundle.json"
1322+
bundle_path.write_text(json.dumps(root_value))
1323+
result = subprocess.run(
1324+
[
1325+
"python3",
1326+
str(VALIDATOR_PATH),
1327+
"--schema-dir",
1328+
str(SCHEMAS_DIR),
1329+
"--credential-bindings",
1330+
str(SCHEMAS_DIR / "provider-credential-bindings.json"),
1331+
"--bundle",
1332+
str(bundle_path),
1333+
],
1334+
text=True,
1335+
capture_output=True,
1336+
check=False,
1337+
)
1338+
assert result.returncode == 1
1339+
assert "Traceback" not in result.stderr
1340+
assert "bundle root must be an object" in result.stderr
1341+
1342+
1343+
@pytest.mark.parametrize("schema_version_value", [None, [], 42, True, {"nested": "object"}])
1344+
def test_runtime_validator_rejects_non_string_schema_version_direct_api(tmp_path, schema_version_value):
1345+
"""Direct validate_bundle raises ContractError for non-string schemaVersion."""
1346+
bundle_path = tmp_path / "bundle.json"
1347+
bundle_path.write_text(json.dumps({"schemaVersion": schema_version_value}))
1348+
with pytest.raises(ContractError, match="bundle schemaVersion must be a string"):
1349+
validate_bundle(bundle_path, {"acme-ai": "ACME_AI_API_KEY"})
1350+
1351+
1352+
@pytest.mark.parametrize("schema_version_value", [None, [], 42, True, {"nested": "object"}])
1353+
def test_validator_cli_rejects_non_string_schema_version(tmp_path, schema_version_value):
1354+
"""CLI subprocess exits 1 with no Traceback for non-string schemaVersion."""
1355+
bundle_path = tmp_path / "bundle.json"
1356+
bundle_path.write_text(json.dumps({"schemaVersion": schema_version_value}))
1357+
result = subprocess.run(
1358+
[
1359+
"python3",
1360+
str(VALIDATOR_PATH),
1361+
"--schema-dir",
1362+
str(SCHEMAS_DIR),
1363+
"--credential-bindings",
1364+
str(SCHEMAS_DIR / "provider-credential-bindings.json"),
1365+
"--bundle",
1366+
str(bundle_path),
1367+
],
1368+
text=True,
1369+
capture_output=True,
1370+
check=False,
1371+
)
1372+
assert result.returncode == 1
1373+
assert "Traceback" not in result.stderr
1374+
assert "bundle schemaVersion must be a string" in result.stderr

‎scripts/validate_provider_input_contract.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ def validate_bundle(
139139
if credential_bindings is None:
140140
credential_bindings = load_json(HOST_CREDENTIAL_BINDINGS)["providers"]
141141
bundle = load_json(bundle_path)
142-
schema_name = SUPPORTED_BUNDLE_SCHEMAS.get(bundle.get("schemaVersion"))
142+
if not isinstance(bundle, dict):
143+
raise ContractError(f"{bundle_path}: bundle root must be an object")
144+
schema_version = bundle.get("schemaVersion")
145+
if not isinstance(schema_version, str):
146+
raise ContractError(f"{bundle_path}: bundle schemaVersion must be a string")
147+
schema_name = SUPPORTED_BUNDLE_SCHEMAS.get(schema_version)
143148
if schema_name is None:
144149
raise ContractError(f"{bundle_path}: unsupported bundle schemaVersion")
145150
validate_schema(bundle, schema_name, str(bundle_path), schema_dir)

0 commit comments

Comments
 (0)