Files
mtusi2026lab7/scripts/security_negative.py
T

24 lines
808 B
Python
Executable File

#!/usr/bin/env python3
"""Small operator-facing negative checks for the level-4 defense."""
import ssl, socket, urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CA = ROOT / "certs/ca.crt"
def main():
ctx = ssl.create_default_context(cafile=str(CA))
with urllib.request.urlopen("https://localhost:8443/health/ready", context=ctx, timeout=5) as r:
assert r.status in (200, 503)
raw = socket.create_connection(("127.0.0.1", 8443), 3)
try:
raw.sendall(b"GET /health/ready HTTP/1.1\r\nHost: localhost\r\n\r\n")
data = raw.recv(128)
assert not data.startswith(b"HTTP/1.1 200")
finally:
raw.close()
print("PASS: trusted HTTPS works and plaintext external access is rejected")
if __name__ == "__main__":
main()