mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 10:18:18 +00:00
feat(valid-detection-benchmark): isolate label blind arm execution
This commit is contained in:
@@ -0,0 +1,370 @@
|
||||
"""Task 6.3 isolated label-blind execution boundary tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
from dataclasses import fields, replace
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from test_valid_benchmark_arm_registry import _digest, _registrations
|
||||
from test_valid_benchmark_label_blind_manifest import _reviewed
|
||||
|
||||
from benchmarks.valid import arm_contract as registry
|
||||
from benchmarks.valid import isolated_execution as execution
|
||||
from benchmarks.valid.replacement_artifact_contract import (
|
||||
ArtifactAccessClass,
|
||||
ArtifactKind,
|
||||
ArtifactReference,
|
||||
)
|
||||
from openultrasast.core.evaluation_measurement import ArmKindV3
|
||||
from openultrasast.core.evaluation_plan import derive_evaluation_input_set_digest
|
||||
|
||||
|
||||
def _plan(manifest=None) -> registry.RegisteredEvaluationPlanV1:
|
||||
manifest = _reviewed()[-1] if manifest is None else manifest
|
||||
return registry.register_evaluation_plan(
|
||||
registrations=_registrations(),
|
||||
population_digest=manifest.population_digest,
|
||||
freeze_digest=manifest.freeze_digest,
|
||||
input_set_digest=derive_evaluation_input_set_digest(manifest.input_digests),
|
||||
input_identity_digests=manifest.input_digests,
|
||||
)
|
||||
|
||||
|
||||
def _case(
|
||||
input_digest: str,
|
||||
*,
|
||||
change_digest: str | None = None,
|
||||
reference_digest: str | None = None,
|
||||
byte_length: int = 128,
|
||||
provenance_digest: str | None = None,
|
||||
access: ArtifactAccessClass = ArtifactAccessClass.PUBLIC,
|
||||
) -> execution.LabelBlindCaseInputV1:
|
||||
change = input_digest if change_digest is None else change_digest
|
||||
reference = ArtifactReference(
|
||||
ArtifactKind.POPULATION,
|
||||
reference_digest or change,
|
||||
byte_length,
|
||||
provenance_digest or _digest(f"provenance-{input_digest}"),
|
||||
access,
|
||||
)
|
||||
return execution.LabelBlindCaseInputV1(input_digest, change, reference, "")
|
||||
|
||||
|
||||
def _cases(manifest=None) -> tuple[execution.LabelBlindCaseInputV1, ...]:
|
||||
_, selection, partition, *_, reviewed = _reviewed()
|
||||
manifest = reviewed if manifest is None else manifest
|
||||
pushes = {row.push_digest: row for row in selection.pushes}
|
||||
public = {row.change_digest: row for row in partition.manifest.public_sources}
|
||||
private = {row.change_digest: row for row in partition.manifest.private_refs}
|
||||
return tuple(
|
||||
_case(
|
||||
item,
|
||||
change_digest=pushes[item].change_content_digest,
|
||||
byte_length=pushes[item].change_byte_length,
|
||||
provenance_digest=pushes[item].history_source_digest,
|
||||
access=ArtifactAccessClass.PRIVATE,
|
||||
)
|
||||
if item in pushes
|
||||
else _case(
|
||||
item,
|
||||
reference_digest=(public[item].source_digest if item in public else private[item].source_digest),
|
||||
byte_length=(public[item].byte_length if item in public else private[item].byte_length),
|
||||
provenance_digest=(public[item].object_digest if item in public else partition.manifest.private_object_digest),
|
||||
access=(ArtifactAccessClass.PUBLIC if item in public else ArtifactAccessClass.PRIVATE),
|
||||
)
|
||||
for item in manifest.input_digests
|
||||
)
|
||||
|
||||
|
||||
def _budgets(plan: registry.RegisteredEvaluationPlanV1) -> tuple[execution.ArmExecutionBudgetV1, ...]:
|
||||
return tuple(
|
||||
execution.ArmExecutionBudgetV1(
|
||||
row.arm_id,
|
||||
row.configuration.registered_budget_digest,
|
||||
120,
|
||||
512 * 1024 * 1024,
|
||||
1024 * 1024 * 1024,
|
||||
4 * 1024 * 1024,
|
||||
"",
|
||||
)
|
||||
for row in plan.registrations
|
||||
if row.configuration.kind is ArmKindV3.COMPONENT
|
||||
)
|
||||
|
||||
|
||||
def _bundles(
|
||||
*,
|
||||
adapter_kind: execution.ExecutionAdapterKind = execution.ExecutionAdapterKind.LOCAL,
|
||||
) -> tuple[execution.LabelBlindExecutionBundleV1, ...]:
|
||||
_, selection, partition, *_, manifest = _reviewed()
|
||||
plan = _plan(manifest)
|
||||
return execution.build_component_execution_bundles(
|
||||
plan,
|
||||
manifest,
|
||||
selection,
|
||||
partition.manifest,
|
||||
_cases(manifest),
|
||||
_budgets(plan),
|
||||
adapter_kind=adapter_kind,
|
||||
output_namespace_digest=_digest("observation-output-namespace"),
|
||||
)
|
||||
|
||||
|
||||
def _authorities():
|
||||
_, selection, partition, *_, manifest = _reviewed()
|
||||
return manifest, partition.manifest, selection
|
||||
|
||||
|
||||
def test_component_bundles_share_exact_ordered_label_blind_inputs() -> None:
|
||||
bundles = _bundles()
|
||||
|
||||
assert tuple(row.arm.arm_id for row in bundles) == ("arm-experimental", "arm-shipped")
|
||||
assert all(row.case_inputs is bundles[0].case_inputs for row in bundles)
|
||||
assert tuple(item.input_identity_digest for item in bundles[0].case_inputs) == _reviewed()[-1].input_digests
|
||||
assert all(row.input_set_digest == _reviewed()[-1].input_set_digest for row in bundles)
|
||||
assert all(row.arm.role is not registry.ArmRole.COMBINED for row in bundles)
|
||||
assert all(row.arm.configuration_kind is ArmKindV3.COMPONENT for row in bundles)
|
||||
|
||||
|
||||
def test_bundle_has_no_label_custody_review_or_secret_surface() -> None:
|
||||
bundles = _bundles(adapter_kind=execution.ExecutionAdapterKind.AX)
|
||||
plan = _plan()
|
||||
forbidden_fields = {
|
||||
"label",
|
||||
"labels",
|
||||
"expected_outcome",
|
||||
"finding",
|
||||
"custody",
|
||||
"review",
|
||||
"strategy_decision_authority",
|
||||
"credentials",
|
||||
"secret",
|
||||
"endpoint",
|
||||
"path",
|
||||
}
|
||||
for bundle in bundles:
|
||||
content = execution.label_blind_execution_bundle_bytes(plan, bundle, *_authorities())
|
||||
decoded = json.loads(content)
|
||||
assert forbidden_fields.isdisjoint(decoded)
|
||||
assert not any(name in forbidden_fields for name in (field.name for field in fields(bundle)))
|
||||
for sentinel in (
|
||||
b"sealed label",
|
||||
b"expected_outcome",
|
||||
b"password=",
|
||||
b"api_key",
|
||||
b"strategy_decision",
|
||||
b"/home/",
|
||||
b"s3://",
|
||||
):
|
||||
assert sentinel not in content.lower()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("access", (ArtifactAccessClass.CUSTODIAN_SEALED, ArtifactAccessClass.OPERATOR_SAFE))
|
||||
def test_sealed_or_operator_references_cannot_enter_case_inputs(access: ArtifactAccessClass) -> None:
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
_case(_reviewed()[-1].input_digests[0], access=access)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mutation",
|
||||
("missing", "extra", "duplicate", "order", "zero", "wrong_input", "wrong_change", "wrong_provenance"),
|
||||
)
|
||||
def test_bundle_builder_rejects_incomplete_or_changed_input_set(mutation: str) -> None:
|
||||
manifest = _reviewed()[-1]
|
||||
selection = _reviewed()[1]
|
||||
partition = _reviewed()[2]
|
||||
plan = _plan(manifest)
|
||||
cases = _cases(manifest)
|
||||
with pytest.raises((execution.IsolatedExecutionError, ValueError)):
|
||||
if mutation == "missing":
|
||||
cases = cases[:-1]
|
||||
elif mutation == "extra":
|
||||
cases = (*cases, _case(_digest("extra-input")))
|
||||
elif mutation == "duplicate":
|
||||
cases = (*cases[:-1], cases[0])
|
||||
elif mutation == "order":
|
||||
cases = tuple(reversed(cases))
|
||||
elif mutation == "zero":
|
||||
reference = replace(cases[0].code_reference, byte_length=0)
|
||||
cases = (replace(cases[0], code_reference=reference, case_digest=""), *cases[1:])
|
||||
elif mutation == "wrong_input":
|
||||
cases = (replace(cases[0], input_identity_digest=_digest("wrong-input"), case_digest=""), *cases[1:])
|
||||
elif mutation == "wrong_change":
|
||||
changed = _digest("wrong-change")
|
||||
reference = replace(cases[0].code_reference, content_digest=changed)
|
||||
cases = (replace(cases[0], change_input_digest=changed, code_reference=reference, case_digest=""), *cases[1:])
|
||||
else:
|
||||
reference = replace(cases[0].code_reference, provenance_digest=_digest("wrong-provenance"))
|
||||
cases = (replace(cases[0], code_reference=reference, case_digest=""), *cases[1:])
|
||||
execution.build_component_execution_bundles(
|
||||
plan,
|
||||
manifest,
|
||||
selection,
|
||||
partition.manifest,
|
||||
cases,
|
||||
_budgets(plan),
|
||||
adapter_kind=execution.ExecutionAdapterKind.LOCAL,
|
||||
output_namespace_digest=_digest("output"),
|
||||
)
|
||||
|
||||
|
||||
def test_environment_is_fixed_and_does_not_inherit_ambient_or_aliases() -> None:
|
||||
tainted = {
|
||||
"PATH": "/attacker/bin",
|
||||
"OPENAI_API_KEY": "model-secret",
|
||||
"openai_api_key": "alias-secret",
|
||||
"AWS_SECRET_ACCESS_KEY": "store-secret",
|
||||
"CORPUS_S3_SECRET_KEY": "admin-secret",
|
||||
"LABEL_BYTES": "private-label",
|
||||
"REVIEW_TOKEN": "review-secret",
|
||||
"OUSAST_CUSTODY_TOKEN": "custody-secret",
|
||||
}
|
||||
environment = execution.build_execution_environment(execution.ExecutionAdapterKind.DOCKER, tainted)
|
||||
|
||||
assert dict(environment.variables) == execution.SAFE_EXECUTION_ENVIRONMENT
|
||||
assert not (set(tainted) - {"PATH"}).intersection(dict(environment.variables))
|
||||
assert dict(environment.variables)["PATH"] != tainted["PATH"]
|
||||
content = execution.execution_environment_bytes(environment)
|
||||
assert all(value.encode() not in content for value in tainted.values())
|
||||
|
||||
|
||||
def test_environment_contract_rejects_direct_allowlist_bypass() -> None:
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.ExecutionEnvironmentV1(
|
||||
execution.ExecutionAdapterKind.LOCAL,
|
||||
(("MODEL_PROVIDER_TOKEN", "private-canary"),),
|
||||
"",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field_name", ("deadline_seconds", "memory_bytes", "disk_bytes", "maximum_output_bytes"))
|
||||
def test_budget_is_positive_bounded_and_bound_to_registered_arm(field_name: str) -> None:
|
||||
plan = _plan()
|
||||
selection = _reviewed()[1]
|
||||
partition = _reviewed()[2]
|
||||
budget = _budgets(plan)[0]
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
replace(budget, **{field_name: 0}, budget_digest="")
|
||||
wrong = replace(budget, registered_budget_digest=_digest("wrong-budget"), budget_digest="")
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.build_component_execution_bundles(
|
||||
plan,
|
||||
_reviewed()[-1],
|
||||
selection,
|
||||
partition.manifest,
|
||||
_cases(),
|
||||
(wrong, *_budgets(plan)[1:]),
|
||||
adapter_kind=execution.ExecutionAdapterKind.LOCAL,
|
||||
output_namespace_digest=_digest("output"),
|
||||
)
|
||||
|
||||
|
||||
class _Adapter:
|
||||
def __init__(self, *, reason: execution.ProcessOutcomeReason = execution.ProcessOutcomeReason.NONE) -> None:
|
||||
self.reason = reason
|
||||
self.calls: list[execution.LabelBlindExecutionBundleV1] = []
|
||||
|
||||
def execute(self, bundle: execution.LabelBlindExecutionBundleV1) -> execution.ArmProcessOutcomeV1:
|
||||
self.calls.append(bundle)
|
||||
if self.reason is not execution.ProcessOutcomeReason.NONE:
|
||||
return execution.impaired_adapter_outcome(bundle, self.reason, runtime_ms=3)
|
||||
return execution.completed_adapter_outcome(bundle, b"detector-output", runtime_ms=7)
|
||||
|
||||
|
||||
def test_adapter_runs_once_per_component_and_never_for_combined_arm() -> None:
|
||||
bundles = _bundles()
|
||||
adapter = _Adapter()
|
||||
|
||||
outcomes = execution.execute_component_bundles(_plan(), bundles, adapter, *_authorities())
|
||||
|
||||
assert tuple(row.arm_id for row in outcomes) == ("arm-experimental", "arm-shipped")
|
||||
assert adapter.calls == list(bundles)
|
||||
assert all(row.status is execution.ProcessOutcomeStatus.COMPLETED for row in outcomes)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"reason",
|
||||
(
|
||||
execution.ProcessOutcomeReason.BACKPRESSURE,
|
||||
execution.ProcessOutcomeReason.DEADLINE,
|
||||
execution.ProcessOutcomeReason.BUDGET_EXHAUSTED,
|
||||
execution.ProcessOutcomeReason.RESOURCE_LIMIT,
|
||||
execution.ProcessOutcomeReason.PROCESS_FAILURE,
|
||||
),
|
||||
)
|
||||
def test_adapter_impairment_is_closed_and_does_not_attribute_infrastructure(reason: execution.ProcessOutcomeReason) -> None:
|
||||
adapter = _Adapter(reason=reason)
|
||||
plan = _plan()
|
||||
bundles = _bundles(adapter_kind=execution.ExecutionAdapterKind.AX)
|
||||
outcomes = execution.execute_component_bundles(plan, bundles, adapter, *_authorities())
|
||||
content = b"".join(
|
||||
execution.arm_process_outcome_bytes(plan, bundle, row, *_authorities()) for bundle, row in zip(bundles, outcomes, strict=True)
|
||||
)
|
||||
|
||||
assert all(row.status is execution.ProcessOutcomeStatus.IMPAIRED and row.reason is reason for row in outcomes)
|
||||
for forbidden in (b"cluster", b"worker", b"network", b"node", b"k3s", b"exception"):
|
||||
assert forbidden not in content.lower()
|
||||
|
||||
|
||||
class _FailingAdapter:
|
||||
def execute(self, bundle: execution.LabelBlindExecutionBundleV1) -> execution.ArmProcessOutcomeV1:
|
||||
raise RuntimeError("provider-diagnostic private-canary /restricted/location")
|
||||
|
||||
|
||||
def test_adapter_exception_becomes_closed_outcome_without_diagnostic_retention() -> None:
|
||||
plan = _plan()
|
||||
bundles = _bundles()
|
||||
outcomes = execution.execute_component_bundles(plan, bundles, _FailingAdapter(), *_authorities())
|
||||
content = b"".join(
|
||||
execution.arm_process_outcome_bytes(plan, bundle, row, *_authorities()) for bundle, row in zip(bundles, outcomes, strict=True)
|
||||
)
|
||||
|
||||
assert all(row.reason is execution.ProcessOutcomeReason.PROCESS_FAILURE for row in outcomes)
|
||||
assert b"provider" not in content and b"private" not in content
|
||||
|
||||
|
||||
class _CrossArmAdapter:
|
||||
def execute(self, bundle: execution.LabelBlindExecutionBundleV1) -> execution.ArmProcessOutcomeV1:
|
||||
outcome = execution.completed_adapter_outcome(bundle, b"detector-output", runtime_ms=1)
|
||||
return replace(outcome, arm_id="different-arm", outcome_digest="")
|
||||
|
||||
|
||||
def test_adapter_cannot_return_an_outcome_for_a_different_arm() -> None:
|
||||
outcomes = execution.execute_component_bundles(
|
||||
_plan(),
|
||||
_bundles(),
|
||||
_CrossArmAdapter(),
|
||||
*_authorities(),
|
||||
)
|
||||
|
||||
assert all(row.reason is execution.ProcessOutcomeReason.OUTPUT_INVALID for row in outcomes)
|
||||
|
||||
|
||||
def test_direct_bundle_and_outcome_mutation_is_rejected_by_serialization() -> None:
|
||||
bundle = _bundles()[0]
|
||||
plan = _plan()
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.label_blind_execution_bundle_bytes(
|
||||
plan,
|
||||
replace(bundle, plan_digest=_digest("wrong-plan"), bundle_digest=""),
|
||||
*_authorities(),
|
||||
)
|
||||
outcome = execution.completed_adapter_outcome(bundle, b"output", runtime_ms=1)
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.arm_process_outcome_bytes(
|
||||
plan,
|
||||
bundle,
|
||||
replace(outcome, output_bytes=0, outcome_digest=""),
|
||||
*_authorities(),
|
||||
)
|
||||
|
||||
|
||||
def test_execution_boundary_has_no_label_or_custody_hook() -> None:
|
||||
public = {name for name in execution.__all__ if not name.startswith("_")}
|
||||
assert not any(token in name.lower() for name in public for token in ("label_loader", "custody", "review", "credential"))
|
||||
@@ -0,0 +1,380 @@
|
||||
"""Authority regressions for the Task 6.3 isolated executor."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import sys
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from test_valid_benchmark_arm_registry import _digest, _registrations
|
||||
from test_valid_benchmark_isolated_executor import _authorities, _budgets, _bundles, _cases, _plan
|
||||
from test_valid_benchmark_label_blind_manifest import _reviewed
|
||||
|
||||
from benchmarks.valid import arm_contract as registry
|
||||
from benchmarks.valid import isolated_execution as execution
|
||||
from benchmarks.valid import isolated_execution_bundle as execution_bundle
|
||||
from benchmarks.valid import isolated_execution_source as execution_source
|
||||
from benchmarks.valid import ordinary_change_contract as ordinary_contract
|
||||
from benchmarks.valid import population_material_contract as material_contract
|
||||
from benchmarks.valid import population_materials as materials
|
||||
from benchmarks.valid.replacement_artifact_contract import (
|
||||
ArtifactAccessClass,
|
||||
ArtifactKind,
|
||||
ArtifactReference,
|
||||
)
|
||||
from openultrasast.core import canonical
|
||||
|
||||
|
||||
def test_bundle_serialization_rejects_canonical_arm_substitution_under_original_plan() -> None:
|
||||
bundle = _bundles()[0]
|
||||
attacker_arm = replace(bundle.arm, analyzer_identity="attacker-analyzer", execution_arm_digest="")
|
||||
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
attacker_bundle = replace(bundle, arm=attacker_arm, bundle_digest="")
|
||||
execution.label_blind_execution_bundle_bytes(_plan(), attacker_bundle, *_authorities())
|
||||
|
||||
|
||||
def test_real_ordinary_push_keeps_distinct_case_and_change_identities() -> None:
|
||||
_, selection, *_ = _reviewed()
|
||||
push = selection.pushes[0]
|
||||
assert push.push_digest != push.change_content_digest
|
||||
reference = ArtifactReference(
|
||||
ArtifactKind.POPULATION,
|
||||
push.change_content_digest,
|
||||
push.change_byte_length,
|
||||
push.history_source_digest,
|
||||
ArtifactAccessClass.PRIVATE,
|
||||
)
|
||||
|
||||
case = execution.LabelBlindCaseInputV1(push.push_digest, push.change_content_digest, reference, "")
|
||||
|
||||
assert case.input_identity_digest == push.push_digest
|
||||
assert case.change_input_digest == push.change_content_digest
|
||||
|
||||
|
||||
def test_controller_serializer_requires_exact_registered_plan_authority() -> None:
|
||||
parameters = inspect.signature(execution.label_blind_execution_bundle_bytes).parameters
|
||||
|
||||
assert {
|
||||
"plan",
|
||||
"manifest",
|
||||
"public_material_manifest",
|
||||
"selection",
|
||||
} <= parameters.keys()
|
||||
|
||||
|
||||
def test_controller_rejects_bundle_derived_from_different_valid_analyzer_plan() -> None:
|
||||
_, selection, partition, *_, manifest = _reviewed()
|
||||
original = _plan(manifest)
|
||||
registrations = list(_registrations())
|
||||
target_index = next(index for index, row in enumerate(registrations) if row.arm_id == "arm-experimental")
|
||||
target = registrations[target_index]
|
||||
changed_configuration = replace(
|
||||
target.configuration,
|
||||
analyzer_identity="attacker-analyzer",
|
||||
configuration_identity_digest="",
|
||||
)
|
||||
registrations[target_index] = replace(target, configuration=changed_configuration, registration_digest="")
|
||||
forged = registry.register_evaluation_plan(
|
||||
registrations=tuple(registrations),
|
||||
population_digest=manifest.population_digest,
|
||||
freeze_digest=manifest.freeze_digest,
|
||||
input_set_digest=manifest.input_set_digest,
|
||||
input_identity_digests=manifest.input_digests,
|
||||
)
|
||||
forged_bundle = execution.build_component_execution_bundles(
|
||||
forged,
|
||||
manifest,
|
||||
selection,
|
||||
partition.manifest,
|
||||
_cases(manifest),
|
||||
_budgets(forged),
|
||||
adapter_kind=execution.ExecutionAdapterKind.LOCAL,
|
||||
output_namespace_digest=_digest("output"),
|
||||
)[0]
|
||||
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.label_blind_execution_bundle_bytes(
|
||||
original,
|
||||
forged_bundle,
|
||||
manifest,
|
||||
partition.manifest,
|
||||
selection,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mutation", ("stale", "missing", "extra"))
|
||||
def test_controller_rejects_rehashed_projection_with_changed_baseline_vector(mutation: str) -> None:
|
||||
bundle = _bundles()[0]
|
||||
projection = bundle.plan_projection
|
||||
if mutation == "missing":
|
||||
baselines = projection.baseline_digests[:-1]
|
||||
elif mutation == "extra":
|
||||
baselines = (*projection.baseline_digests, _digest("extra-baseline"))
|
||||
else:
|
||||
baselines = (_digest("stale-baseline"), *projection.baseline_digests[1:])
|
||||
plan_value = {
|
||||
"schema_version": registry.REGISTERED_PLAN_SCHEMA,
|
||||
"registration_digests": tuple(row.registration_digest for row in projection.arm_bindings),
|
||||
"declaration_digest": projection.declaration_digest,
|
||||
"baseline_digests": baselines,
|
||||
}
|
||||
changed_projection = replace(
|
||||
projection,
|
||||
baseline_digests=baselines,
|
||||
plan_digest=canonical.content_digest(plan_value),
|
||||
projection_digest="",
|
||||
)
|
||||
destination = replace(
|
||||
bundle.output_destination,
|
||||
plan_digest=changed_projection.plan_digest,
|
||||
destination_digest="",
|
||||
)
|
||||
changed_bundle = replace(
|
||||
bundle,
|
||||
plan_projection=changed_projection,
|
||||
plan_digest=changed_projection.plan_digest,
|
||||
output_destination=destination,
|
||||
bundle_digest="",
|
||||
)
|
||||
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.label_blind_execution_bundle_bytes(_plan(), changed_bundle, *_authorities())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mutation", ("source", "length", "provenance", "access", "swapped"))
|
||||
def test_positive_reference_cannot_detach_from_material_authority(mutation: str) -> None:
|
||||
_, selection, partition, *_, manifest = _reviewed()
|
||||
plan = _plan(manifest)
|
||||
cases = _cases(manifest)
|
||||
positives = tuple(index for index, case in enumerate(cases) if case.input_identity_digest in selection.positive_change_digests)
|
||||
positive = positives[0]
|
||||
reference = cases[positive].code_reference
|
||||
if mutation == "source":
|
||||
changed_reference = replace(reference, content_digest=_digest("wrong-source"))
|
||||
elif mutation == "length":
|
||||
changed_reference = replace(reference, byte_length=1)
|
||||
elif mutation == "provenance":
|
||||
changed_reference = replace(reference, provenance_digest=_digest("wrong-source-provenance"))
|
||||
elif mutation == "access":
|
||||
changed_reference = replace(reference, access_class=ArtifactAccessClass.PRIVATE)
|
||||
else:
|
||||
changed_reference = cases[positives[1]].code_reference
|
||||
changed_case = replace(cases[positive], code_reference=changed_reference, case_digest="")
|
||||
changed_cases = (*cases[:positive], changed_case, *cases[positive + 1 :])
|
||||
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.build_component_execution_bundles(
|
||||
plan,
|
||||
manifest,
|
||||
selection,
|
||||
partition.manifest,
|
||||
changed_cases,
|
||||
_budgets(plan),
|
||||
adapter_kind=execution.ExecutionAdapterKind.LOCAL,
|
||||
output_namespace_digest=_digest("output"),
|
||||
)
|
||||
|
||||
|
||||
def test_execution_source_projection_and_bundles_never_open_sealed_material(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
_, selection, partition, *_, manifest = _reviewed()
|
||||
sealed_byte_count = sum(len(row.label_content) + len(row.judgment_content) for row in partition.sealed_label_records)
|
||||
assert sealed_byte_count > 0
|
||||
plan = _plan(manifest)
|
||||
cases = _cases(manifest)
|
||||
budgets = _budgets(plan)
|
||||
calls: list[str] = []
|
||||
|
||||
def fail_if_called(name: str):
|
||||
def fail(*_args, **_kwargs):
|
||||
calls.append(name)
|
||||
raise AssertionError(f"forbidden sealed-material hook called: {name}")
|
||||
|
||||
return fail
|
||||
|
||||
for module, name in (
|
||||
(material_contract, "sealed_label_object_bytes"),
|
||||
(material_contract, "_label_record_value"),
|
||||
(materials, "sealed_label_object_bytes"),
|
||||
(materials, "material_artifact_bytes"),
|
||||
(materials, "resolve_sealed_labels"),
|
||||
):
|
||||
monkeypatch.setattr(module, name, fail_if_called(name))
|
||||
|
||||
projection = execution.build_execution_source_projection(
|
||||
manifest,
|
||||
partition.manifest,
|
||||
selection,
|
||||
)
|
||||
bundles = execution.build_component_execution_bundles(
|
||||
plan,
|
||||
manifest,
|
||||
selection,
|
||||
partition.manifest,
|
||||
cases,
|
||||
budgets,
|
||||
adapter_kind=execution.ExecutionAdapterKind.LOCAL,
|
||||
output_namespace_digest=_digest("zero-access-output"),
|
||||
)
|
||||
|
||||
assert len(bundles) == 2
|
||||
assert all(bundle.source_projection == projection for bundle in bundles)
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_execution_bundle_import_and_signature_exclude_full_material_authority() -> None:
|
||||
parameters = inspect.signature(execution.build_component_execution_bundles).parameters
|
||||
source = inspect.getsource(execution_bundle)
|
||||
|
||||
assert "public_material_manifest" in parameters
|
||||
assert "material_partition" not in parameters
|
||||
for forbidden in (
|
||||
"PopulationMaterialPartitionV1",
|
||||
"PrivateSourceRecordV1",
|
||||
"SealedLabelRecordV1",
|
||||
"IndependenceAttestation",
|
||||
):
|
||||
assert forbidden not in source
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mutation", ("public_manifest", "artifact_set", "input_set"))
|
||||
def test_execution_source_authority_identity_mutation_is_rejected(mutation: str) -> None:
|
||||
_, selection, partition, *_, manifest = _reviewed()
|
||||
if mutation == "public_manifest":
|
||||
changed_public_manifest = replace(
|
||||
partition.manifest,
|
||||
manifest_digest=_digest("wrong-public-manifest"),
|
||||
)
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.build_execution_source_projection(
|
||||
manifest,
|
||||
changed_public_manifest,
|
||||
selection,
|
||||
)
|
||||
return
|
||||
|
||||
bundle = _bundles()[0]
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
changed_projection = replace(
|
||||
bundle.source_projection,
|
||||
**{"material_artifact_set_digest" if mutation == "artifact_set" else "input_set_digest": _digest(f"wrong-{mutation}")},
|
||||
)
|
||||
execution.label_blind_execution_bundle_bytes(
|
||||
_plan(),
|
||||
replace(bundle, source_projection=changed_projection, bundle_digest=""),
|
||||
*_authorities(),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"field_name",
|
||||
(
|
||||
"public_material_manifest_digest",
|
||||
"material_artifact_set_digest",
|
||||
"ordinary_selection_digest",
|
||||
),
|
||||
)
|
||||
def test_rehashed_source_projection_cannot_replace_controller_authority(field_name: str) -> None:
|
||||
bundle = _bundles()[0]
|
||||
changed_projection = replace(
|
||||
bundle.source_projection,
|
||||
**{field_name: _digest(f"attacker-{field_name}"), "projection_digest": ""},
|
||||
)
|
||||
attacker_bundle = replace(
|
||||
bundle,
|
||||
source_projection=changed_projection,
|
||||
bundle_digest="",
|
||||
)
|
||||
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.label_blind_execution_bundle_bytes(_plan(), attacker_bundle, *_authorities())
|
||||
|
||||
|
||||
def _different_valid_selection(selection: ordinary_contract.OrdinarySelectionV1) -> ordinary_contract.OrdinarySelectionV1:
|
||||
policy_digest = _digest("different-valid-selection-policy")
|
||||
provenance_digest = canonical.content_digest(
|
||||
{
|
||||
"history_sources": tuple(
|
||||
sorted({(push.canonical_repository, push.network_identity_digest, push.history_source_digest) for push in selection.pushes})
|
||||
),
|
||||
"accepted_fix_exclusions": tuple(ordinary_contract._fix_exclusion_value(item) for item in selection.fix_exclusions),
|
||||
"policy_digest": policy_digest,
|
||||
}
|
||||
)
|
||||
changed = replace(
|
||||
selection,
|
||||
policy_digest=policy_digest,
|
||||
provenance_digest=provenance_digest,
|
||||
selection_digest="",
|
||||
)
|
||||
return replace(
|
||||
changed,
|
||||
selection_digest=canonical.content_digest(ordinary_contract._selection_value(changed, include_digest=False)),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("authority", ("public_manifest", "ordinary_selection"))
|
||||
def test_bundle_rejects_different_internally_valid_source_authority(authority: str) -> None:
|
||||
manifest, public_manifest, selection = _authorities()
|
||||
if authority == "public_manifest":
|
||||
changed = replace(
|
||||
public_manifest,
|
||||
partition_policy_digest=_digest("different-valid-partition-policy"),
|
||||
manifest_digest="",
|
||||
)
|
||||
public_manifest = replace(
|
||||
changed,
|
||||
manifest_digest=canonical.content_digest(material_contract._manifest_value(changed, include_digest=False)),
|
||||
)
|
||||
assert execution_source._public_manifest_bytes(public_manifest)
|
||||
else:
|
||||
selection = _different_valid_selection(selection)
|
||||
assert ordinary_contract.ordinary_selection_bytes(selection)
|
||||
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
execution.label_blind_execution_bundle_bytes(
|
||||
_plan(),
|
||||
_bundles()[0],
|
||||
manifest,
|
||||
public_manifest,
|
||||
selection,
|
||||
)
|
||||
|
||||
|
||||
class _NeverExecuteAdapter:
|
||||
def execute(self, _bundle: execution.LabelBlindExecutionBundleV1) -> execution.ArmProcessOutcomeV1:
|
||||
raise AssertionError("source authority substitution reached the adapter")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("boundary", ("outcome", "execution"))
|
||||
def test_rehashed_source_projection_is_rejected_at_controller_boundaries(boundary: str) -> None:
|
||||
bundle = _bundles()[0]
|
||||
changed_projection = replace(
|
||||
bundle.source_projection,
|
||||
material_artifact_set_digest=_digest("attacker-boundary-artifact-set"),
|
||||
projection_digest="",
|
||||
)
|
||||
attacker_bundle = replace(bundle, source_projection=changed_projection, bundle_digest="")
|
||||
with pytest.raises(execution.IsolatedExecutionError):
|
||||
if boundary == "outcome":
|
||||
outcome = execution.completed_adapter_outcome(attacker_bundle, b"output", runtime_ms=1)
|
||||
execution.arm_process_outcome_bytes(
|
||||
_plan(),
|
||||
attacker_bundle,
|
||||
outcome,
|
||||
*_authorities(),
|
||||
)
|
||||
else:
|
||||
execution.execute_component_bundles(
|
||||
_plan(),
|
||||
(attacker_bundle,),
|
||||
_NeverExecuteAdapter(),
|
||||
*_authorities(),
|
||||
)
|
||||
Reference in New Issue
Block a user