name: docker-egress-mitm x-egress-allowed-domains: &egress-allowed-domains | example.com api.github.com services: mitm-ca-init: image: docker-egress-mitm-squid build: context: . dockerfile_inline: | FROM ubuntu:24.04 ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update \ && apt-get install -y --no-install-recommends ca-certificates openssl squid-openssl \ && rm -rf /var/lib/apt/lists/* network_mode: none volumes: - mitm_ca_private:/mitm-ca-private - mitm_ca_public:/mitm-ca-public entrypoint: - /bin/sh - -ec command: - | if [ ! -s /mitm-ca-private/ca.crt ] || [ ! -s /mitm-ca-private/ca.key ]; then tmpdir="$$(mktemp -d)" openssl req -x509 -newkey rsa:2048 -sha256 -days 3650 -nodes \ -subj '/CN=docker-egress-test MITM CA' \ -keyout "$$tmpdir/ca.key" \ -out "$$tmpdir/ca.crt" mv "$$tmpdir/ca.key" /mitm-ca-private/ca.key mv "$$tmpdir/ca.crt" /mitm-ca-private/ca.crt rmdir "$$tmpdir" fi chmod 0644 /mitm-ca-private/ca.crt chmod 0600 /mitm-ca-private/ca.key openssl x509 -in /mitm-ca-private/ca.crt -noout -subject >/dev/null openssl rsa -in /mitm-ca-private/ca.key -check -noout >/dev/null cp /mitm-ca-private/ca.crt /mitm-ca-public/ca.crt rm -f /mitm-ca-public/ca.key chmod 0644 /mitm-ca-public/ca.crt egress-proxy: image: docker-egress-mitm-squid depends_on: mitm-ca-init: condition: service_completed_successfully networks: egress_internal: aliases: - egress-proxy egress_public: {} environment: EGRESS_ALLOWED_DOMAINS: *egress-allowed-domains volumes: - mitm_ca_private:/mitm-ca-private:ro entrypoint: - /bin/sh - -ec command: - | rm -rf /var/spool/squid/ssl_db mkdir -p /var/spool/squid /var/log/squid /run/squid chown -R proxy:proxy /var/spool/squid /var/log/squid /run/squid /usr/lib/squid/security_file_certgen -c -s /var/spool/squid/ssl_db -M 4MB chown -R proxy:proxy /var/spool/squid/ssl_db printf '%s\n' "$$EGRESS_ALLOWED_DOMAINS" > /etc/squid/allowed-domains.txt awk ' NF && $$1 !~ /^#/ { domain = $$1 gsub(/\./, "\\.", domain) print "^" domain "(:[0-9]+)?$$" } ' /etc/squid/allowed-domains.txt > /etc/squid/allowed-host-regex.txt cat > /etc/squid/squid.conf <<'SQUID_CONF' http_port 3128 ssl-bump cert=/mitm-ca-private/ca.crt key=/mitm-ca-private/ca.key generate-host-certificates=on dynamic_cert_mem_cache_size=4MB visible_hostname egress-proxy access_log none cache_log /var/log/squid/cache.log cache_store_log none cache deny all sslcrtd_program /usr/lib/squid/security_file_certgen -s /var/spool/squid/ssl_db -M 4MB sslcrtd_children 5 startup=1 idle=1 acl step1 at_step SslBump1 ssl_bump peek step1 ssl_bump bump all acl allowed_connect dstdomain "/etc/squid/allowed-domains.txt" acl allowed_http_host req_header Host -i "/etc/squid/allowed-host-regex.txt" acl SSL_ports port 443 acl Safe_ports port 80 acl Safe_ports port 443 acl CONNECT method CONNECT http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow CONNECT allowed_connect http_access deny CONNECT http_access allow allowed_connect allowed_http_host http_access deny all SQUID_CONF exec squid -f /etc/squid/squid.conf -NYC egress-test: image: ${EGRESS_TEST_IMAGE:-curlimages/curl:8.11.1} depends_on: mitm-ca-init: condition: service_completed_successfully egress-proxy: condition: service_started volumes: - mitm_ca_public:/mitm-ca:ro entrypoint: - /bin/sh - -ec command: - | pass() { printf 'ok - %s\n' "$$1" } fail() { printf 'not ok - %s\n' "$$1" >&2 exit 1 } if [ -e /mitm-ca/ca.key ]; then fail 'MITM CA private key is mounted into the test container' fi pass 'MITM CA private key is not mounted into the test container' printf 'waiting for allowed bumped request to succeed...\n' ready=0 attempt=1 while [ "$$attempt" -le 12 ]; do if curl -fsS --max-time 10 https://example.com >/dev/null 2>&1; then ready=1 break fi attempt=$$((attempt + 1)) sleep 1 done if [ "$$ready" != "1" ]; then fail 'allowed domain did not succeed through bumped proxy' fi pass 'allowed domain succeeds through bumped proxy' if SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt curl -fsS --max-time 10 https://example.com >/dev/null 2>&1; then fail 'bumped TLS unexpectedly worked without trusting the test CA' fi pass 'bumped TLS requires the mounted test CA' if curl --http1.1 -fsS --max-time 10 -H 'Host: example.org' https://example.com >/dev/null 2>&1; then fail 'decrypted Host header mismatch unexpectedly succeeded' fi pass 'decrypted Host header is enforced' if curl --http1.1 -fsS --max-time 10 --proxy-header 'Host: example.com' https://example.org >/dev/null 2>&1; then fail 'spoofed CONNECT Host header unexpectedly bypassed allowlist' fi pass 'spoofed CONNECT Host header cannot bypass allowlist' if curl -fsS --max-time 10 -H 'Host: example.com' http://example.org >/dev/null 2>&1; then fail 'spoofed HTTP Host header unexpectedly bypassed allowlist' fi pass 'spoofed HTTP Host header cannot bypass allowlist' if curl -fsS --max-time 10 https://example.org >/dev/null 2>&1; then fail 'disallowed domain unexpectedly succeeded through bumped proxy' fi pass 'disallowed domain is blocked by bumped proxy' if curl --noproxy "*" -fsS --connect-timeout 3 --max-time 8 http://example.com >/dev/null 2>&1; then fail 'direct egress unexpectedly succeeded without proxy' fi pass 'direct egress from app network is blocked' printf 'egress MITM integration test passed\n' environment: SSL_CERT_FILE: /mitm-ca/ca.crt CURL_CA_BUNDLE: /mitm-ca/ca.crt HTTPS_PROXY: http://egress-proxy:3128 http_proxy: http://egress-proxy:3128 https_proxy: http://egress-proxy:3128 NO_PROXY: localhost,127.0.0.1,::1 no_proxy: localhost,127.0.0.1,::1 networks: - egress_internal networks: egress_internal: internal: true egress_public: {} volumes: mitm_ca_private: {} mitm_ca_public: {}