#!/usr/bin/env python3 """Private bucket checked OUTSIDE the API, least-privilege API credentials, no exposed admin interfaces.""" import json import subprocess from stack import SAMPLE_PNG, api_python, dc, psql from client import Client, arguments, check, run PROBE = r''' import json, socket, urllib.request, urllib.error from app import config from app.objects import ObjectStore from botocore.exceptions import ClientError key = %r s = ObjectStore() out = {} def anon(path): try: return urllib.request.urlopen(config.S3_ENDPOINT + path, timeout=3).status except urllib.error.HTTPError as e: return e.code out["anonymous_get_object"] = anon("/" + s.bucket + "/" + key) out["anonymous_list_bucket"] = anon("/" + s.bucket + "?list-type=2") out["anonymous_list_all"] = anon("/") def try_(name, fn): try: fn(); out[name] = "ALLOWED" except ClientError as e: out[name] = e.response["Error"]["Code"] pub = '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%%s/*"}]}' %% s.bucket try_("api_create_bucket", lambda: s._s3.create_bucket(Bucket="evil-bucket")) try_("api_put_bucket_policy_public", lambda: s._s3.put_bucket_policy(Bucket=s.bucket, Policy=pub)) try_("api_put_object_acl_public", lambda: s._s3.put_object_acl(Bucket=s.bucket, Key=key, ACL="public-read")) try_("api_delete_bucket", lambda: s._s3.delete_bucket(Bucket=s.bucket)) try_("api_get_own_bucket_object", lambda: s._s3.head_object(Bucket=s.bucket, Key=key)) def port(p): try: socket.create_connection(("s3", p), timeout=2).close(); return "OPEN" except OSError as e: return type(e).__name__ out["s3:9333 master UI"] = port(9333) out["s3:8888 filer UI"] = port(8888) out["s3:8080 volume"] = port(8080) out["s3:9000 S3 API"] = port(9000) print(json.dumps(out)) ''' def main(): args = arguments("S3 privacy and least privilege") c = Client(args.base_url, timeout=10) alice, bob, eve, chat = c.bootstrap() att = c.call("POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=p.png", 201, actor=alice, raw=SAMPLE_PNG, headers={"Content-Type": "image/png"}, schema="Attachment")["json"] key = psql("SELECT object_key FROM attachments WHERE id = '%s'" % att["id"]) check(key.startswith("originals/") and att["id"].replace("-", "") not in key and "p.png" not in key, "object key must be server-generated, unrelated to id/filename") print("object key: originals/, unrelated to attachment id and filename") r = json.loads(api_python(PROBE % key)) for k, v in r.items(): print(" %-32s %s" % (k, v)) check(r["anonymous_get_object"] == 403 and r["anonymous_list_bucket"] == 403, "bucket is not private") check(r["anonymous_list_all"] in (401, 403), "anonymous ListBuckets allowed") for k in ("api_create_bucket", "api_put_bucket_policy_public", "api_put_object_acl_public", "api_delete_bucket"): check(r[k] != "ALLOWED", k + " must be denied for API credentials") check(r["api_get_own_bucket_object"] == "ALLOWED", "API cannot read its bucket") for k in ("s3:9333 master UI", "s3:8888 filer UI", "s3:8080 volume"): check(r[k] != "OPEN", k + " reachable from the network") ports = subprocess.run(["docker", "inspect", "-f", "{{json .NetworkSettings.Ports}}", dc("ps", "-q", "s3").stdout.strip()], capture_output=True, text=True).stdout.strip() check("HostPort" not in ports, "s3 publishes ports on the host") print(" s3 published host ports: none (%s)" % ports) lb = dc("exec", "-T", "lb", "wget", "-q", "-T", "2", "-O", "/dev/null", "http://s3:9000/", check=False) check(lb.returncode != 0, "LB (edge network) can reach S3") print(" lb -> s3:9000 (edge network): unreachable (%s)" % (lb.stderr.strip().splitlines() or ["refused"])[-1]) print("PASS: private bucket, least-privilege API credentials, no exposed admin interfaces") if __name__ == "__main__": run(main)