OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
feat(examples): add synthetic security workflow benchmarks (#2618)
Neha Rungta462bad3da308
diff --git a/examples/README.md b/examples/README.mdindex f58ede64c..e35d155e6 100644--- a/examples/README.md+++ b/examples/README.md@@ -3,6 +3,7 @@| Task | Example || --- | --- || Scan an app, inspect a finding, patch it and verify the repair | [Invoice Desk walkthrough](invoice-desk/walkthrough.md). Copy only the synthetic app into the scan target. |+| Compare code findings with deployed VPC reachability | [Network reachability sample](network-reachability-sample/README.md) pairs identical vulnerable workers with different security-group and network ACL rules. || Supply your own validation test | [Custom validation demo](custom-validation/README.md). Run the local HTTP test on its own or let a model-backed scan invoke it. || Scan pull requests and repositories with GitHub Actions | [GitHub Actions with Amazon Bedrock](github-actions/README.md) uses AWS OIDC and saves SARIF and readable reports. || Scan Azure Repos from a central pipeline | [Azure Pipelines with Amazon Bedrock](azure-pipelines/README.md) runs manual full or committed-diff scans. |diff --git a/examples/network-reachability-sample/README.md b/examples/network-reachability-sample/README.mdnew file mode 100644index 000000000..a9095fb45--- /dev/null+++ b/examples/network-reachability-sample/README.md@@ -0,0 +1,35 @@+# Network reachability sample++This synthetic benchmark tests whether a scanner separates a code vulnerability+from the exposure of each deployed copy. **It intentionally deploys remote code+execution paths. Use only an isolated test account and remove the stack after+collecting evidence.** The stack creates a public EC2 gateway and two private+EC2 workers. All three workers run the *same* `worker.zip`; their deployed+network controls give them different reachability.++| ID | Seeded behavior | Expected internet exposure |+| --- | --- | --- |+| `NR-001` | `/run?cmd=...` injects a shell command on `OpenWorker`. | Reachable through public gateway `/open`. |+| `NR-002` | The identical `/run` code runs on `BlockedWorker`. | Gateway has a route, but the worker security group denies its traffic. |+| `NR-003` | The identical `/run` code runs on `AclBlockedWorker`. | Its security group allows the gateway, but the subnet network ACL denies the request. |+| `NR-004` | `/diagnostic?expr=...` evaluates Python code when enabled. | All worker services disable it, and no gateway route forwards to it. |++The threat model is an unauthenticated internet caller entering through the+gateway. The blocked cases can still matter if deployed network controls+change; `NR-004` would matter if diagnostics were enabled. A source scan should+find the command injection and dynamic evaluation. A deployment-aware result+should assess each workload and route separately. See+[the answer key](qa/expected-findings.json) for the required evidence.++## Local checks++From this directory:++```sh+python3 -m unittest discover -s qa -p 'test_*.py' -v+```++These checks confirm seeded code behavior and gateway routing. They do not+establish AWS reachability. To create and inspect the deployed topology, follow+[the AWS runbook](deployment/README.md). Keep `qa/` and the answer key outside+scanner source inputs.diff --git a/examples/network-reachability-sample/app/__init__.py b/examples/network-reachability-sample/app/__init__.pynew file mode 100644index 000000000..4a473f593--- /dev/null+++ b/examples/network-reachability-sample/app/__init__.py@@ -0,0 +1 @@+"""Synthetic network reachability benchmark source."""diff --git a/examples/network-reachability-sample/app/gateway.py b/examples/network-reachability-sample/app/gateway.pynew file mode 100644index 000000000..58bac1fcb--- /dev/null+++ b/examples/network-reachability-sample/app/gateway.py@@ -0,0 +1,47 @@+"""Public gateway with two fixed paths to the private benchmark workers."""++import json+import os+from http.server import BaseHTTPRequestHandler, HTTPServer+from urllib.error import URLError+from urllib.parse import urlsplit+from urllib.request import urlopen+++TARGETS = {+ "/open": os.environ.get("OPEN_WORKER_HOST"),+ "/blocked": os.environ.get("BLOCKED_WORKER_HOST"),+ "/acl-blocked": os.environ.get("ACL_BLOCKED_WORKER_HOST"),+}+++def route_request(path, query, targets=TARGETS, fetch=urlopen):+ if path == "/health":+ return 200, {"status": "ok"}+ host = targets.get(path)+ if host is None:+ return 404, {"error": "not_found"}+ address = f"http://{host}:8080/run"+ if query:+ address += f"?{query}"+ try:+ with fetch(address, timeout=2) as response:+ return response.status, json.load(response)+ except (TimeoutError, URLError):+ return 502, {"error": "upstream_unavailable"}+++class Handler(BaseHTTPRequestHandler):+ def do_GET(self):+ request = urlsplit(self.path)+ status, body = route_request(request.path, request.query)+ payload = json.dumps(body).encode()+ self.send_response(status)+ self.send_header("Content-Type", "application/json")+ self.send_header("Content-Length", str(len(payload)))+ self.end_headers()+ self.wfile.write(payload)+++if __name__ == "__main__":+ HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()diff --git a/examples/network-reachability-sample/app/worker.py b/examples/network-reachability-sample/app/worker.pynew file mode 100644index 000000000..d4222355f--- /dev/null+++ b/examples/network-reachability-sample/app/worker.py@@ -0,0 +1,49 @@+"""Intentionally vulnerable HTTP worker for a synthetic reachability benchmark."""++import json+import os+import subprocess+from http.server import BaseHTTPRequestHandler, HTTPServer+from urllib.parse import parse_qs, urlsplit+++def route_request(path, query, diagnostics_enabled=None):+ if diagnostics_enabled is None:+ diagnostics_enabled = os.environ.get("ENABLE_DIAGNOSTIC") == "1"+ if path == "/health":+ return 200, {"status": "ok"}+ if path == "/run":+ command = query.get("cmd", ["sample"])[0]+ # NR-001/002/003: the same command injection runs on three instances.+ result = subprocess.run(+ f"printf '%s\\n' {command}",+ shell=True,+ capture_output=True,+ text=True,+ timeout=2,+ check=False,+ )+ return 200, {"output": result.stdout, "exit_code": result.returncode}+ if path == "/diagnostic":+ if not diagnostics_enabled:+ return 404, {"error": "not_found"}+ expression = query.get("expr", ["1 + 1"])[0]+ # NR-004: deployed workers disable this route; the gateway also omits it.+ return 200, {"result": str(eval(expression))} # noqa: S307+ return 404, {"error": "not_found"}+++class Handler(BaseHTTPRequestHandler):+ def do_GET(self):+ request = urlsplit(self.path)+ status, body = route_request(request.path, parse_qs(request.query))+ payload = json.dumps(body).encode()+ self.send_response(status)+ self.send_header("Content-Type", "application/json")+ self.send_header("Content-Length", str(len(payload)))+ self.end_headers()+ self.wfile.write(payload)+++if __name__ == "__main__":+ HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()diff --git a/examples/network-reachability-sample/deployment/README.md b/examples/network-reachability-sample/deployment/README.mdnew file mode 100644index 000000000..164695021--- /dev/null+++ b/examples/network-reachability-sample/deployment/README.md@@ -0,0 +1,104 @@+# Deploy and score network reachability++Run this fixture only in an isolated AWS test account. The CloudFormation stack+creates four small EC2 instances, a VPC, a public subnet, two private subnets,+security groups, a network ACL, IAM roles, and an S3 gateway endpoint. EC2 and public IPv4+usage incur charges. The public gateway intentionally exposes a benign proof+path to vulnerable code. Keep real data and credentials out of the fixture.++## Deploy++Set a profile and Region for the test account and confirm the identity. Choose+a unique bucket and stack name. Run from the sample root:++```sh+export AWS_PROFILE=<test-profile>+export AWS_REGION=us-east-1+aws sts get-caller-identity+run_dir=$(mktemp -d "${TMPDIR:-/tmp}/reachability.XXXXXX")+artifact_bucket=<unique-test-bucket>+stack_name=<test-stack-name>+external_id=$(python3 -c 'import uuid; print(uuid.uuid4())')+python3 deployment/package.py "$run_dir"+aws s3 mb "s3://$artifact_bucket" --region "$AWS_REGION"+aws s3 cp "$run_dir/gateway.zip" "s3://$artifact_bucket/nr/gateway.zip"+aws s3 cp "$run_dir/worker.zip" "s3://$artifact_bucket/nr/worker.zip"+aws cloudformation deploy \+ --template-file deployment/stack.yaml \+ --stack-name "$stack_name" --capabilities CAPABILITY_IAM \+ --parameter-overrides \+ ArtifactBucket="$artifact_bucket" \+ GatewayKey=nr/gateway.zip WorkerKey=nr/worker.zip \+ ContinuumExternalId="$external_id"+aws cloudformation describe-stacks --stack-name "$stack_name" \+ > "$run_dir/stack.json"+```++Keep the run folder private. The three worker instances use the same S3 archive+and have no public addresses. Their private subnets have an S3 gateway endpoint+so boot scripts can fetch that archive without a NAT gateway. The gateway+forwards only `/open`, `/blocked`, and `/acl-blocked` to worker `/run`.++## Verify the deployed path++Read `GatewayUrl` from the stack outputs, then allow time for EC2 user data and+the worker services to start:++```sh+gateway_url=$(aws cloudformation describe-stacks --stack-name "$stack_name" \+ --query "Stacks[0].Outputs[?OutputKey=='GatewayUrl'].OutputValue | [0]" \+ --output text)+curl -sS -o "$run_dir/health.json" -w '%{http_code}\n' \+ "$gateway_url/health" > "$run_dir/health.status"+curl -sS -G -o "$run_dir/open.json" -w '%{http_code}\n' \+ --data-urlencode 'cmd=sample; printf BENCHMARK_PROOF' \+ "$gateway_url/open" > "$run_dir/open.status"+curl -sS -G -o "$run_dir/blocked.json" -w '%{http_code}\n' \+ --data-urlencode 'cmd=sample; printf BENCHMARK_PROOF' \+ "$gateway_url/blocked" > "$run_dir/blocked.status"+curl -sS -G -o "$run_dir/acl-blocked.json" -w '%{http_code}\n' \+ --data-urlencode 'cmd=sample; printf BENCHMARK_PROOF' \+ "$gateway_url/acl-blocked" > "$run_dir/acl-blocked.status"+curl -sS -o "$run_dir/diagnostic.json" -w '%{http_code}\n' \+ "$gateway_url/diagnostic?expr=1" > "$run_dir/diagnostic.status"+```++Expected responses after boot: `/open` contains `BENCHMARK_PROOF`,+`/blocked` and `/acl-blocked` return `upstream_unavailable`, and+`/diagnostic` returns `not_found`. All worker services explicitly set+`ENABLE_DIAGNOSTIC=0`; a+private caller also receives `not_found` for that route. Save response status+codes as well as bodies. A blocked response+alone is inconclusive: inspect deployed instance network interfaces, subnet+routes, security-group ingress, and network ACL rules to identify the denied+hop. Confirm all workers run the same archive hash. Save evidence only outside this public+repository.++## Run Continuum++Create an S3 code integration with base path `nr/` using the stack's+`ContinuumIntegrationRoleArn` output and the generated external ID. Create a+code environment and an AWS collector in the same test account and Region.+Update the stack with `ContinuumEnvironmentArn` and `ContinuumCollectorArn`,+then associate the collector using the new+`ContinuumCollectorDataAccessRoleArn` output. The association sequence is the+same as in [the SW-003 AWS runbook](../../security-workflow-sample/deployment/aws/README.md).++Record the collector association, scan ID, discovered EC2 and VPC resources,+finding-to-workload links, reachability assessment, and any runtime proof.+Compare them with [the answer key](../qa/expected-findings.json). If Continuum+cannot map the S3 archives to the EC2 instances, record a source-to-workload+mapping gap instead of treating code inference as deployed proof. Do not+collapse the three identical worker deployments into one exposure result.++## Clean up++After saving private evidence, disassociate and delete the Continuum test+environment, collector, and integration. Then delete the stack and bucket:++```sh+aws cloudformation delete-stack --stack-name "$stack_name"+aws cloudformation wait stack-delete-complete --stack-name "$stack_name"+aws s3 rm "s3://$artifact_bucket" --recursive+aws s3 rb "s3://$artifact_bucket"+```diff --git a/examples/network-reachability-sample/deployment/package.py b/examples/network-reachability-sample/deployment/package.pynew file mode 100644index 000000000..3de9eed67--- /dev/null+++ b/examples/network-reachability-sample/deployment/package.py@@ -0,0 +1,22 @@+"""Package the two code sources uploaded before stack deployment."""++import sys+from pathlib import Path+from zipfile import ZIP_DEFLATED, ZipFile++ROOT = Path(__file__).resolve().parents[1]+++def package(output_dir):+ output_dir.mkdir(parents=True, exist_ok=True)+ for name in ("gateway", "worker"):+ archive = output_dir / f"{name}.zip"+ with ZipFile(archive, "w", ZIP_DEFLATED) as bundle:+ bundle.write(ROOT / "app" / f"{name}.py", f"{name}.py")+ print(archive)+++if __name__ == "__main__":+ if len(sys.argv) != 2:+ raise SystemExit("usage: python3 deployment/package.py OUTPUT_DIR")+ package(Path(sys.argv[1]))diff --git a/examples/network-reachability-sample/deployment/stack.yaml b/examples/network-reachability-sample/deployment/stack.yamlnew file mode 100644index 000000000..f8bddbd05--- /dev/null+++ b/examples/network-reachability-sample/deployment/stack.yaml@@ -0,0 +1,440 @@+AWSTemplateFormatVersion: "2010-09-09"+Description: Synthetic network reachability benchmark with identical private RCE workers++Parameters:+ ArtifactBucket:+ Type: String+ GatewayKey:+ Type: String+ WorkerKey:+ Type: String+ ContinuumExternalId:+ Type: String+ NoEcho: true+ ContinuumEnvironmentArn:+ Type: String+ Default: ""+ ContinuumCollectorArn:+ Type: String+ Default: ""+ AmiId:+ Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>+ Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64+ InstanceType:+ Type: String+ Default: t3.micro++Conditions:+ HasCollectorAssociation: !And+ - !Not [!Equals [!Ref ContinuumEnvironmentArn, ""]]+ - !Not [!Equals [!Ref ContinuumCollectorArn, ""]]++Resources:+ Vpc:+ Type: AWS::EC2::VPC+ Properties:+ CidrBlock: 10.80.0.0/16+ EnableDnsHostnames: true+ EnableDnsSupport: true++ InternetGateway:+ Type: AWS::EC2::InternetGateway++ GatewayAttachment:+ Type: AWS::EC2::VPCGatewayAttachment+ Properties:+ VpcId: !Ref Vpc+ InternetGatewayId: !Ref InternetGateway++ PublicSubnet:+ Type: AWS::EC2::Subnet+ Properties:+ VpcId: !Ref Vpc+ CidrBlock: 10.80.0.0/24+ MapPublicIpOnLaunch: true++ PrivateSubnet:+ Type: AWS::EC2::Subnet+ Properties:+ VpcId: !Ref Vpc+ CidrBlock: 10.80.1.0/24+ MapPublicIpOnLaunch: false++ AclPrivateSubnet:+ Type: AWS::EC2::Subnet+ Properties:+ VpcId: !Ref Vpc+ CidrBlock: 10.80.2.0/24+ MapPublicIpOnLaunch: false++ PublicRoutes:+ Type: AWS::EC2::RouteTable+ Properties:+ VpcId: !Ref Vpc++ PublicDefaultRoute:+ Type: AWS::EC2::Route+ DependsOn: GatewayAttachment+ Properties:+ RouteTableId: !Ref PublicRoutes+ DestinationCidrBlock: 0.0.0.0/0+ GatewayId: !Ref InternetGateway++ PublicRouteAssociation:+ Type: AWS::EC2::SubnetRouteTableAssociation+ Properties:+ SubnetId: !Ref PublicSubnet+ RouteTableId: !Ref PublicRoutes++ PrivateRoutes:+ Type: AWS::EC2::RouteTable+ Properties:+ VpcId: !Ref Vpc++ PrivateRouteAssociation:+ Type: AWS::EC2::SubnetRouteTableAssociation+ Properties:+ SubnetId: !Ref PrivateSubnet+ RouteTableId: !Ref PrivateRoutes++ AclPrivateRouteAssociation:+ Type: AWS::EC2::SubnetRouteTableAssociation+ Properties:+ SubnetId: !Ref AclPrivateSubnet+ RouteTableId: !Ref PrivateRoutes++ AclPrivateNetworkAcl:+ Type: AWS::EC2::NetworkAcl+ Properties:+ VpcId: !Ref Vpc++ AclPrivateNetworkAclAssociation:+ Type: AWS::EC2::SubnetNetworkAclAssociation+ Properties:+ SubnetId: !Ref AclPrivateSubnet+ NetworkAclId: !Ref AclPrivateNetworkAcl++ AclDenyGatewayHttp:+ Type: AWS::EC2::NetworkAclEntry+ Properties:+ NetworkAclId: !Ref AclPrivateNetworkAcl+ RuleNumber: 100+ Protocol: 6+ RuleAction: deny+ Egress: false+ CidrBlock: 10.80.0.0/24+ PortRange:+ From: 8080+ To: 8080++ AclAllowOtherIngress:+ Type: AWS::EC2::NetworkAclEntry+ Properties:+ NetworkAclId: !Ref AclPrivateNetworkAcl+ RuleNumber: 200+ Protocol: -1+ RuleAction: allow+ Egress: false+ CidrBlock: 0.0.0.0/0++ AclAllowEgress:+ Type: AWS::EC2::NetworkAclEntry+ Properties:+ NetworkAclId: !Ref AclPrivateNetworkAcl+ RuleNumber: 100+ Protocol: -1+ RuleAction: allow+ Egress: true+ CidrBlock: 0.0.0.0/0++ S3Endpoint:+ Type: AWS::EC2::VPCEndpoint+ Properties:+ VpcId: !Ref Vpc+ ServiceName: !Sub com.amazonaws.${AWS::Region}.s3+ VpcEndpointType: Gateway+ RouteTableIds:+ - !Ref PrivateRoutes++ GatewaySecurityGroup:+ Type: AWS::EC2::SecurityGroup+ Properties:+ GroupDescription: Public benchmark gateway+ VpcId: !Ref Vpc+ SecurityGroupIngress:+ - IpProtocol: tcp+ FromPort: 8080+ ToPort: 8080+ CidrIp: 0.0.0.0/0++ OpenWorkerSecurityGroup:+ Type: AWS::EC2::SecurityGroup+ Properties:+ GroupDescription: Worker accepts HTTP from gateway security group+ VpcId: !Ref Vpc+ SecurityGroupIngress:+ - IpProtocol: tcp+ FromPort: 8080+ ToPort: 8080+ SourceSecurityGroupId: !Ref GatewaySecurityGroup++ BlockedWorkerSecurityGroup:+ Type: AWS::EC2::SecurityGroup+ Properties:+ GroupDescription: Worker with no inbound access+ VpcId: !Ref Vpc+ SecurityGroupIngress: []++ AclBlockedWorkerSecurityGroup:+ Type: AWS::EC2::SecurityGroup+ Properties:+ GroupDescription: Worker whose security group allows the gateway but network ACL denies it+ VpcId: !Ref Vpc+ SecurityGroupIngress:+ - IpProtocol: tcp+ FromPort: 8080+ ToPort: 8080+ SourceSecurityGroupId: !Ref GatewaySecurityGroup++ InstanceRole:+ Type: AWS::IAM::Role+ Properties:+ AssumeRolePolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Principal:+ Service: ec2.amazonaws.com+ Action: sts:AssumeRole+ Policies:+ - PolicyName: ReadBenchmarkSources+ PolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Action: s3:GetObject+ Resource:+ - !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}/${GatewayKey}+ - !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}/${WorkerKey}++ InstanceProfile:+ Type: AWS::IAM::InstanceProfile+ Properties:+ Roles:+ - !Ref InstanceRole++ ContinuumIntegrationRole:+ Type: AWS::IAM::Role+ Properties:+ Path: /continuum/integrations/+ AssumeRolePolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Principal:+ Service: continuum.amazonaws.com+ Action: sts:AssumeRole+ Condition:+ StringEquals:+ sts:ExternalId: !Ref ContinuumExternalId+ Policies:+ - PolicyName: SourceBucketAccess+ PolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Action: s3:ListBucket+ Resource: !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}+ - Effect: Allow+ Action:+ - s3:GetObject+ - s3:GetObjectVersion+ Resource: !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}/nr/*++ ContinuumCollectorDataAccessRole:+ Type: AWS::IAM::Role+ Condition: HasCollectorAssociation+ Properties:+ AssumeRolePolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Principal:+ Service: continuum.amazonaws.com+ Action: sts:AssumeRole+ Condition:+ StringEquals:+ aws:SourceAccount: !Ref AWS::AccountId+ ArnLike:+ aws:SourceArn: !Ref ContinuumEnvironmentArn+ Policies:+ - PolicyName: CollectorDataAccess+ PolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Action: continuum:GetCollectorDataAccessAuthorization+ Resource: !Ref ContinuumCollectorArn++ OpenWorker:+ Type: AWS::EC2::Instance+ DependsOn:+ - S3Endpoint+ - PrivateRouteAssociation+ Properties:+ ImageId: !Ref AmiId+ InstanceType: !Ref InstanceType+ IamInstanceProfile: !Ref InstanceProfile+ SubnetId: !Ref PrivateSubnet+ SecurityGroupIds:+ - !Ref OpenWorkerSecurityGroup+ MetadataOptions:+ HttpTokens: required+ UserData:+ Fn::Base64: !Sub |+ #!/bin/bash+ set -eux+ mkdir -p /opt/reachability+ aws s3 cp s3://${ArtifactBucket}/${WorkerKey} /tmp/worker.zip+ python3 -m zipfile -e /tmp/worker.zip /opt/reachability+ cat > /etc/systemd/system/reachability.service <<'UNIT'+ [Unit]+ Description=Synthetic reachability worker+ After=network-online.target+ [Service]+ Environment=ENABLE_DIAGNOSTIC=0+ ExecStart=/usr/bin/python3 /opt/reachability/worker.py+ Restart=always+ [Install]+ WantedBy=multi-user.target+ UNIT+ systemctl daemon-reload+ systemctl enable --now reachability.service++ BlockedWorker:+ Type: AWS::EC2::Instance+ DependsOn:+ - S3Endpoint+ - PrivateRouteAssociation+ Properties:+ ImageId: !Ref AmiId+ InstanceType: !Ref InstanceType+ IamInstanceProfile: !Ref InstanceProfile+ SubnetId: !Ref PrivateSubnet+ SecurityGroupIds:+ - !Ref BlockedWorkerSecurityGroup+ MetadataOptions:+ HttpTokens: required+ UserData:+ Fn::Base64: !Sub |+ #!/bin/bash+ set -eux+ mkdir -p /opt/reachability+ aws s3 cp s3://${ArtifactBucket}/${WorkerKey} /tmp/worker.zip+ python3 -m zipfile -e /tmp/worker.zip /opt/reachability+ cat > /etc/systemd/system/reachability.service <<'UNIT'+ [Unit]+ Description=Synthetic reachability worker+ After=network-online.target+ [Service]+ Environment=ENABLE_DIAGNOSTIC=0+ ExecStart=/usr/bin/python3 /opt/reachability/worker.py+ Restart=always+ [Install]+ WantedBy=multi-user.target+ UNIT+ systemctl daemon-reload+ systemctl enable --now reachability.service++ AclBlockedWorker:+ Type: AWS::EC2::Instance+ DependsOn:+ - S3Endpoint+ - AclPrivateRouteAssociation+ - AclPrivateNetworkAclAssociation+ - AclDenyGatewayHttp+ - AclAllowOtherIngress+ - AclAllowEgress+ Properties:+ ImageId: !Ref AmiId+ InstanceType: !Ref InstanceType+ IamInstanceProfile: !Ref InstanceProfile+ SubnetId: !Ref AclPrivateSubnet+ SecurityGroupIds:+ - !Ref AclBlockedWorkerSecurityGroup+ MetadataOptions:+ HttpTokens: required+ UserData:+ Fn::Base64: !Sub |+ #!/bin/bash+ set -eux+ mkdir -p /opt/reachability+ aws s3 cp s3://${ArtifactBucket}/${WorkerKey} /tmp/worker.zip+ python3 -m zipfile -e /tmp/worker.zip /opt/reachability+ cat > /etc/systemd/system/reachability.service <<'UNIT'+ [Unit]+ Description=Synthetic reachability worker+ After=network-online.target+ [Service]+ Environment=ENABLE_DIAGNOSTIC=0+ ExecStart=/usr/bin/python3 /opt/reachability/worker.py+ Restart=always+ [Install]+ WantedBy=multi-user.target+ UNIT+ systemctl daemon-reload+ systemctl enable --now reachability.service++ Gateway:+ Type: AWS::EC2::Instance+ DependsOn:+ - PublicDefaultRoute+ - PublicRouteAssociation+ Properties:+ ImageId: !Ref AmiId+ InstanceType: !Ref InstanceType+ IamInstanceProfile: !Ref InstanceProfile+ SubnetId: !Ref PublicSubnet+ SecurityGroupIds:+ - !Ref GatewaySecurityGroup+ MetadataOptions:+ HttpTokens: required+ UserData:+ Fn::Base64: !Sub |+ #!/bin/bash+ set -eux+ mkdir -p /opt/reachability+ aws s3 cp s3://${ArtifactBucket}/${GatewayKey} /tmp/gateway.zip+ python3 -m zipfile -e /tmp/gateway.zip /opt/reachability+ cat > /etc/systemd/system/reachability.service <<'UNIT'+ [Unit]+ Description=Synthetic public reachability gateway+ After=network-online.target+ [Service]+ Environment=OPEN_WORKER_HOST=${OpenWorker.PrivateIp}+ Environment=BLOCKED_WORKER_HOST=${BlockedWorker.PrivateIp}+ Environment=ACL_BLOCKED_WORKER_HOST=${AclBlockedWorker.PrivateIp}+ ExecStart=/usr/bin/python3 /opt/reachability/gateway.py+ Restart=always+ [Install]+ WantedBy=multi-user.target+ UNIT+ systemctl daemon-reload+ systemctl enable --now reachability.service++Outputs:+ GatewayUrl:+ Value: !Sub http://${Gateway.PublicIp}:8080+ GatewayInstanceId:+ Value: !Ref Gateway+ OpenWorkerInstanceId:+ Value: !Ref OpenWorker+ BlockedWorkerInstanceId:+ Value: !Ref BlockedWorker+ AclBlockedWorkerInstanceId:+ Value: !Ref AclBlockedWorker+ ContinuumIntegrationRoleArn:+ Value: !GetAtt ContinuumIntegrationRole.Arn+ ContinuumCollectorDataAccessRoleArn:+ Condition: HasCollectorAssociation+ Value: !GetAtt ContinuumCollectorDataAccessRole.Arndiff --git a/examples/network-reachability-sample/qa/expected-findings.json b/examples/network-reachability-sample/qa/expected-findings.jsonnew file mode 100644index 000000000..2ee19af20--- /dev/null+++ b/examples/network-reachability-sample/qa/expected-findings.json@@ -0,0 +1,75 @@+{+ "schemaVersion": 1,+ "threatModel": "Unauthenticated caller on the public internet, entering through the gateway instance",+ "sourceFindings": [+ {+ "id": "NR-001/002/003",+ "category": "OS command injection",+ "cwe": ["CWE-78"],+ "location": "app/worker.py:route_request:/run",+ "rootCause": "The cmd query parameter is interpolated into a subprocess shell command.",+ "expectedFix": "Pass fixed command arguments without a shell or reject shell syntax.",+ "deploymentAssessments": [+ {+ "id": "NR-001",+ "workload": "OpenWorker",+ "internetReachable": true,+ "path": "internet -> Gateway:8080 -> /open -> OpenWorker:8080 -> /run",+ "requiredEvidence": [+ "Gateway has a public address, internet route, and 0.0.0.0/0 ingress on port 8080.",+ "Gateway forwards /open to the OpenWorker private address and /run path.",+ "OpenWorker security group allows port 8080 from the Gateway security group.",+ "The worker service is running and a benign command injection proof succeeds through the public gateway."+ ]+ },+ {+ "id": "NR-002",+ "workload": "BlockedWorker",+ "internetReachable": false,+ "path": "internet -> Gateway:8080 -> /blocked -X-> BlockedWorker:8080",+ "requiredEvidence": [+ "The same worker source archive runs on BlockedWorker.",+ "Gateway forwards /blocked to the BlockedWorker private address and /run path.",+ "BlockedWorker security group has no inbound rule permitting traffic from Gateway.",+ "The gateway request fails; the failure alone does not prove the security group is the cause."+ ]+ },+ {+ "id": "NR-003",+ "workload": "AclBlockedWorker",+ "internetReachable": false,+ "path": "internet -> Gateway:8080 -> /acl-blocked -X-> AclBlockedWorker:8080",+ "requiredEvidence": [+ "The same worker source archive runs on AclBlockedWorker.",+ "Gateway forwards /acl-blocked to the AclBlockedWorker private address and /run path.",+ "AclBlockedWorker security group permits port 8080 from Gateway.",+ "Its subnet network ACL denies TCP port 8080 from the gateway subnet before a later allow rule.",+ "The gateway request fails; the failure alone does not prove the network ACL is the cause."+ ]+ }+ ]+ },+ {+ "id": "NR-004",+ "category": "Dynamic code evaluation",+ "cwe": ["CWE-94"],+ "location": "app/worker.py:route_request:/diagnostic",+ "rootCause": "The worker evaluates the expr query parameter as Python code.",+ "internetReachable": false,+ "requiredEvidence": [+ "All three deployed workers contain the vulnerable /diagnostic route.",+ "All worker services set ENABLE_DIAGNOSTIC=0, so the handler returns before evaluation.",+ "The public gateway only forwards /open, /blocked, and /acl-blocked to worker /run.",+ "No public request path controls the worker URL path."+ ],+ "caveat": "The route becomes exploitable if diagnostics are enabled and a caller can reach a worker."+ }+ ],+ "scoringRules": [+ "Report source vulnerabilities even when a specific deployment is unreachable.",+ "Assess reachability per workload; identical source code does not imply identical exposure.",+ "Evaluate subnet routes, security groups, and network ACL rules together.",+ "Do not claim that a failed HTTP request alone proves the denying network control.",+ "Do not claim deployed resource or runtime proof from source code or the template alone."+ ]+}diff --git a/examples/network-reachability-sample/qa/test_behavior.py b/examples/network-reachability-sample/qa/test_behavior.pynew file mode 100644index 000000000..1cae5e962--- /dev/null+++ b/examples/network-reachability-sample/qa/test_behavior.py@@ -0,0 +1,68 @@+"""Local behavior checks; AWS security-group claims require a deployed stack."""++import io+import json+import unittest++from app.gateway import route_request as gateway_request+from app.worker import route_request as worker_request+++class Response(io.BytesIO):+ status = 200++ def __enter__(self):+ return self++ def __exit__(self, *_args):+ self.close()+++class ReachabilityBehaviorTests(unittest.TestCase):+ def test_worker_command_input_executes_a_second_benign_command(self):+ status, body = worker_request("/run", {"cmd": ["sample; printf BENCHMARK_PROOF"]})+ self.assertEqual(status, 200)+ self.assertIn("BENCHMARK_PROOF", body["output"])++ def test_gateway_selects_only_fixed_worker_run_paths(self):+ requests = []++ def fetch(url, timeout):+ requests.append((url, timeout))+ return Response(json.dumps({"output": "sample"}).encode())++ targets = {+ "/open": "10.80.1.10",+ "/blocked": "10.80.1.11",+ "/acl-blocked": "10.80.2.10",+ }+ for path in ("/open", "/blocked", "/acl-blocked"):+ status, _ = gateway_request(path, "cmd=sample", targets, fetch)+ self.assertEqual(status, 200)+ self.assertEqual(+ requests,+ [+ ("http://10.80.1.10:8080/run?cmd=sample", 2),+ ("http://10.80.1.11:8080/run?cmd=sample", 2),+ ("http://10.80.2.10:8080/run?cmd=sample", 2),+ ],+ )+ status, _ = gateway_request("/diagnostic", "expr=1", targets, fetch)+ self.assertEqual(status, 404)+ self.assertEqual(len(requests), 3)++ def test_worker_diagnostic_code_is_executable_locally(self):+ status, body = worker_request(+ "/diagnostic", {"expr": ["40 + 2"]}, diagnostics_enabled=True+ )+ self.assertEqual(status, 200)+ self.assertEqual(body["result"], "42")+ status, body = worker_request(+ "/diagnostic", {"expr": ["40 + 2"]}, diagnostics_enabled=False+ )+ self.assertEqual(status, 404)+ self.assertEqual(body["error"], "not_found")+++if __name__ == "__main__":+ unittest.main()diff --git a/examples/security-workflow-sample/README.md b/examples/security-workflow-sample/README.mdnew file mode 100644index 000000000..cab587946--- /dev/null+++ b/examples/security-workflow-sample/README.md@@ -0,0 +1,60 @@+# Security workflow sample++This sample supplies **three benchmark examples** for an order API security+workflow: cross-customer order access, reporting SQL injection, and coverage+across a synthetic deployed environment. All identities, orders, and deployment+names are invented. The local API binds to `127.0.0.1`, stores data in memory,+and needs only Python 3.9 or later.++| Directory | Purpose |+| --- | --- |+| `app/` | Standalone source target with the two seeded flaws. |+| `qa/` | Expected findings and local behavior checks. Do not include in a source scan. |+| `deployment/` | Mock topology, two additional source roots, and a Lambda deployment template. |++## Run and verify the fixture++From this directory:++```bash+python3 -m unittest discover -s qa -p 'test_*.py' -v+python3 -m app.server+```++The server prints its loopback address. `app/README.md` lists the API routes and+synthetic bearer tokens. Stop and restart the process to reset its database.+The tests intentionally confirm vulnerable behavior; they are not a scanner+recall score or a secure post-fix test suite.++## Scan the two code findings together++Copy only `app/` into a fresh directory. Keep `qa/`, `deployment/`, and this+guide outside the scanner's source tree. For example, from this directory:++```bash+sample_run="$(mktemp -d "${TMPDIR:-/tmp}/security-workflow.XXXXXX")"+cp -R app "$sample_run/app"+cd "$sample_run"+codex-security scan app --mode standard --effort high --format json \+ --output-dir "$sample_run/scan"+```++This command uses the locally configured Codex Security CLI and model access.+Check the saved scan status and scope before comparing findings with+`qa/expected-findings.json`. A completed scan can report zero, one, two, or+additional findings; the fixture does not guarantee discovery.++## Exercise environment coverage++`deployment/inventory.json` models three source roots and their network entry+points. It is useful for checking expected reachability and negative controls+without an AWS account. An actual AWS Continuum exercise needs three connected+code sources, corresponding workloads in an authorized test account, a+collector, and saved scan evidence. The local inventory alone does not+establish what Continuum would find in a live environment. Follow the+[SW-003 AWS runbook](deployment/aws/README.md) to deploy the Lambda variant+and retain observed evidence.++Use [the QA guide](qa/README.md) for the expected scanner results and+disposition rules. No automatic merge runs as part of this sample; that step+requires a separately configured repository policy.diff --git a/examples/security-workflow-sample/app/README.md b/examples/security-workflow-sample/app/README.mdnew file mode 100644index 000000000..c9c1e15bd--- /dev/null+++ b/examples/security-workflow-sample/app/README.md@@ -0,0 +1,30 @@+# Order Report API++This small order service has three synthetic customer accounts and a regional+report. It runs on loopback with Python's standard library; no installation,+external service, or real customer data is needed.++From the directory containing `app/`, run:++```bash+python3 -m app.server+```++Set `PORT=0` to select an available port. Restarting resets the database.++Use `Authorization: Bearer demo-acme`, `demo-beacon`, or `demo-cedar` for the+matching customer. The routes are:++| Route | Response |+| --- | --- |+| `GET /health` | Process readiness |+| `GET /api/orders` | Signed-in customer's order list |+| `GET /api/orders/{id}` | One order |+| `GET /api/reports/orders?region=west` | Regional order report |++The fixed tokens and order records are sample data. The API makes no outbound+requests and writes no database files.++`lambda_handler.py` exposes the same routes through an API Gateway HTTP API+event. The [SW-003 AWS runbook](../deployment/aws/README.md) packages this+adapter with the order service for a test account.diff --git a/examples/security-workflow-sample/app/__init__.py b/examples/security-workflow-sample/app/__init__.pynew file mode 100644index 000000000..11369cebf--- /dev/null+++ b/examples/security-workflow-sample/app/__init__.py@@ -0,0 +1 @@+"""Synthetic order API for scanner evaluation."""diff --git a/examples/security-workflow-sample/app/data.py b/examples/security-workflow-sample/app/data.pynew file mode 100644index 000000000..c4596c0c5--- /dev/null+++ b/examples/security-workflow-sample/app/data.py@@ -0,0 +1,60 @@+"""Process-local order data for the sample API."""++import sqlite3++TOKENS = {+ "demo-acme": "acme",+ "demo-beacon": "beacon",+ "demo-cedar": "cedar",+}++ORDERS = [+ ("A-101", "acme", "west", "Acme-only notebook", 1200),+ ("A-102", "acme", "east", "Acme-only pen", 300),+ ("B-201", "beacon", "west", "Beacon-only lamp", 4200),+ ("B-202", "beacon", "east", "Beacon-only chair", 7800),+ ("C-301", "cedar", "west", "Cedar-only mug", 900),+ ("C-302", "cedar", "east", "Cedar-only desk", 11900),+]+++def create_database():+ database = sqlite3.connect(":memory:", check_same_thread=False)+ database.row_factory = sqlite3.Row+ database.execute(+ """+ CREATE TABLE orders (+ id TEXT PRIMARY KEY,+ tenant_id TEXT NOT NULL,+ region TEXT NOT NULL,+ description TEXT NOT NULL,+ amount_cents INTEGER NOT NULL+ )+ """+ )+ database.executemany(+ "INSERT INTO orders VALUES (?, ?, ?, ?, ?)",+ ORDERS,+ )+ database.commit()+ return database+++def list_orders(database, tenant_id):+ return database.execute(+ """+ SELECT id, tenant_id, region, description, amount_cents+ FROM orders WHERE tenant_id = ? ORDER BY id+ """,+ (tenant_id,),+ ).fetchall()+++def lookup_order(database, order_id):+ return database.execute(+ """+ SELECT id, tenant_id, region, description, amount_cents+ FROM orders WHERE id = ?+ """,+ (order_id,),+ ).fetchone()diff --git a/examples/security-workflow-sample/app/lambda_handler.py b/examples/security-workflow-sample/app/lambda_handler.pynew file mode 100644index 000000000..ce7b54144--- /dev/null+++ b/examples/security-workflow-sample/app/lambda_handler.py@@ -0,0 +1,31 @@+"""API Gateway HTTP API adapter for the synthetic order service."""++import json++from .data import create_database+from .server import route_request+++def handler(event, _context):+ method = event.get("requestContext", {}).get("http", {}).get("method", "GET")+ if method != "GET":+ return {+ "statusCode": 405,+ "headers": {"content-type": "application/json"},+ "body": json.dumps({"error": "method not allowed"}),+ }+ path = event.get("rawPath", "/")+ query = event.get("rawQueryString", "")+ if query:+ path = f"{path}?{query}"+ headers = {key.lower(): value for key, value in event.get("headers", {}).items()}+ database = create_database()+ try:+ status, body = route_request(path, headers.get("authorization", ""), database)+ finally:+ database.close()+ return {+ "statusCode": status,+ "headers": {"content-type": "application/json"},+ "body": json.dumps(body),+ }diff --git a/examples/security-workflow-sample/app/reports.py b/examples/security-workflow-sample/app/reports.pynew file mode 100644index 000000000..65d1222c1--- /dev/null+++ b/examples/security-workflow-sample/app/reports.py@@ -0,0 +1,11 @@+"""Order reports for the signed-in account."""+++def orders_by_region(database, tenant_id, region):+ query = f"""+ SELECT id, tenant_id, region, description, amount_cents+ FROM orders+ WHERE tenant_id = ? AND region = '{region}'+ ORDER BY id+ """+ return database.execute(query, (tenant_id,)).fetchall()diff --git a/examples/security-workflow-sample/app/server.py b/examples/security-workflow-sample/app/server.pynew file mode 100644index 000000000..1317a845b--- /dev/null+++ b/examples/security-workflow-sample/app/server.py@@ -0,0 +1,76 @@+"""Loopback HTTP entry point for the order API."""++import json+import os+from http.server import BaseHTTPRequestHandler, HTTPServer+from urllib.parse import parse_qs, unquote, urlsplit++from .data import TOKENS, create_database, list_orders, lookup_order+from .reports import orders_by_region+++class OrderServer(HTTPServer):+ def __init__(self, port=0):+ self.database = create_database()+ super().__init__(("127.0.0.1", port), OrderHandler)++ def server_close(self):+ super().server_close()+ self.database.close()+++class OrderHandler(BaseHTTPRequestHandler):+ def log_message(self, format_string, *args):+ pass++ def reply(self, status, body):+ encoded = json.dumps(body).encode("utf-8")+ self.send_response(status)+ self.send_header("Content-Type", "application/json")+ self.send_header("Content-Length", str(len(encoded)))+ self.end_headers()+ self.wfile.write(encoded)++ def do_GET(self):+ status, body = route_request(+ self.path, self.headers.get("Authorization", ""), self.server.database+ )+ self.reply(status, body)+++def route_request(path, authorization, database):+ target = urlsplit(path)+ if target.path == "/health":+ return 200, {"status": "ok"}++ token = authorization.removeprefix("Bearer ")+ tenant_id = TOKENS.get(token)+ if not authorization.startswith("Bearer ") or tenant_id is None:+ return 401, {"error": "unauthorized"}++ if target.path == "/api/orders":+ return 200, {"orders": [dict(row) for row in list_orders(database, tenant_id)]}+ if target.path.startswith("/api/orders/"):+ order_id = unquote(target.path.removeprefix("/api/orders/"))+ order = lookup_order(database, order_id)+ if order is None:+ return 404, {"error": "order not found"}+ return 200, {"order": dict(order)}+ if target.path == "/api/reports/orders":+ region = parse_qs(target.query).get("region", [""])[0]+ rows = orders_by_region(database, tenant_id, region)+ return 200, {"orders": [dict(row) for row in rows]}+ return 404, {"error": "not found"}+++def create_server(port=0):+ return OrderServer(port)+++if __name__ == "__main__":+ server = create_server(int(os.environ.get("PORT", "4312")))+ print(f"http://127.0.0.1:{server.server_port}", flush=True)+ try:+ server.serve_forever()+ finally:+ server.server_close()diff --git a/examples/security-workflow-sample/deployment/README.md b/examples/security-workflow-sample/deployment/README.mdnew file mode 100644index 000000000..9349b2a19--- /dev/null+++ b/examples/security-workflow-sample/deployment/README.md@@ -0,0 +1,22 @@+# Mock deployment context++`inventory.json` is a synthetic description of three deployed revisions. It+is **not** an AWS Continuum API response or proof of a live AWS deployment.+It lets reviewers check whether a claimed reachability path is supported by+the supplied topology.++The order API has a public ingress to `/api/*`. Each route still requires an+account token. The billing API is private, and its one business function uses+the authorized tenant. The analytics worker has no HTTP listener or ingress;+its unused `legacy_region_export` function is a negative control for+reachability claims.++For an actual AWS Continuum evaluation, create three code sources and matching+workloads in an authorized test account, attach a collector to the environment,+and record the real deployment revision and scan evidence. The AWS runbook uses+Lambda resources, while this mock inventory names EKS resources. Keep the+environment response separate from the scanner input and from the QA answer+key.++Use [the AWS test-account runbook](aws/README.md) to deploy the Lambda variant+and record observed results.diff --git a/examples/security-workflow-sample/deployment/aws/README.md b/examples/security-workflow-sample/deployment/aws/README.mdnew file mode 100644index 000000000..08dfe0bd1--- /dev/null+++ b/examples/security-workflow-sample/deployment/aws/README.md@@ -0,0 +1,189 @@+# SW-003 in an AWS test account++Use a separate test account containing only synthetic data. This directory+deploys the three workloads as Lambda functions in `us-east-1`. The order+function has a public API Gateway HTTP API. Billing is callable only through+IAM, and analytics has an EventBridge schedule without an HTTP route. The+EKS-based `deployment/inventory.json` remains a mock fixture, not an AWS+export.++## Prepare the account++Choose an AWS CLI profile explicitly. Keep the account ID and evidence in a+private run folder outside this public repository:++```sh+export AWS_PROFILE=<test-account-profile>+export AWS_REGION=us-east-1+export EXPECTED_ACCOUNT_ID=<test-account-id>+test "$(aws sts get-caller-identity --query Account --output text)" = \+ "$EXPECTED_ACCOUNT_ID"+aws --version+```++Verify that the profile resolves to the intended account before creating+anything. The finding export commands in `docs/scanner-imports/continuum.md`+need AWS CLI 2.38.5 or later; deployment and a console-based scan do not.++## Package and deploy++From the sample root, choose a new globally unique S3 bucket name and a new+CloudFormation stack name. The bucket stores the three source archives that+Lambda runs. Keep the run folder outside the checkout.++```sh+run_dir=$(mktemp -d "${TMPDIR:-/tmp}/sw003.XXXXXX")+artifact_bucket=<new-globally-unique-bucket>+stack_name=<new-stack-name>+integration_external_id=$(python3 -c 'import uuid; print(uuid.uuid4())')+printf '%s\n' "$integration_external_id" > "$run_dir/integration-external-id.txt"+python3 deployment/aws/package.py "$run_dir"+shasum -a 256 "$run_dir"/*.zip > "$run_dir/source-sha256.txt"+aws s3 mb "s3://$artifact_bucket" --region "$AWS_REGION"+for name in order billing analytics; do+ aws s3 cp "$run_dir/$name.zip" "s3://$artifact_bucket/sw003/$name.zip"+done+aws cloudformation deploy \+ --template-file deployment/aws/lambda-stack.yaml \+ --stack-name "$stack_name" --capabilities CAPABILITY_IAM \+ --parameter-overrides \+ ArtifactBucket="$artifact_bucket" \+ OrderKey=sw003/order.zip BillingKey=sw003/billing.zip \+ AnalyticsKey=sw003/analytics.zip \+ ContinuumExternalId="$integration_external_id"+aws cloudformation describe-stacks --stack-name "$stack_name" \+ --output json > "$run_dir/stack.json"+aws cloudformation describe-stack-resources --stack-name "$stack_name" \+ --output json > "$run_dir/resources.json"+```++`package.py` archives only `app/`, the billing module, and the analytics+module. It excludes `qa/` and the answer key. Record the archive hashes, the+three Lambda ARNs, API ID and route, and EventBridge rule from the stack+responses. The stack also outputs a Continuum S3 integration role ARN.++## Verify behavior++Get the `OrderApiUrl` output from `stack.json`. Request `/health`, then use the+synthetic `demo-acme` token for the two proof requests:++```sh+api_url=$(aws cloudformation describe-stacks --stack-name "$stack_name" \+ --query "Stacks[0].Outputs[?OutputKey=='OrderApiUrl'].OutputValue | [0]" \+ --output text)+api_id=$(aws cloudformation describe-stack-resources --stack-name "$stack_name" \+ --query "StackResources[?LogicalResourceId=='OrderApi'].PhysicalResourceId | [0]" \+ --output text)+billing_arn=$(aws cloudformation describe-stacks --stack-name "$stack_name" \+ --query "Stacks[0].Outputs[?OutputKey=='BillingFunctionArn'].OutputValue | [0]" \+ --output text)+analytics_arn=$(aws cloudformation describe-stacks --stack-name "$stack_name" \+ --query "Stacks[0].Outputs[?OutputKey=='AnalyticsFunctionArn'].OutputValue | [0]" \+ --output text)+aws apigatewayv2 get-routes --api-id "$api_id" > "$run_dir/api-routes.json"+aws apigatewayv2 get-integrations --api-id "$api_id" > "$run_dir/api-integrations.json"+aws lambda list-function-url-configs --function-name "$billing_arn" \+ > "$run_dir/billing-urls.json"+aws lambda list-function-url-configs --function-name "$analytics_arn" \+ > "$run_dir/analytics-urls.json"+curl -sS -o "$run_dir/health.json" -w '%{http_code}\n' \+ "$api_url/health" > "$run_dir/health-status.txt"+curl -sS -o "$run_dir/order-proof.json" -w '%{http_code}\n' \+ -H 'Authorization: Bearer demo-acme' \+ "$api_url/api/orders/B-201" > "$run_dir/order-status.txt"+curl -sS -G -o "$run_dir/report-proof.json" -w '%{http_code}\n' \+ -H 'Authorization: Bearer demo-acme' \+ --data-urlencode "region=absent' OR 1=1 --" \+ "$api_url/api/reports/orders" > "$run_dir/report-status.txt"+```++Check the status codes and JSON before marking either proof as observed.+`order-proof.json` should contain the Beacon order, and `report-proof.json`+should contain rows for all three synthetic tenants. Confirm from the stack+resources and saved AWS responses that neither billing nor analytics has an+API route or Lambda Function URL. Limit that network claim to this sample+stack.++## Run Continuum and score the result++In the Continuum console for the same account and Region, create an S3+integration using the new bucket, the stack's `ContinuumIntegrationRoleArn`+output, and the external ID saved in the private run folder. Set the source+code base path to `sw003/`. Create a code environment from that integration.+Create or select a collector for the test account. Update the stack with the+same parameter values plus `ContinuumEnvironmentArn` and+`ContinuumCollectorArn`. This creates a data-access role whose trust policy+is scoped to the environment and whose permission is scoped to the collector.+Then associate the collector:++```sh+environment_id=<new-environment-id>+environment_arn=<new-environment-arn>+collector_arn=<new-collector-arn>+aws cloudformation deploy \+ --template-file deployment/aws/lambda-stack.yaml \+ --stack-name "$stack_name" --capabilities CAPABILITY_IAM \+ --parameter-overrides \+ ArtifactBucket="$artifact_bucket" \+ OrderKey=sw003/order.zip BillingKey=sw003/billing.zip \+ AnalyticsKey=sw003/analytics.zip \+ ContinuumExternalId="$integration_external_id" \+ ContinuumEnvironmentArn="$environment_arn" \+ ContinuumCollectorArn="$collector_arn"+data_access_role_arn=$(aws cloudformation describe-stacks \+ --stack-name "$stack_name" \+ --query "Stacks[0].Outputs[?OutputKey=='ContinuumCollectorDataAccessRoleArn'].OutputValue | [0]" \+ --output text)+aws continuum associate-collector \+ --environment-id "$environment_id" \+ --collector-arn "$collector_arn" \+ --collector-data-access-role-arn "$data_access_role_arn"+aws continuum list-associated-collectors --environment-id "$environment_id"+```++An initial scan may start when the environment is created. Check+`aws continuum list-code-scans --environment-id "$environment_id"` before+requesting another scan. Confirm that all three archives were ingested;+record any source that was not.++Save the environment ID, scan ID and status, collector association, source+revisions, finding details, and any validation result in the private run+folder. Use `docs/scanner-imports/continuum.md` for finding export commands.++Compare observed results with `qa/expected-findings.json`:++- `SW-001`: the public order route is reachable after authentication. A+ `demo-acme` request for `B-201` returns a Beacon order in the vulnerable+ deployment. Record the actual request and response before claiming runtime+ proof.+- `SW-002`: the public report route reaches the SQL query. A synthetic+ `region` value can return rows from other tenants. Keep the query and result+ as private evidence.+- `SW-003`: all three deployed resources and source revisions are accounted+ for. The billing entry point is private, and the analytics worker has no+ HTTP route. Do not infer a live route from its unused+ `legacy_region_export` function.++Report code findings, network context, and runtime validation separately. If+Continuum cannot associate an archive with its Lambda deployment, record that+as a mapping gap. A missing finding is a measured miss, not proof that the code+is safe. The sample does not configure automatic merging.++## Clean up++After saving the private run record, remove the sample environment, collector,+integration, stack, and new artifact bucket. Remove Continuum resources in the+console or with the CLI; delete the stack after the environment no longer uses+its IAM roles:++```sh+aws continuum disassociate-collector \+ --environment-id "$environment_id" --collector-arn "$collector_arn"+aws continuum delete-environment --environment-id "$environment_id"+aws continuum delete-collector --collector-id <new-collector-id>+aws continuum delete-integration --integration-id <new-integration-id>+aws cloudformation delete-stack --stack-name "$stack_name"+aws cloudformation wait stack-delete-complete --stack-name "$stack_name"+aws s3 rm "s3://$artifact_bucket" --recursive+aws s3 rb "s3://$artifact_bucket"+```diff --git a/examples/security-workflow-sample/deployment/aws/lambda-stack.yaml b/examples/security-workflow-sample/deployment/aws/lambda-stack.yamlnew file mode 100644index 000000000..196216547--- /dev/null+++ b/examples/security-workflow-sample/deployment/aws/lambda-stack.yaml@@ -0,0 +1,208 @@+AWSTemplateFormatVersion: "2010-09-09"+Description: Synthetic SW-003 Lambda workloads in an isolated test account++Parameters:+ ArtifactBucket:+ Type: String+ OrderKey:+ Type: String+ BillingKey:+ Type: String+ AnalyticsKey:+ Type: String+ ContinuumExternalId:+ Type: String+ NoEcho: true+ ContinuumEnvironmentArn:+ Type: String+ Default: ""+ ContinuumCollectorArn:+ Type: String+ Default: ""++Conditions:+ HasCollectorAssociation: !And+ - !Not [!Equals [!Ref ContinuumEnvironmentArn, ""]]+ - !Not [!Equals [!Ref ContinuumCollectorArn, ""]]++Resources:+ ExecutionRole:+ Type: AWS::IAM::Role+ Properties:+ AssumeRolePolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Principal:+ Service: lambda.amazonaws.com+ Action: sts:AssumeRole+ ManagedPolicyArns:+ - !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole++ ContinuumIntegrationRole:+ Type: AWS::IAM::Role+ Properties:+ Path: /continuum/integrations/+ AssumeRolePolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Principal:+ Service: continuum.amazonaws.com+ Action: sts:AssumeRole+ Condition:+ StringEquals:+ sts:ExternalId: !Ref ContinuumExternalId+ Policies:+ - PolicyName: SourceBucketAccess+ PolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Action: s3:ListBucket+ Resource: !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}+ - Effect: Allow+ Action:+ - s3:GetObject+ - s3:GetObjectVersion+ Resource:+ - !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}/sw003/*+ - !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}/context/*+ - Effect: Allow+ Action:+ - s3:PutObject+ - s3:GetObject+ - s3:GetObjectVersion+ Resource: !Sub arn:${AWS::Partition}:s3:::${ArtifactBucket}/continuum-patches/*++ ContinuumCollectorDataAccessRole:+ Type: AWS::IAM::Role+ Condition: HasCollectorAssociation+ Properties:+ AssumeRolePolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Principal:+ Service: continuum.amazonaws.com+ Action: sts:AssumeRole+ Condition:+ StringEquals:+ aws:SourceAccount: !Ref AWS::AccountId+ ArnLike:+ aws:SourceArn: !Ref ContinuumEnvironmentArn+ Policies:+ - PolicyName: CollectorDataAccess+ PolicyDocument:+ Version: "2012-10-17"+ Statement:+ - Effect: Allow+ Action: continuum:GetCollectorDataAccessAuthorization+ Resource: !Ref ContinuumCollectorArn++ OrderFunction:+ Type: AWS::Lambda::Function+ Properties:+ Runtime: python3.12+ Handler: app.lambda_handler.handler+ Role: !GetAtt ExecutionRole.Arn+ Code:+ S3Bucket: !Ref ArtifactBucket+ S3Key: !Ref OrderKey+ Timeout: 5+ MemorySize: 128++ BillingFunction:+ Type: AWS::Lambda::Function+ Properties:+ Runtime: python3.12+ Handler: service.handler+ Role: !GetAtt ExecutionRole.Arn+ Code:+ S3Bucket: !Ref ArtifactBucket+ S3Key: !Ref BillingKey+ Environment:+ Variables:+ AUTHORIZED_TENANT_ID: acme+ Timeout: 5+ MemorySize: 128++ AnalyticsFunction:+ Type: AWS::Lambda::Function+ Properties:+ Runtime: python3.12+ Handler: worker.handler+ Role: !GetAtt ExecutionRole.Arn+ Code:+ S3Bucket: !Ref ArtifactBucket+ S3Key: !Ref AnalyticsKey+ Timeout: 5+ MemorySize: 128++ OrderApi:+ Type: AWS::ApiGatewayV2::Api+ Properties:+ Name: sw003-order-api+ ProtocolType: HTTP++ OrderIntegration:+ Type: AWS::ApiGatewayV2::Integration+ Properties:+ ApiId: !Ref OrderApi+ IntegrationType: AWS_PROXY+ IntegrationUri: !GetAtt OrderFunction.Arn+ PayloadFormatVersion: "2.0"++ OrderRoute:+ Type: AWS::ApiGatewayV2::Route+ Properties:+ ApiId: !Ref OrderApi+ RouteKey: $default+ Target: !Join ["/", [integrations, !Ref OrderIntegration]]++ OrderStage:+ Type: AWS::ApiGatewayV2::Stage+ Properties:+ ApiId: !Ref OrderApi+ StageName: $default+ AutoDeploy: true++ OrderInvokePermission:+ Type: AWS::Lambda::Permission+ Properties:+ Action: lambda:InvokeFunction+ FunctionName: !Ref OrderFunction+ Principal: apigateway.amazonaws.com+ SourceArn: !Sub arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${OrderApi}/*/*++ AnalyticsSchedule:+ Type: AWS::Events::Rule+ Properties:+ ScheduleExpression: rate(1 day)+ State: ENABLED+ Targets:+ - Arn: !GetAtt AnalyticsFunction.Arn+ Id: analytics++ AnalyticsInvokePermission:+ Type: AWS::Lambda::Permission+ Properties:+ Action: lambda:InvokeFunction+ FunctionName: !Ref AnalyticsFunction+ Principal: events.amazonaws.com+ SourceArn: !GetAtt AnalyticsSchedule.Arn++Outputs:+ OrderApiUrl:+ Value: !Sub https://${OrderApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}+ OrderFunctionArn:+ Value: !GetAtt OrderFunction.Arn+ BillingFunctionArn:+ Value: !GetAtt BillingFunction.Arn+ AnalyticsFunctionArn:+ Value: !GetAtt AnalyticsFunction.Arn+ ContinuumIntegrationRoleArn:+ Value: !GetAtt ContinuumIntegrationRole.Arn+ ContinuumCollectorDataAccessRoleArn:+ Condition: HasCollectorAssociation+ Value: !GetAtt ContinuumCollectorDataAccessRole.Arndiff --git a/examples/security-workflow-sample/deployment/aws/package.py b/examples/security-workflow-sample/deployment/aws/package.pynew file mode 100644index 000000000..feeca53f4--- /dev/null+++ b/examples/security-workflow-sample/deployment/aws/package.py@@ -0,0 +1,41 @@+"""Package only the three source roots used by the Lambda deployment."""++import sys+from pathlib import Path+from zipfile import ZIP_DEFLATED, ZipFile++ROOT = Path(__file__).resolve().parents[2]+ARTIFACTS = {+ "order": {+ "app/__init__.py": ROOT / "app/__init__.py",+ "app/data.py": ROOT / "app/data.py",+ "app/reports.py": ROOT / "app/reports.py",+ "app/server.py": ROOT / "app/server.py",+ "app/lambda_handler.py": ROOT / "app/lambda_handler.py",+ },+ "billing": {+ "service.py": ROOT / "deployment/services/billing/service.py",+ },+ "analytics": {+ "worker.py": ROOT / "deployment/services/analytics/worker.py",+ },+}+++def package(output_dir):+ output_dir.mkdir(parents=True, exist_ok=True)+ archives = []+ for name, files in ARTIFACTS.items():+ archive = output_dir / f"{name}.zip"+ with ZipFile(archive, "w", ZIP_DEFLATED) as bundle:+ for archive_path, source in files.items():+ bundle.write(source, archive_path)+ archives.append(archive)+ return archives+++if __name__ == "__main__":+ if len(sys.argv) != 2:+ raise SystemExit("usage: python3 deployment/aws/package.py OUTPUT_DIR")+ for path in package(Path(sys.argv[1])):+ print(path)diff --git a/examples/security-workflow-sample/deployment/inventory.json b/examples/security-workflow-sample/deployment/inventory.jsonnew file mode 100644index 000000000..7b8788d14--- /dev/null+++ b/examples/security-workflow-sample/deployment/inventory.json@@ -0,0 +1,38 @@+{+ "schemaVersion": 1,+ "kind": "synthetic-deployment-inventory",+ "environment": "sample-production",+ "services": [+ {+ "name": "order-report-api",+ "source": "app",+ "revision": "order-report-r1",+ "deployment": "eks/order-report",+ "ingress": {+ "internetExposed": true,+ "route": "/api/*",+ "authentication": "bearer-token",+ "target": "eks/order-report"+ }+ },+ {+ "name": "billing-api",+ "source": "deployment/services/billing",+ "revision": "billing-r1",+ "deployment": "eks/billing",+ "ingress": {+ "internetExposed": false,+ "route": "/api/balance",+ "authentication": "bearer-token",+ "target": "eks/billing"+ }+ },+ {+ "name": "analytics-worker",+ "source": "deployment/services/analytics",+ "revision": "analytics-r1",+ "deployment": "scheduled/analytics",+ "ingress": null+ }+ ]+}diff --git a/examples/security-workflow-sample/deployment/services/analytics/worker.py b/examples/security-workflow-sample/deployment/services/analytics/worker.pynew file mode 100644index 000000000..cb403ef02--- /dev/null+++ b/examples/security-workflow-sample/deployment/services/analytics/worker.py@@ -0,0 +1,29 @@+"""Synthetic scheduled analytics worker with no HTTP listener."""++import sqlite3+++def summarize_orders(database, tenant_id):+ return database.execute(+ "SELECT COUNT(*) FROM orders WHERE tenant_id = ?",+ (tenant_id,),+ ).fetchone()[0]+++def legacy_region_export(database, region):+ query = f"SELECT * FROM orders WHERE region = '{region}'"+ return database.execute(query).fetchall()+++def handler(_event, _context):+ """Scheduled Lambda entry point that never calls the legacy export."""+ database = sqlite3.connect(":memory:")+ try:+ database.execute("CREATE TABLE orders (tenant_id TEXT, region TEXT)")+ database.executemany(+ "INSERT INTO orders VALUES (?, ?)",+ [("acme", "west"), ("acme", "east"), ("beacon", "west")],+ )+ return {"acme_order_count": summarize_orders(database, "acme")}+ finally:+ database.close()diff --git a/examples/security-workflow-sample/deployment/services/billing/service.py b/examples/security-workflow-sample/deployment/services/billing/service.pynew file mode 100644index 000000000..9245d7972--- /dev/null+++ b/examples/security-workflow-sample/deployment/services/billing/service.py@@ -0,0 +1,21 @@+"""Synthetic internal balance service."""++import os++BALANCES = {+ "acme": 500,+ "beacon": 800,+ "cedar": 1200,+}+++def balance_for_account(authorized_tenant_id):+ return {+ "tenant_id": authorized_tenant_id,+ "balance_cents": BALANCES[authorized_tenant_id],+ }+++def handler(_event, _context):+ """IAM-only Lambda entry point; tenant comes from deployment configuration."""+ return balance_for_account(os.environ.get("AUTHORIZED_TENANT_ID", "acme"))diff --git a/examples/security-workflow-sample/qa/README.md b/examples/security-workflow-sample/qa/README.mdnew file mode 100644index 000000000..96e3a2c48--- /dev/null+++ b/examples/security-workflow-sample/qa/README.md@@ -0,0 +1,45 @@+# Security workflow sample QA++This directory is the answer key. Keep it, `deployment/`, and the top-level+guide outside a source-only scan of `app/`.++## Expected scanner results++| ID | Root cause and proof | Expected disposition |+| --- | --- | --- |+| `SW-001` | `GET /api/orders/{id}` authenticates the caller, then looks up an order by ID without comparing its tenant. `demo-acme` reads all four seeded Beacon and Cedar orders. The list endpoint remains correctly scoped. | Propose a tenant ownership check. Send the authorization change to developer review. |+| `SW-002` | `GET /api/reports/orders?region=...` passes user input into SQL text. A crafted synthetic region changes the predicate and returns all three tenants' rows. Normal `west` filtering returns only Acme's row. | Bind both tenant and region values. Permit automatic merge only if an explicitly configured bot policy accepts the diff and all required tests pass. |+| `SW-003` | The mock inventory names three deployed source roots. The order API is public at the network edge but authenticated; billing is private; analytics has no HTTP route. | Report inventory coverage and supported reachability. Do not claim the unused analytics export is publicly reachable or invent a billing finding. |++`SW-001` and `SW-002` should appear as separate findings from one scan of the+order API. A source-only scanner can infer the code paths and suggest runtime+tests. It cannot claim to have observed the mock deployment or sandbox responses+unless it has actually consumed and tested that evidence.++The `policy_gated_merge` label is a benchmark decision, not a claim that Codex+Security or AWS Continuum automatically merges pull requests. Likewise,+`inventory.json` is a mock fixture, not a real Continuum result.++## Run behavior checks++From the sample root:++```bash+python3 -m unittest discover -s qa -p 'test_*.py' -v+```++The tests start fresh loopback servers, prove the two seeded weaknesses, check+normal and anonymous behavior, and validate the three-source inventory. Passing+tests mean the **vulnerable fixture** behaves as intended. They do not mean a+scanner detected either issue, produced a repair, or validated a real AWS+deployment. The detailed answer key is in `expected-findings.json`.++`test_aws_adapters.py` also checks the Lambda entry points and confirms the+deployment archives exclude the answer key. These tests do not create AWS+resources. If the test environment disallows loopback sockets, run the adapter,+query, request-handler, and inventory cases separately.++To score a real scan, record the scanner version, input revision, scan status,+source scope, findings, proof artifacts, patches, test output, and review/merge+decision. Match findings by root cause and affected route; track duplicates,+misses, uncertain cases, and unexpected findings separately.diff --git a/examples/security-workflow-sample/qa/expected-findings.json b/examples/security-workflow-sample/qa/expected-findings.jsonnew file mode 100644index 000000000..b3ae40655--- /dev/null+++ b/examples/security-workflow-sample/qa/expected-findings.json@@ -0,0 +1,52 @@+{+ "schemaVersion": 1,+ "application": "../app",+ "findings": [+ {+ "id": "SW-001",+ "category": "Broken object authorization",+ "cwe": ["CWE-639"],+ "title": "Order lookup omits customer ownership",+ "locations": [+ {"path": "data.py", "symbol": "lookup_order"},+ {"path": "server.py", "symbol": "route_request"}+ ],+ "boundary": "An authenticated customer reads another customer's order by ID.",+ "expectedEvidence": "The unscoped lookup returns both beacon and cedar orders to acme.",+ "expectedFix": "Enforce tenant ownership in the lookup while preserving same-tenant reads.",+ "disposition": "developer_review"+ },+ {+ "id": "SW-002",+ "category": "SQL injection",+ "cwe": ["CWE-89"],+ "title": "Reporting query interpolates the region parameter",+ "locations": [+ {"path": "reports.py", "symbol": "orders_by_region"},+ {"path": "server.py", "symbol": "route_request"}+ ],+ "boundary": "A signed-in customer changes the report predicate and sees another customer's rows.",+ "expectedEvidence": "A synthetic region value returns rows outside the caller's tenant.",+ "expectedFix": "Bind tenant and region as query parameters while retaining ordinary region filtering.",+ "disposition": "policy_gated_merge"+ }+ ],+ "environmentCheck": {+ "id": "SW-003",+ "expectedCoverage": [+ "order-report-api",+ "billing-api",+ "analytics-worker"+ ],+ "expectedReachability": {+ "order-report-api": "public_authenticated",+ "billing-api": "private",+ "analytics-worker": "no_http_route"+ },+ "expectedFindingIds": ["SW-001", "SW-002"],+ "negativeControls": [+ "No unsubstantiated finding for the scoped billing balance function.",+ "Do not describe analytics legacy_region_export as reachable."+ ]+ }+}diff --git a/examples/security-workflow-sample/qa/test_aws_adapters.py b/examples/security-workflow-sample/qa/test_aws_adapters.pynew file mode 100644index 000000000..0324ae924--- /dev/null+++ b/examples/security-workflow-sample/qa/test_aws_adapters.py@@ -0,0 +1,67 @@+"""Checks for the entry points used by the AWS Lambda deployment."""++import json+import sys+import tempfile+import unittest+from pathlib import Path+from unittest.mock import patch+from urllib.parse import quote+from zipfile import ZipFile++ROOT = Path(__file__).resolve().parents[1]+sys.path.insert(0, str(ROOT))++from app.lambda_handler import handler as order_handler+from deployment.aws.package import package+from deployment.services.analytics.worker import handler as analytics_handler+from deployment.services.billing.service import handler as billing_handler+++class LambdaEntryPoints(unittest.TestCase):+ def get(self, path, token=None, query=""):+ headers = {"authorization": f"Bearer {token}"} if token else {}+ response = order_handler(+ {+ "rawPath": path,+ "rawQueryString": query,+ "headers": headers,+ "requestContext": {"http": {"method": "GET"}},+ },+ None,+ )+ return response["statusCode"], json.loads(response["body"])++ def test_order_api_preserves_synthetic_findings(self):+ self.assertEqual(self.get("/api/orders")[0], 401)+ self.assertEqual(+ self.get("/api/orders", "demo-acme")[1]["orders"][0]["id"], "A-101"+ )+ status, other = self.get("/api/orders/B-201", "demo-acme")+ self.assertEqual(status, 200)+ self.assertEqual(other["order"]["tenant_id"], "beacon")+ query = "region=" + quote("absent' OR 1=1 --", safe="")+ status, report = self.get("/api/reports/orders", "demo-acme", query)+ self.assertEqual(status, 200)+ self.assertEqual(+ {row["tenant_id"] for row in report["orders"]},+ {"acme", "beacon", "cedar"},+ )++ def test_negative_controls_have_deployed_entry_points(self):+ with patch.dict("os.environ", {"AUTHORIZED_TENANT_ID": "acme"}):+ balance = billing_handler({"authorized_tenant_id": "beacon"}, None)+ self.assertEqual(balance, {"tenant_id": "acme", "balance_cents": 500})+ self.assertEqual(analytics_handler({}, None), {"acme_order_count": 2})++ def test_archives_exclude_qa_answer_key(self):+ with tempfile.TemporaryDirectory() as directory:+ package(Path(directory))+ with ZipFile(Path(directory) / "order.zip") as archive:+ names = set(archive.namelist())+ self.assertIn("app/lambda_handler.py", names)+ self.assertNotIn("qa/expected-findings.json", names)+++if __name__ == "__main__":+ unittest.main()diff --git a/examples/security-workflow-sample/qa/test_scenarios.py b/examples/security-workflow-sample/qa/test_scenarios.pynew file mode 100644index 000000000..c8021a4ee--- /dev/null+++ b/examples/security-workflow-sample/qa/test_scenarios.py@@ -0,0 +1,167 @@+"""Behavior checks for the synthetic security workflow sample."""++import json+import sys+import threading+import unittest+from io import BytesIO+from pathlib import Path+from types import SimpleNamespace+from urllib.error import HTTPError+from urllib.parse import quote+from urllib.request import Request, urlopen++ROOT = Path(__file__).resolve().parents[1]+sys.path.insert(0, str(ROOT))++from app.data import create_database, list_orders, lookup_order+from app.reports import orders_by_region+from app.server import OrderHandler, create_server+++class QueryScenarios(unittest.TestCase):+ def test_order_and_report_queries_expose_cross_tenant_rows(self):+ database = create_database()+ self.addCleanup(database.close)+ self.assertEqual(+ [row["id"] for row in list_orders(database, "acme")],+ ["A-101", "A-102"],+ )+ self.assertEqual(lookup_order(database, "B-201")["tenant_id"], "beacon")+ self.assertEqual(+ [row["id"] for row in orders_by_region(database, "acme", "west")],+ ["A-101"],+ )+ changed = orders_by_region(database, "acme", "absent' OR 1=1 --")+ self.assertEqual(+ {row["tenant_id"] for row in changed},+ {"acme", "beacon", "cedar"},+ )+++class MemoryRequest:+ def __init__(self, request):+ self.input = BytesIO(request)+ self.output = BytesIO()++ def makefile(self, mode, _buffering=None):+ return self.input if mode == "rb" else self.output++ def sendall(self, data):+ self.output.write(data)+++class RouteScenariosWithoutSockets(unittest.TestCase):+ def test_authenticated_routes_reach_both_flaws(self):+ database = create_database()+ self.addCleanup(database.close)+ server = SimpleNamespace(+ database=database, server_name="localhost", server_port=80+ )++ def get(path, token=None):+ authorization = f"Authorization: Bearer {token}\r\n" if token else ""+ request = MemoryRequest(+ f"GET {path} HTTP/1.1\r\nHost: localhost\r\n"+ f"{authorization}\r\n".encode()+ )+ OrderHandler(request, ("127.0.0.1", 1), server)+ head, body = request.output.getvalue().split(b"\r\n\r\n", 1)+ status = int(head.split(b"\r\n", 1)[0].split()[1])+ return status, json.loads(body)++ self.assertEqual(get("/api/orders")[0], 401)+ own_status, own = get("/api/orders", "demo-acme")+ self.assertEqual(own_status, 200)+ self.assertEqual(len(own["orders"]), 2)+ other_status, other = get("/api/orders/B-201", "demo-acme")+ self.assertEqual(other_status, 200)+ self.assertEqual(other["order"]["tenant_id"], "beacon")+ region = quote("absent' OR 1=1 --", safe="")+ report_status, report = get(f"/api/reports/orders?region={region}", "demo-acme")+ self.assertEqual(report_status, 200)+ self.assertEqual(len(report["orders"]), 6)+++class SecurityWorkflowScenarios(unittest.TestCase):+ def setUp(self):+ self.server = create_server()+ self.worker = threading.Thread(target=self.server.serve_forever)+ self.worker.start()+ self.origin = f"http://127.0.0.1:{self.server.server_port}"++ def tearDown(self):+ self.server.shutdown()+ self.worker.join(timeout=5)+ self.server.server_close()++ def get(self, path, token=None):+ headers = {}+ if token is not None:+ headers["Authorization"] = f"Bearer {token}"+ request = Request(self.origin + path, headers=headers)+ try:+ with urlopen(request, timeout=5) as response:+ return response.status, json.load(response)+ except HTTPError as error:+ with error:+ return error.code, json.load(error)++ def test_order_lookup_crosses_customer_boundary(self):+ self.assertEqual(self.get("/health"), (200, {"status": "ok"}))+ self.assertEqual(self.get("/api/orders")[0], 401)+ status, own_list = self.get("/api/orders", "demo-acme")+ self.assertEqual(status, 200)+ self.assertEqual(+ {order["id"] for order in own_list["orders"]}, {"A-101", "A-102"}+ )+ self.assertEqual(self.get("/api/orders/A-101", "demo-acme")[0], 200)++ for order_id, tenant_id in [+ ("B-201", "beacon"),+ ("B-202", "beacon"),+ ("C-301", "cedar"),+ ("C-302", "cedar"),+ ]:+ with self.subTest(order_id=order_id):+ status, body = self.get(f"/api/orders/{order_id}", "demo-acme")+ self.assertEqual(status, 200)+ self.assertEqual(body["order"]["tenant_id"], tenant_id)++ def test_report_region_input_changes_sql_predicate(self):+ status, normal = self.get("/api/reports/orders?region=west", "demo-acme")+ self.assertEqual(status, 200)+ self.assertEqual([order["id"] for order in normal["orders"]], ["A-101"])+ self.assertEqual(+ self.get("/api/reports/orders?region=absent", "demo-acme"),+ (200, {"orders": []}),+ )++ region = quote("absent' OR 1=1 --", safe="")+ status, changed = self.get(f"/api/reports/orders?region={region}", "demo-acme")+ self.assertEqual(status, 200)+ self.assertEqual(len(changed["orders"]), 6)+ self.assertEqual(+ {order["tenant_id"] for order in changed["orders"]},+ {"acme", "beacon", "cedar"},+ )+++class DeploymentInventoryScenario(unittest.TestCase):+ def test_inventory_names_three_distinct_source_roots(self):+ inventory = json.loads((ROOT / "deployment/inventory.json").read_text())+ services = inventory["services"]+ self.assertEqual(+ {service["name"] for service in services},+ {"order-report-api", "billing-api", "analytics-worker"},+ )+ self.assertEqual(len({service["source"] for service in services}), 3)+ for service in services:+ self.assertTrue((ROOT / service["source"]).is_dir())+ self.assertTrue(services[0]["ingress"]["internetExposed"])+ self.assertFalse(services[1]["ingress"]["internetExposed"])+ self.assertIsNone(services[2]["ingress"])+++if __name__ == "__main__":+ unittest.main()