Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(eventbridge): validate and contain the API destination outbound request (#1365)

t-rech8cd11b1af382
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e7f1c1b7..a573c250 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
- **CloudWatch Logs — `GetLogGroupFields`** — returns field names found in recent stored events with a rough presence percent (`logGroupFields: [{name, percent}]`). Honors `logGroupName` or `logGroupIdentifier`, optional `time` (±8 minutes) or the default last 15 minutes, system `@*` fields, and flattened JSON message keys.
### Fixed
+- **EventBridge — API destination delivery no longer leaks credentials, and the caller-supplied values on the outbound path are validated** — an API destination target makes an outbound HTTP request that carries the connection's credentials to a URL the caller chooses, so each value on that path is now checked before it can reach the wire. (1) Delivery and OAuth token requests no longer follow redirects: a `302` to another host could carry the connection's `Authorization` header to the redirect target (urllib preserves it across a cross-host hop) — a `3xx` is now a non-retryable delivery failure, as on AWS. (2) `CreateApiDestination` / `UpdateApiDestination` reject an `InvocationEndpoint` that is not a dialable `http(s)://` URL, and `CreateConnection` / `UpdateConnection` apply the same check to the OAuth `AuthorizationEndpoint` — the URL a connection's `client_id`/`client_secret` are POSTed to, previously stored unvalidated and failing only later as a log line on a delivery thread. The outbound opener independently refuses non-HTTP(S) schemes, so `file://` never reaches the wire even for a record restored from persistence. This is a scheme guard only — MiniStack is a local emulator where delivering to `127.0.0.1` is the normal case, so loopback, private and link-local hosts stay reachable over http(s) by design. (3) `HttpMethod` and a connection's `AuthorizationType` are validated against their API enums instead of being stored verbatim — an unrecognized method silently became `POST`, and an unrecognized authorization type matched nothing, so the connection delivered with no credentials at all. (4) A connection `ApiKeyName` colliding with a reserved header (e.g. `User-Agent`) can no longer override EventBridge's fixed headers. (5) `DeauthorizeConnection` now removes the connection's authorization parameters, as the API reference says it does, and delivery skips a connection that is not `AUTHORIZED` — previously the credentials stayed in place and every later delivery still presented them. (6) The OAuth token response is read with a 1 MiB bound, the ceiling EventBridge's fixed `Range` header already puts on a delivery response.
- **Lambda — uncaught handler exceptions report `X-Amz-Function-Error: Unhandled`** — the Docker/RIE executor read the error class off a response header the RIE never sets, so every failure was reported `Handled` and API Gateway consumers keying off the 502-for-Unhandled contract never took the error path. Headerless payloads are now classified by shape: the runtime's error serialization reports `Unhandled`, an HTTP-style envelope with `statusCode` stays `Handled`. Contributed by @iot-rocket.
- **IoT — `CreateThingType` is idempotent for identical re-creates** — re-creating an existing thing type always returned `ResourceAlreadyExistsException`, so a retried request or a re-run provisioning script failed where AWS succeeds. The same `thingTypeProperties` now return the existing ids — absent, `null` and empty compare equal, `searchableAttributes` is unordered — and only a real mismatch keeps the `409`, as `CreateThing` already did. Contributed by @iot-rocket.
- **EC2 — `RevokeSecurityGroupIngress` / `RevokeSecurityGroupEgress` honour `SecurityGroupRuleIds`** — both read only `IpPermissions`, so a revoke by rule id (as Terraform does) returned `Return=true` while removing nothing, leaving the rule in place forever. Ids now resolve against the group's `sgr-*` rules, which are removed with their tags and echoed in `revokedSecurityGroupRuleSet`; one unknown id rejects the whole call with `InvalidSecurityGroupRuleId.NotFound`. Contributed by @iot-rocket.
diff --git a/README.md b/README.md
index 235ea599..9938c952 100644
--- a/README.md
+++ b/README.md
@@ -415,7 +415,7 @@ subnet = ec2.create_subnet(
| Service | Operations | Notes |
|---------|-----------|-------|
| **SSM Parameter Store** | PutParameter, GetParameter, GetParameters, GetParametersByPath, DeleteParameter, DeleteParameters, DescribeParameters, GetParameterHistory, LabelParameterVersion, AddTagsToResource, RemoveTagsFromResource, ListTagsForResource | Supports String, SecureString, StringList |
-| **EventBridge** | CreateEventBus, UpdateEventBus, DeleteEventBus, ListEventBuses, DescribeEventBus, PutRule, DeleteRule, ListRules, DescribeRule, EnableRule, DisableRule, PutTargets, RemoveTargets, ListTargetsByRule, ListRuleNamesByTarget, PutEvents, TestEventPattern, TagResource, UntagResource, ListTagsForResource, CreateArchive, DeleteArchive, DescribeArchive, UpdateArchive, ListArchives, PutPermission, RemovePermission, CreateConnection, DescribeConnection, DeleteConnection, UpdateConnection, DeauthorizeConnection, ListConnections, CreateApiDestination, DescribeApiDestination, DeleteApiDestination, UpdateApiDestination, ListApiDestinations, StartReplay, DescribeReplay, ListReplays, CancelReplay, CreateEndpoint, DeleteEndpoint, DescribeEndpoint, ListEndpoints, UpdateEndpoint, ActivateEventSource, DeactivateEventSource, DescribeEventSource, CreatePartnerEventSource, DeletePartnerEventSource, DescribePartnerEventSource, ListPartnerEventSources, ListPartnerEventSourceAccounts, ListEventSources, PutPartnerEvents | Lambda target dispatch on PutEvents; S3 EventBridge notifications; archives capture matching events and `StartReplay` re-dispatches them to the destination bus in a background thread; API destination targets are **invoked over HTTP** on PutEvents — connection auth (Basic, API Key, OAuth client-credentials with token caching and 401/407 refresh), connection/target header/query/body parameter merging with connection precedence, `*` path parameters, and the AWS default headers (`User-Agent: Amazon/EventBridge/ApiDestinations`, default `Content-Type: application/json; charset=utf-8`) — the 24h retry pipeline, rate limiting, and DLQs are not modeled; SaaS/partner APIs are otherwise control-plane stubs |
+| **EventBridge** | CreateEventBus, UpdateEventBus, DeleteEventBus, ListEventBuses, DescribeEventBus, PutRule, DeleteRule, ListRules, DescribeRule, EnableRule, DisableRule, PutTargets, RemoveTargets, ListTargetsByRule, ListRuleNamesByTarget, PutEvents, TestEventPattern, TagResource, UntagResource, ListTagsForResource, CreateArchive, DeleteArchive, DescribeArchive, UpdateArchive, ListArchives, PutPermission, RemovePermission, CreateConnection, DescribeConnection, DeleteConnection, UpdateConnection, DeauthorizeConnection, ListConnections, CreateApiDestination, DescribeApiDestination, DeleteApiDestination, UpdateApiDestination, ListApiDestinations, StartReplay, DescribeReplay, ListReplays, CancelReplay, CreateEndpoint, DeleteEndpoint, DescribeEndpoint, ListEndpoints, UpdateEndpoint, ActivateEventSource, DeactivateEventSource, DescribeEventSource, CreatePartnerEventSource, DeletePartnerEventSource, DescribePartnerEventSource, ListPartnerEventSources, ListPartnerEventSourceAccounts, ListEventSources, PutPartnerEvents | Lambda target dispatch on PutEvents; S3 EventBridge notifications; archives capture matching events and `StartReplay` re-dispatches them to the destination bus in a background thread; API destination targets are **invoked over HTTP** on PutEvents — connection auth (Basic, API Key, OAuth client-credentials with token caching and 401/407 refresh), connection/target header/query/body parameter merging with connection precedence, `*` path parameters, and the AWS default headers (`User-Agent: Amazon/EventBridge/ApiDestinations`, default `Content-Type: application/json; charset=utf-8`), which connection and target parameters cannot override; redirects are not followed (a `3xx` is a non-retryable delivery failure), only `http(s)` invocation and OAuth authorization endpoints are accepted, `HttpMethod` and `AuthorizationType` are validated against their enums, and a connection that is not `AUTHORIZED` is not invoked (`DeauthorizeConnection` clears its stored credentials) — the 24h retry pipeline, rate limiting, and DLQs are not modeled; SaaS/partner APIs are otherwise control-plane stubs |
| **Kinesis** | CreateStream, DeleteStream, DescribeStream, DescribeStreamSummary, ListStreams, ListShards, PutRecord, PutRecords, GetShardIterator, GetRecords, IncreaseStreamRetentionPeriod, DecreaseStreamRetentionPeriod, MergeShards, SplitShard, UpdateShardCount, StartStreamEncryption, StopStreamEncryption, EnableEnhancedMonitoring, DisableEnhancedMonitoring, RegisterStreamConsumer, DeregisterStreamConsumer, ListStreamConsumers, DescribeStreamConsumer, AddTagsToStream, RemoveTagsFromStream, ListTagsForStream | Partition key → shard routing; AWS limits enforced (1 MB/record, 500 records/batch, 5 MB payload, 256-char partition key) |
| **CloudWatch Metrics** | PutMetricData, GetMetricStatistics, GetMetricData, ListMetrics, PutMetricAlarm, PutCompositeAlarm, DescribeAlarms, DescribeAlarmsForMetric, DescribeAlarmHistory, DeleteAlarms, SetAlarmState, EnableAlarmActions, DisableAlarmActions, TagResource, UntagResource, ListTagsForResource, PutDashboard, GetDashboard, DeleteDashboards, ListDashboards | CBOR and JSON protocol |
| **SES** | SendEmail, SendRawEmail, SendTemplatedEmail, SendBulkTemplatedEmail, VerifyEmailIdentity, VerifyEmailAddress, VerifyDomainIdentity, VerifyDomainDkim, ListIdentities, ListVerifiedEmailAddresses, GetIdentityVerificationAttributes, GetIdentityDkimAttributes, DeleteIdentity, GetSendQuota, GetSendStatistics, SetIdentityNotificationTopic, SetIdentityFeedbackForwardingEnabled, CreateConfigurationSet, DeleteConfigurationSet, DescribeConfigurationSet, ListConfigurationSets, CreateTemplate, GetTemplate, UpdateTemplate, DeleteTemplate, ListTemplates | Emails stored in-memory, not sent |
diff --git a/ministack/services/eventbridge.py b/ministack/services/eventbridge.py
index a7505ee6..99125424 100644
--- a/ministack/services/eventbridge.py
+++ b/ministack/services/eventbridge.py
@@ -30,6 +30,9 @@ import os
import re
import threading
import time
+import urllib.error
+import urllib.parse
+import urllib.request
from datetime import datetime, timedelta, timezone
from ministack.core.arn import ArnParseError, parse_arn
@@ -1449,6 +1452,9 @@ _API_DEST_FIXED_HEADERS = {
"Connection": "close",
}
_API_DEST_DEFAULT_CONTENT_TYPE = "application/json; charset=utf-8"
+# Same 1 MiB ceiling the Range header above imposes on a delivery response,
+# applied to the OAuth token response — which this emulator does read.
+_MAX_OAUTH_RESPONSE_BYTES = 1048576
# Real EventBridge removes these headers from API destination requests, so
# connection/target parameters cannot smuggle them in (transport-owned headers
@@ -1464,6 +1470,35 @@ _API_DEST_REMOVED_HEADERS = {
}
+class _NoFollowRedirect(urllib.request.HTTPRedirectHandler):
+ """Never follow HTTP redirects. urllib preserves the Authorization header
+ across a cross-host 3xx hop, which would leak a connection's credentials to
+ the redirect target. Real EventBridge does not follow redirects — a 3xx is
+ a (non-retryable) delivery failure — so declining to redirect lets urllib's
+ default error handler surface the 3xx as an ``HTTPError``, which the
+ delivery path reports as that (non-retryable) status code."""
+
+ def redirect_request(self, *_args, **_kwargs):
+ return None
+
+
+_api_dest_opener = urllib.request.build_opener(_NoFollowRedirect())
+
+
+def _http_open(req: urllib.request.Request, timeout: int = 5):
+ """Open one outbound API-destination / OAuth request without following
+ redirects and without honoring non-HTTP(S) schemes, so a caller-controlled
+ endpoint cannot turn a delivery into a ``file://`` read. A scheme guard, not
+ a network guard: delivering to ``127.0.0.1`` is the normal local case, so
+ private, loopback, and link-local hosts stay reachable over http(s). The
+ 5-second default is AWS parity — API destination requests have a maximum
+ client execution timeout of 5 seconds."""
+ scheme = urllib.parse.urlsplit(req.full_url).scheme.lower()
+ if scheme not in ("http", "https"):
+ raise ValueError(f"unsupported URL scheme for API destination request: {scheme or '(none)'}")
+ return _api_dest_opener.open(req, timeout=timeout)
+
+
def _connection_params_map(params) -> dict:
"""Flatten a ConnectionHttpParameters list (``[{Key, Value, IsValueSecret}]``)
into a plain map. Connection-side parameters use the list-of-objects shape,
@@ -1482,8 +1517,6 @@ def _apply_path_parameters(url: str, values) -> str:
EventBridge."""
if not values:
return url
- import urllib.parse
-
parsed = urllib.parse.urlsplit(url)
path = parsed.path
for value in values:
@@ -1496,8 +1529,6 @@ def _apply_path_parameters(url: str, values) -> str:
def _merge_query(url: str, params: dict) -> str:
if not params:
return url
- import urllib.parse
-
parsed = urllib.parse.urlsplit(url)
query = dict(urllib.parse.parse_qsl(parsed.query, keep_blank_values=True))
query.update(params)
@@ -1530,9 +1561,6 @@ def _fetch_oauth_token(oauth: dict) -> dict:
response. ``grant_type`` defaults to ``client_credentials`` when
OAuthHttpParameters does not set one — AWS's own examples pass it
explicitly through body parameters."""
- import urllib.parse
- import urllib.request
-
method = (oauth.get("HttpMethod") or "POST").upper()
client = oauth.get("ClientParameters") or {}
http_params = oauth.get("OAuthHttpParameters") or {}
@@ -1554,8 +1582,13 @@ def _fetch_oauth_token(oauth: dict) -> dict:
data = urllib.parse.urlencode(form).encode("utf-8")
headers.setdefault("Content-Type", "application/x-www-form-urlencoded")
req = urllib.request.Request(url, data=data, headers=headers, method=method)
- with urllib.request.urlopen(req, timeout=5) as resp:
- token = json.loads(resp.read().decode("utf-8"))
+ with _http_open(req) as resp:
+ # Bounded read: the authorization endpoint is caller-supplied, and an
+ # unbounded read of whatever it returns is a memory hazard on a delivery
+ # thread. 1 MiB is EventBridge's own ceiling for an API destination
+ # response (the fixed "Range: bytes=0-1048575" header); a token document
+ # that large is not one.
+ token = json.loads(resp.read(_MAX_OAUTH_RESPONSE_BYTES).decode("utf-8"))
token_type = token.get("token_type") or "Bearer"
if token_type.lower() == "bearer":
token_type = "Bearer"
@@ -1617,6 +1650,20 @@ def _connection_auth_headers(conn: dict, token_key, force_refresh: bool = False)
return {}
+def _finalize_api_dest_headers(headers: dict) -> dict:
+ """Drop the headers real EventBridge strips from API destination requests
+ and stamp its non-overridable ones. Applied after *every* merge — target
+ and connection parameters first, then the connection auth headers — so no
+ configured name can win, e.g. an ApiKeyName of ``User-Agent``/``Host``.
+ Names are matched trimmed, because ``"User-Agent "`` is not in the removed
+ set but is a legal header name to http.client, which only rejects leading
+ whitespace — so the padded spelling would ride out alongside the real one."""
+ out = {k.strip(): v for k, v in headers.items()
+ if k.strip().lower() not in _API_DEST_REMOVED_HEADERS}
+ out.update(_API_DEST_FIXED_HEADERS)
+ return out
+
+
def _build_api_destination_request(dest: dict, conn: dict, target_http_params: dict, payload: str) -> dict:
invocation = (conn.get("AuthParameters") or {}).get("InvocationHttpParameters") or {}
conn_headers = _connection_params_map(invocation.get("HeaderParameters"))
@@ -1635,10 +1682,9 @@ def _build_api_destination_request(dest: dict, conn: dict, target_http_params: d
url = _merge_query(url, query)
body = _merge_body_parameters(payload, conn_body)
- headers = {k: v for k, v in headers.items() if k.lower() not in _API_DEST_REMOVED_HEADERS}
+ headers = _finalize_api_dest_headers(headers)
if not any(k.lower() == "content-type" for k in headers):
headers["Content-Type"] = _API_DEST_DEFAULT_CONTENT_TYPE
- headers.update(_API_DEST_FIXED_HEADERS)
return {
"url": url,
@@ -1651,13 +1697,9 @@ def _build_api_destination_request(dest: dict, conn: dict, target_http_params: d
def _api_destination_send_sync(request: dict, auth_headers: dict) -> int:
"""Blocking HTTP send for one API destination delivery. Runs on a worker
thread so the event loop stays unblocked; stdlib only (see the SNS HTTP
- delivery note — aiohttp is not a declared dependency). The 5-second timeout
- is AWS parity: API destination requests have a maximum client execution
- timeout of 5 seconds."""
- import urllib.error
- import urllib.request
-
- headers = {**request["headers"], **auth_headers}
+ delivery note — aiohttp is not a declared dependency). ``_http_open``
+ carries the AWS-parity 5-second client execution timeout."""
+ headers = _finalize_api_dest_headers({**request["headers"], **auth_headers})
body = request["body"]
req = urllib.request.Request(
request["url"],
@@ -1666,7 +1708,7 @@ def _api_destination_send_sync(request: dict, auth_headers: dict) -> int:
method=request["method"],
)
try:
- with urllib.request.urlopen(req, timeout=5) as resp:
+ with _http_open(req) as resp:
return resp.status
except urllib.error.HTTPError as exc:
return exc.code
@@ -1729,6 +1771,16 @@ def _dispatch_to_api_destination(spec, payload, target):
"EventBridge → API destination %s: connection %s not found", name, conn_name or "<unset>"
)
return
+ if conn.get("ConnectionState") != "AUTHORIZED":
+ # The mirror of the ApiDestinationState check above. DeauthorizeConnection
+ # already clears the stored credentials, but a connection restored from
+ # persisted state can be DEAUTHORIZED with parameters still attached — and
+ # delivering unauthenticated is not a better outcome than not delivering.
+ logger.warning(
+ "EventBridge → API destination %s: connection %s is %s; not invoked",
+ name, conn_name, conn.get("ConnectionState"),
+ )
+ return
# InvocationRateLimitPerSecond is accepted at CreateApiDestination but not
# enforced here — MiniStack delivers immediately and does not model the
# delivery queue that rate limiting implies.
@@ -2284,10 +2336,92 @@ def _remove_permission(data):
# Connections
# ---------------------------------------------------------------------------
+# An http(s) URL with a non-empty authority. Schemes are case-insensitive per
+# RFC 3986 — "HTTPS://host" is the same endpoint urllib would dial, so the API
+# must not reject what the outbound opener accepts. Control characters and
+# spaces are rejected separately: urlsplit strips CR/LF/TAB before it reports a
+# hostname (bpo-43882), so the pattern alone would pass them through.
+_HTTP_ENDPOINT_RE = re.compile(r"^https?://[^/?#\s]+", re.IGNORECASE)
+_ENDPOINT_CTL_RE = re.compile(r"[\x00-\x20\x7f]")
+
+# The other two caller-supplied values that ride the outbound request: the
+# method goes on the request line, and the authorization type decides which
+# credentials are attached. Both are enums on the API model, so an unrecognized
+# value is a 400 on AWS — here it used to be stored and then silently mean
+# "send no credentials" or "POST".
+_API_DEST_HTTP_METHODS = ("DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT")
+_CONNECTION_AUTH_TYPES = ("API_KEY", "BASIC", "OAUTH_CLIENT_CREDENTIALS")
+
+
+def _validate_enum(value, allowed, param: str):
+ """Reject a value outside an API-model enum, in the shape AWS's own
+ validator emits. Absent is not invalid — required-ness is checked by the
+ caller that needs it."""
+ if value is None or value in allowed:
+ return None
+ return error_response_json(
+ "ValidationException",
+ f"1 validation error detected: Value '{value}' at '{param}' failed to satisfy "
+ f"constraint: Member must satisfy enum value set: [{', '.join(allowed)}]",
+ 400,
+ )
+
+
+def _validate_http_endpoint(endpoint, param: str):
+ """Reject a caller-supplied endpoint that is not a dialable http(s) URL, as
+ real AWS does at CreateApiDestination / CreateConnection. Keeping non-http
+ schemes out of stored state also keeps them off the outbound opener —
+ defense in depth with ``_http_open``'s scheme guard, which is the backstop
+ for records restored from persistence. "Dialable" is the whole point, so a
+ non-empty authority is not sufficient: ``http://@`` and ``http://:80`` carry
+ no host, and ``http://ho\\nst/x`` reaches the connect call as something the
+ caller never wrote."""
+ ok = (
+ isinstance(endpoint, str)
+ and _HTTP_ENDPOINT_RE.match(endpoint) is not None
+ and _ENDPOINT_CTL_RE.search(endpoint) is None
+ )
+ if ok:
+ try:
+ ok = bool(urllib.parse.urlsplit(endpoint).hostname)
+ except ValueError:
+ ok = False # malformed authority, e.g. an unclosed IPv6 bracket
+ if not ok:
+ return error_response_json(
+ "ValidationException",
+ f"Parameter {param} is not valid. "
+ "Reason: Must be an http:// or https:// endpoint.",
+ 400,
+ )
+ return None
+
+
+def _validate_oauth_authorization_endpoint(auth_params):
+ """The OAuth AuthorizationEndpoint is the second caller-controlled URL on
+ this path, and the one the client_id/client_secret are POSTed to — so it
+ gets the same http(s) check as an API destination's InvocationEndpoint
+ rather than only failing (silently, on a delivery thread) at token time.
+
+ ``auth_params`` is raw caller JSON, so it is type-checked rather than
+ dereferenced: CreateConnection stored a non-object AuthParameters verbatim
+ and answered 200, and a validation step must not turn that into a 500."""
+ oauth = auth_params.get("OAuthParameters") if isinstance(auth_params, dict) else None
+ if not isinstance(oauth, dict) or "AuthorizationEndpoint" not in oauth:
+ return None
+ return _validate_http_endpoint(oauth["AuthorizationEndpoint"], "AuthorizationEndpoint")
+
+
def _create_connection(data):
name = data.get("Name")
if not name:
return error_response_json("ValidationException", "Name is required", 400)
+ auth_type_error = _validate_enum(
+ data.get("AuthorizationType"), _CONNECTION_AUTH_TYPES, "authorizationType")
+ if auth_type_error:
+ return auth_type_error
+ endpoint_error = _validate_oauth_authorization_endpoint(data.get("AuthParameters"))
+ if endpoint_error:
+ return endpoint_error
if name in _connections:
return error_response_json("ResourceAlreadyExistsException",
f"Connection {name} already exists", 400)
@@ -2362,6 +2496,15 @@ def _update_connection(data):
if name not in _connections:
return error_response_json("ResourceNotFoundException",
f"Connection {name} does not exist.", 400)
+ if "AuthorizationType" in data:
+ auth_type_error = _validate_enum(
+ data["AuthorizationType"], _CONNECTION_AUTH_TYPES, "authorizationType")
+ if auth_type_error:
+ return auth_type_error
+ if "AuthParameters" in data:
+ endpoint_error = _validate_oauth_authorization_endpoint(data["AuthParameters"])
+ if endpoint_error:
+ return endpoint_error
conn = _connections[name]
now = _now_ts()
for key in ("AuthorizationType", "AuthParameters", "Description"):
@@ -2384,6 +2527,12 @@ def _update_connection(data):
def _deauthorize_connection(data):
+ """Per the API reference this "removes all authorization parameters from the
+ connection … so you can reuse it without having to create a new connection",
+ so the stored credentials go with the state flip. Keeping them meant a
+ deauthorized connection carried on presenting its Basic/API-key credentials
+ on every delivery, and evicting the OAuth token achieved nothing because the
+ next delivery simply fetched a fresh one."""
name = data.get("Name")
if not name:
return error_response_json("ValidationException", "Name is required", 400)
@@ -2395,6 +2544,7 @@ def _deauthorize_connection(data):
conn["ConnectionState"] = "DEAUTHORIZED"
conn["LastModifiedTime"] = now
conn.pop("LastAuthorizedTime", None)
+ conn["AuthParameters"] = {}
_evict_oauth_token(name)
return json_response({
"ConnectionArn": conn["ConnectionArn"],
@@ -2411,6 +2561,12 @@ def _create_api_destination(data):
name = data.get("Name")
if not name:
return error_response_json("ValidationException", "Name is required", 400)
+ endpoint_error = _validate_http_endpoint(data.get("InvocationEndpoint", ""), "InvocationEndpoint")
+ if endpoint_error:
+ return endpoint_error
+ method_error = _validate_enum(data.get("HttpMethod"), _API_DEST_HTTP_METHODS, "httpMethod")
+ if method_error:
+ return method_error
if name in _api_destinations:
return error_response_json("ResourceAlreadyExistsException",
f"ApiDestination {name} already exists", 400)
@@ -2483,6 +2639,14 @@ def _update_api_destination(data):
if name not in _api_destinations:
return error_response_json("ResourceNotFoundException",
f"ApiDestination {name} does not exist.", 400)
+ if "InvocationEndpoint" in data:
+ endpoint_error = _validate_http_endpoint(data["InvocationEndpoint"], "InvocationEndpoint")
+ if endpoint_error:
+ return endpoint_error
+ if "HttpMethod" in data:
+ method_error = _validate_enum(data["HttpMethod"], _API_DEST_HTTP_METHODS, "httpMethod")
+ if method_error:
+ return method_error
dest = _api_destinations[name]
now = _now_ts()
for key in ("ConnectionArn", "InvocationEndpoint", "HttpMethod",
diff --git a/tests/test_eventbridge.py b/tests/test_eventbridge.py
index 1aba6cf2..e7e88db6 100644
--- a/tests/test_eventbridge.py
+++ b/tests/test_eventbridge.py
@@ -3304,3 +3304,471 @@ def test_eventbridge_api_destination_oauth_refresh_on_401(eb):
finally:
issuer.shutdown()
server.shutdown()
+
+
+# ---------------------------------------------------------------------------
+# API-destination outbound-HTTP hardening regression tests
+# ---------------------------------------------------------------------------
+
+def _start_redirecting_server(location):
+ """A stand-in endpoint that answers every request with 302 -> location."""
+ import threading as _threading
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+
+ seen = []
+
+ class _Handler(BaseHTTPRequestHandler):
+ def _handle(self):
+ length = int(self.headers.get("Content-Length") or 0)
+ if length:
+ self.rfile.read(length)
+ seen.append({"headers": {k.lower(): v for k, v in self.headers.items()}})
+ self.send_response(302)
+ self.send_header("Location", location)
+ self.send_header("Content-Length", "0")
+ self.end_headers()
+
+ do_POST = _handle
+ do_PUT = _handle
+ do_GET = _handle
+
+ def log_message(self, _f, *_a):
+ return
+
+ server = ThreadingHTTPServer(("127.0.0.1", 0), _Handler)
+ _threading.Thread(target=server.serve_forever, daemon=True).start()
+ return server, seen
+
+
+def test_eventbridge_api_destination_does_not_follow_redirect(eb):
+ """A 3xx must not be followed — urllib would carry the Authorization header
+ to the redirect target, leaking credentials. The secondary endpoint must
+ receive nothing."""
+ secondary, secondary_captured = _start_api_dest_capture_server()
+ try:
+ secondary_port = secondary.server_address[1]
+ primary, primary_seen = _start_redirecting_server(
+ f"http://127.0.0.1:{secondary_port}/stolen"
+ )
+ try:
+ primary_port = primary.server_address[1]
+ bus_name, source = _api_dest_pipeline(
+ eb,
+ "redirect",
+ f"http://127.0.0.1:{primary_port}/hook",
+ "BASIC",
+ {"BasicAuthParameters": {"Username": "svcuser", "Password": "hunter2"}},
+ )
+ eb.put_events(Entries=[{
+ "Source": source, "DetailType": "Ping",
+ "Detail": json.dumps({"n": 1}), "EventBusName": bus_name,
+ }])
+ # The primary must actually be hit, or "the secondary saw nothing"
+ # is vacuously true and this stops guarding anything.
+ assert _wait_until(lambda: len(primary_seen) >= 1)
+ # Then give the redirect time to be followed; it must not be.
+ _wait_until(lambda: len(secondary_captured) >= 1, timeout=2.0)
+ assert secondary_captured == []
+ finally:
+ primary.shutdown()
+ finally:
+ secondary.shutdown()
+
+
+def test_eventbridge_api_destination_rejects_non_http_endpoint(eb):
+ """file://, ftp://, gopher://, and non-URLs are rejected at create/update;
+ http(s) endpoints are accepted."""
+ conn = eb.create_connection(
+ Name=f"qa-eb-scheme-conn-{_uuid_mod.uuid4().hex[:8]}",
+ AuthorizationType="API_KEY",
+ AuthParameters={"ApiKeyAuthParameters": {"ApiKeyName": "X-Api-Key", "ApiKeyValue": "k"}},
+ )
+ conn_arn = conn["ConnectionArn"]
+ for bad in ("file:///etc/passwd", "ftp://127.0.0.1/x", "gopher://x/", "not a url at all"):
+ with pytest.raises(ClientError) as exc:
+ eb.create_api_destination(
+ Name=f"qa-eb-scheme-{_uuid_mod.uuid4().hex[:8]}",
+ ConnectionArn=conn_arn,
+ InvocationEndpoint=bad,
+ HttpMethod="POST",
+ )
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert "InvocationEndpoint" in exc.value.response["Error"]["Message"]
+
+ good_name = f"qa-eb-scheme-ok-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_api_destination(
+ Name=good_name, ConnectionArn=conn_arn,
+ InvocationEndpoint="https://example.test/hook", HttpMethod="POST",
+ )
+ with pytest.raises(ClientError) as exc:
+ eb.update_api_destination(Name=good_name, InvocationEndpoint="file:///etc/shadow")
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+
+
+def test_eventbridge_api_destination_api_key_cannot_override_reserved_header(eb):
+ """An ApiKeyName colliding with a reserved header (User-Agent) must not
+ override EventBridge's fixed value at delivery time."""
+ server, captured = _start_api_dest_capture_server()
+ try:
+ port = server.server_address[1]
+ bus_name, source = _api_dest_pipeline(
+ eb,
+ "hdrinject",
+ f"http://127.0.0.1:{port}/hook",
+ "API_KEY",
+ {"ApiKeyAuthParameters": {"ApiKeyName": "User-Agent", "ApiKeyValue": "pwned-agent"}},
+ )
+ eb.put_events(Entries=[{
+ "Source": source, "DetailType": "Ping",
+ "Detail": json.dumps({"n": 1}), "EventBusName": bus_name,
+ }])
+ assert _wait_until(lambda: len(captured) >= 1)
+ assert captured[0]["headers"]["user-agent"] == "Amazon/EventBridge/ApiDestinations"
+ finally:
+ server.shutdown()
+
+
+def test_eventbridge_api_destination_endpoint_scheme_is_case_insensitive(eb):
+ """URL schemes are case-insensitive (RFC 3986) and the outbound opener
+ lowercases before comparing, so ``HTTPS://`` must be accepted at the API —
+ rejecting it would fail a destination the emulator can in fact deliver to."""
+ conn = eb.create_connection(
+ Name=f"qa-eb-scheme-case-conn-{_uuid_mod.uuid4().hex[:8]}",
+ AuthorizationType="API_KEY",
+ AuthParameters={"ApiKeyAuthParameters": {"ApiKeyName": "X-Api-Key", "ApiKeyValue": "k"}},
+ )
+ name = f"qa-eb-scheme-case-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_api_destination(
+ Name=name, ConnectionArn=conn["ConnectionArn"],
+ InvocationEndpoint="HTTPS://api.example.test/hook", HttpMethod="POST",
+ )
+ assert eb.describe_api_destination(Name=name)["InvocationEndpoint"] == \
+ "HTTPS://api.example.test/hook"
+ eb.update_api_destination(Name=name, InvocationEndpoint="Http://api.example.test/hook2")
+ assert eb.describe_api_destination(Name=name)["InvocationEndpoint"] == \
+ "Http://api.example.test/hook2"
+
+
+def test_eventbridge_api_destination_rejects_endpoint_without_host(eb):
+ """An endpoint with no host is not dialable. Storing it only moves the
+ failure to a background delivery thread, where the caller never sees it.
+ ``http://@`` and ``http://:80`` carry a non-empty authority but still no
+ host, so an authority check alone is not enough."""
+ conn = eb.create_connection(
+ Name=f"qa-eb-scheme-host-conn-{_uuid_mod.uuid4().hex[:8]}",
+ AuthorizationType="API_KEY",
+ AuthParameters={"ApiKeyAuthParameters": {"ApiKeyName": "X-Api-Key", "ApiKeyValue": "k"}},
+ )
+ conn_arn = conn["ConnectionArn"]
+ for bad in ("http://", "https://", "http:///hook", "https://?q=1",
+ "http://@", "http://:80", "http://[::1/hook",
+ # urlsplit strips CR/LF/TAB before reporting a hostname
+ # (bpo-43882), so these clear a host check but reach connect
+ # as something the caller never wrote — "http://ho\nst/hook"
+ # resolves to "host", and the CRLF form to "hostx-injected".
+ "http://ho\nst/hook", "http://host\t/hook",
+ "http://host\r\nX-Injected: 1/hook", "http://ho st/hook"):
+ with pytest.raises(ClientError) as exc:
+ eb.create_api_destination(
+ Name=f"qa-eb-scheme-host-{_uuid_mod.uuid4().hex[:8]}",
+ ConnectionArn=conn_arn,
+ InvocationEndpoint=bad,
+ HttpMethod="POST",
+ )
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert "InvocationEndpoint" in exc.value.response["Error"]["Message"]
+
+ name = f"qa-eb-scheme-host-ok-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_api_destination(
+ Name=name, ConnectionArn=conn_arn,
+ InvocationEndpoint="https://api.example.test/hook", HttpMethod="POST",
+ )
+ with pytest.raises(ClientError) as exc:
+ eb.update_api_destination(Name=name, InvocationEndpoint="http://")
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert eb.describe_api_destination(Name=name)["InvocationEndpoint"] == \
+ "https://api.example.test/hook"
+
+
+def test_eventbridge_connection_rejects_non_http_oauth_endpoint(eb):
+ """CreateConnection POSTs the client_id/client_secret to the OAuth
+ AuthorizationEndpoint, so a non-http(s) one is rejected at the API instead
+ of surfacing much later as a log line on a delivery worker."""
+ for bad in ("file:///etc/passwd", "ftp://127.0.0.1/token", "https://", "nope"):
+ with pytest.raises(ClientError) as exc:
+ eb.create_connection(
+ Name=f"qa-eb-oauth-scheme-{_uuid_mod.uuid4().hex[:8]}",
+ AuthorizationType="OAUTH_CLIENT_CREDENTIALS",
+ AuthParameters={"OAuthParameters": {
+ "AuthorizationEndpoint": bad,
+ "HttpMethod": "POST",
+ "ClientParameters": {"ClientID": "cid", "ClientSecret": "csecret"},
+ }},
+ )
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert "AuthorizationEndpoint" in exc.value.response["Error"]["Message"]
+
+ # A connection with no OAuth parameters at all is not an endpoint carrier
+ # and must stay creatable.
+ api_key_name = f"qa-eb-oauth-scheme-apikey-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_connection(
+ Name=api_key_name,
+ AuthorizationType="API_KEY",
+ AuthParameters={"ApiKeyAuthParameters": {"ApiKeyName": "X-Api-Key", "ApiKeyValue": "k"}},
+ )
+ assert eb.describe_connection(Name=api_key_name)["ConnectionState"] == "AUTHORIZED"
+
+
+def test_eventbridge_connection_update_rejects_non_http_oauth_endpoint(eb):
+ """UpdateConnection can repoint the token request at a new URL, so it gets
+ the same check — and a rejected update must leave the stored endpoint
+ untouched. A description-only update carries no endpoint and is unaffected."""
+ name = f"qa-eb-oauth-update-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_connection(
+ Name=name,
+ AuthorizationType="OAUTH_CLIENT_CREDENTIALS",
+ AuthParameters={"OAuthParameters": {
+ "AuthorizationEndpoint": "https://idp.example.test/token",
+ "HttpMethod": "POST",
+ "ClientParameters": {"ClientID": "cid", "ClientSecret": "csecret"},
+ }},
+ )
+ with pytest.raises(ClientError) as exc:
+ eb.update_connection(
+ Name=name,
+ AuthParameters={"OAuthParameters": {
+ "AuthorizationEndpoint": "file:///etc/passwd",
+ "HttpMethod": "POST",
+ "ClientParameters": {"ClientID": "cid", "ClientSecret": "csecret"},
+ }},
+ )
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert "AuthorizationEndpoint" in exc.value.response["Error"]["Message"]
+
+ stored = eb.describe_connection(Name=name)["AuthParameters"]["OAuthParameters"]
+ assert stored["AuthorizationEndpoint"] == "https://idp.example.test/token"
+
+ eb.update_connection(Name=name, Description="still fine")
+ assert eb.describe_connection(Name=name)["Description"] == "still fine"
+
+
+def test_eventbridge_deauthorized_connection_stops_delivering_credentials(eb):
+ """DeauthorizeConnection "removes all authorization parameters from the
+ connection", so the credentials must stop going out. Deauthorizing used to
+ flip the state and evict the cached OAuth token while leaving
+ AuthParameters in place, so every later delivery still presented the same
+ Basic credentials to the endpoint."""
+ server, captured = _start_api_dest_capture_server()
+ try:
+ port = server.server_address[1]
+ bus_name, source = _api_dest_pipeline(
+ eb,
+ "deauth",
+ f"http://127.0.0.1:{port}/hook",
+ "BASIC",
+ {"BasicAuthParameters": {"Username": "svcuser", "Password": "hunter2"}},
+ )
+ entry = {"Source": source, "DetailType": "Ping",
+ "Detail": json.dumps({"n": 1}), "EventBusName": bus_name}
+
+ eb.put_events(Entries=[entry])
+ assert _wait_until(lambda: len(captured) >= 1)
+ assert captured[0]["headers"]["authorization"].startswith("Basic ")
+
+ conn_name = "qa-eb-apidest-deauth-conn"
+ assert eb.deauthorize_connection(
+ Name=conn_name)["ConnectionState"] == "DEAUTHORIZED"
+ assert eb.describe_connection(Name=conn_name).get("AuthParameters", {}) == {}
+
+ captured.clear()
+ eb.put_events(Entries=[entry])
+ # Give delivery time to run; nothing must arrive.
+ _wait_until(lambda: len(captured) >= 1, timeout=2.0)
+ assert captured == []
+ finally:
+ server.shutdown()
+
+
+def test_eventbridge_reauthorized_connection_delivers_again(eb):
+ """Deauthorizing is not a one-way door, and the not-AUTHORIZED delivery gate
+ must not be indistinguishable from breaking the connection for good.
+ UpdateConnection supplies fresh credentials and returns the connection to
+ AUTHORIZED, so the destination delivers again — presenting the *new*
+ credentials, not the ones cleared by the deauthorize."""
+ server, captured = _start_api_dest_capture_server()
+ try:
+ port = server.server_address[1]
+ bus_name, source = _api_dest_pipeline(
+ eb,
+ "reauth",
+ f"http://127.0.0.1:{port}/hook",
+ "BASIC",
+ {"BasicAuthParameters": {"Username": "olduser", "Password": "oldpass"}},
+ )
+ entry = {"Source": source, "DetailType": "Ping",
+ "Detail": json.dumps({"n": 1}), "EventBusName": bus_name}
+ conn_name = "qa-eb-apidest-reauth-conn"
+
+ eb.put_events(Entries=[entry])
+ assert _wait_until(lambda: len(captured) >= 1)
+ # base64("olduser:oldpass")
+ assert captured[0]["headers"]["authorization"] == "Basic b2xkdXNlcjpvbGRwYXNz"
+
+ eb.deauthorize_connection(Name=conn_name)
+ captured.clear()
+ eb.put_events(Entries=[entry])
+ _wait_until(lambda: len(captured) >= 1, timeout=2.0)
+ assert captured == []
+
+ eb.update_connection(
+ Name=conn_name,
+ AuthorizationType="BASIC",
+ AuthParameters={"BasicAuthParameters": {"Username": "newuser",
+ "Password": "newpass"}},
+ )
+ assert eb.describe_connection(Name=conn_name)["ConnectionState"] == "AUTHORIZED"
+
+ captured.clear()
+ eb.put_events(Entries=[entry])
+ assert _wait_until(lambda: len(captured) >= 1)
+ # base64("newuser:newpass") — the re-authorized credentials, not the old ones
+ assert captured[0]["headers"]["authorization"] == "Basic bmV3dXNlcjpuZXdwYXNz"
+ finally:
+ server.shutdown()
+
+
+def test_eventbridge_api_destination_rejects_unknown_http_method(eb):
+ """HttpMethod rides the outbound request line, and AWS models it as an
+ enum. It was stored unvalidated and silently became POST at delivery."""
+ conn = eb.create_connection(
+ Name=f"qa-eb-method-conn-{_uuid_mod.uuid4().hex[:8]}",
+ AuthorizationType="API_KEY",
+ AuthParameters={"ApiKeyAuthParameters": {"ApiKeyName": "X-Api-Key", "ApiKeyValue": "k"}},
+ )
+ with pytest.raises(ClientError) as exc:
+ eb.create_api_destination(
+ Name=f"qa-eb-method-{_uuid_mod.uuid4().hex[:8]}",
+ ConnectionArn=conn["ConnectionArn"],
+ InvocationEndpoint="https://api.example.test/hook",
+ HttpMethod="TRACE",
+ )
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert "httpMethod" in exc.value.response["Error"]["Message"]
+
+ name = f"qa-eb-method-ok-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_api_destination(
+ Name=name, ConnectionArn=conn["ConnectionArn"],
+ InvocationEndpoint="https://api.example.test/hook", HttpMethod="PATCH",
+ )
+ with pytest.raises(ClientError):
+ eb.update_api_destination(Name=name, HttpMethod="TRACE")
+ assert eb.describe_api_destination(Name=name)["HttpMethod"] == "PATCH"
+
+
+def test_eventbridge_connection_rejects_unknown_authorization_type(eb):
+ """AuthorizationType decides which credentials are attached, and an
+ unrecognized one matched nothing — the connection was created and then
+ delivered with no credentials at all, silently."""
+ with pytest.raises(ClientError) as exc:
+ eb.create_connection(
+ Name=f"qa-eb-authtype-{_uuid_mod.uuid4().hex[:8]}",
+ AuthorizationType="basic",
+ AuthParameters={"BasicAuthParameters": {"Username": "u", "Password": "p"}},
+ )
+ assert exc.value.response["Error"]["Code"] == "ValidationException"
+ assert "authorizationType" in exc.value.response["Error"]["Message"]
+
+ name = f"qa-eb-authtype-ok-{_uuid_mod.uuid4().hex[:8]}"
+ eb.create_connection(
+ Name=name, AuthorizationType="BASIC",
+ AuthParameters={"BasicAuthParameters": {"Username": "u", "Password": "p"}},
+ )
+ with pytest.raises(ClientError):
+ eb.update_connection(Name=name, AuthorizationType="Api_Key")
+ assert eb.describe_connection(Name=name)["AuthorizationType"] == "BASIC"
+
+
+def test_eventbridge_connection_non_object_auth_parameters_still_succeeds(eb):
+ """The endpoint check reaches into AuthParameters, which is raw caller
+ JSON. A non-object value was stored verbatim and answered 200 before this
+ validation existed, and it must still — the guard has to hold at the
+ handler, not only in the helper it calls. botocore strict-validates the
+ shape, so call via raw HTTP."""
+ import urllib.request as _r
+
+ name = f"qa-eb-authparams-wire-{_uuid_mod.uuid4().hex[:8]}"
+
+ def _post(target, payload):
+ req = _r.Request(
+ f"{_ENDPOINT}/",
+ data=json.dumps(payload).encode(),
+ headers={
+ "X-Amz-Target": f"AWSEvents.{target}",
+ "Content-Type": "application/x-amz-json-1.1",
+ "Authorization": ("AWS4-HMAC-SHA256 Credential=test/20260101/"
+ "us-east-1/events/aws4_request, SignedHeaders=, Signature=x"),
+ },
+ )
+ with _r.urlopen(req) as resp:
+ return resp.status
+
+ assert _post("CreateConnection", {
+ "Name": name, "AuthorizationType": "API_KEY", "AuthParameters": "oops",
+ }) == 200
+ assert _post("UpdateConnection", {"Name": name, "AuthParameters": ["oops"]}) == 200
+
+
+# ---------------------------------------------------------------------------
+# Unit tests: _http_open / _finalize_api_dest_headers /
+# _validate_oauth_authorization_endpoint
+# ---------------------------------------------------------------------------
+
+def test_eventbridge_api_dest_headers_match_reserved_names_trimmed():
+ """A padded spelling must not slip past the reserved-header filter.
+ ``"User-Agent "`` is not in the removed set, and http.client only rejects
+ whitespace as the FIRST character of a name, so an untrimmed comparison
+ puts an attacker-chosen User-Agent on the wire beside EventBridge's own."""
+ out = _eb._finalize_api_dest_headers({
+ "User-Agent ": "pwned-agent", "Host\t": "pwned-host", "X-Keep": "kept",
+ })
+ assert out["User-Agent"] == "Amazon/EventBridge/ApiDestinations"
+ assert out["X-Keep"] == "kept"
+ assert all(k == k.strip() for k in out) # no padded spelling survives
+ assert "pwned-agent" not in out.values()
+ assert "pwned-host" not in out.values()
+
+
+def test_eventbridge_http_open_refuses_non_http_scheme():
+ """The opener-level scheme guard is the half of this that survives a state
+ restore: load_state repopulates api_destinations and connections verbatim,
+ so a record written before the API validation existed never revalidates.
+ Create/Update now reject these, which is exactly why no black-box test can
+ reach the guard — it is pinned in-process instead."""
+ import urllib.request
+
+ for bad in ("file:///etc/passwd", "ftp://127.0.0.1/token", "gopher://127.0.0.1/1"):
+ with pytest.raises(ValueError, match="unsupported URL scheme"):
+ _eb._http_open(urllib.request.Request(bad))
+
+
+def test_eventbridge_oauth_token_request_uses_the_guarded_opener():
+ """The token POST carries the client_id/client_secret, so it must ride the
+ same guarded opener as delivery rather than a bare urlopen — otherwise the
+ no-redirect handler and the scheme guard both fall off the one request
+ guaranteed to be carrying credentials. A non-http(s) AuthorizationEndpoint
+ is the cheap proof of routing: a bare urlopen would happily read the file
+ and fail later, in the JSON decode."""
+ with pytest.raises(ValueError, match="unsupported URL scheme"):
+ _eb._fetch_oauth_token({
+ "AuthorizationEndpoint": "file:///etc/passwd",
+ "ClientParameters": {"ClientID": "id", "ClientSecret": "secret"},
+ })
+
+
+def test_eventbridge_oauth_endpoint_check_tolerates_non_dict_auth_parameters():
+ """AuthParameters is raw caller JSON and botocore rejects a non-object
+ shape client-side, so only a hand-rolled request reaches this check. It
+ must stay a no-op there: v1.4.15 stored the value verbatim and answered
+ 200, and adding validation must not turn that into a 500."""
+ for junk in (None, "oops", ["x"], 5, True, ""):
+ assert _eb._validate_oauth_authorization_endpoint(junk) is None