Skip to content

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

fix(apigateway): accept presigned calls to AWS_IAM methods under AUTH=true (#2278)

iot-rocket9c83ecfcd217
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5972b49f..9aed34e7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **API Gateway — presigned calls to `AWS_IAM` REST methods under `AUTH=true`** — a request signed in its query string (`X-Amz-Signature`) with an IAM user or STS session key answered 403 `MissingAuthenticationTokenException` on an `AWS_IAM` method even when correctly signed; under `AUTH=true`, which verifies that signature, it now reaches the method as on AWS. Contributed by @iot-rocket.
### Added
- **IoT — MQTT over WebSocket evaluates the signer's IAM policies under `AUTH=true`** — a session signed with an IAM user or STS session key could connect under any client id and publish, subscribe and receive on any topic; with `AUTH=true` its CONNECT, will, PUBLISH, SUBSCRIBE and deliveries now need the key's IAM policies to allow `iot:Connect`, `iot:Publish` (and `iot:RetainPublish`), `iot:Subscribe` and `iot:Receive` on the client, topic and topic filter ARNs, with the same refusals as a certificate session. Contributed by @iot-rocket.
- **Auth — WebSocket handshakes are verified for IAM user and STS session keys** — under `AUTH=true` an MQTT-over-WebSocket upgrade or an `AWS_IAM` WebSocket `$connect` signed with such a key was accepted with a wrong secret, a date more than 5 minutes off or a wrong session token; the signature in the query string or `Authorization` header is now checked like that of other requests, IoT answers 403 `Forbidden` and expects the session token after the signed part of the URL, and API Gateway names the canonical string it expected. Contributed by @iot-rocket.
diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.py
index 86f650f8..c1511382 100644
--- a/ministack/services/apigateway_v1.py
+++ b/ministack/services/apigateway_v1.py
@@ -1927,7 +1927,11 @@ async def _authorize_request_v1(
if auth_type == "AWS_IAM":
# Signatures are verified only under AUTH; here match AWS only to the extent
# that a request with no Authorization header is rejected as unauthenticated.
- if not _header_ci(headers, "authorization"):
+ # Under AUTH a presigned request was verified before it got here.
+ from ministack.app import AUTH
+ from ministack.core.iam_evaluator import _query_signed
+
+ if not _header_ci(headers, "authorization") and not (AUTH and _query_signed(query_params or {})):
return _gw_error("MISSING_AUTHENTICATION_TOKEN", "Missing Authentication Token"), None
return None, None
if auth_type == "COGNITO_USER_POOLS":
diff --git a/tests/test_apigatewayv1.py b/tests/test_apigatewayv1.py
index fa5a7c07..4b66068c 100644
--- a/tests/test_apigatewayv1.py
+++ b/tests/test_apigatewayv1.py
@@ -1,4 +1,5 @@
import asyncio
+import contextlib
import io
import json
import os
@@ -8,10 +9,11 @@ import uuid as _uuid_mod
import zipfile
from urllib.parse import urlparse
+import boto3
import pytest
import yaml
from botocore.exceptions import ClientError
-from conftest import inprocess_iam_user, routed_request
+from conftest import inprocess_iam_user, make_client, routed_request
from ministack.core.responses import set_request_region
from ministack.services import apigateway_v1
@@ -1353,6 +1355,83 @@ def test_apigwv1_execute_aws_iam_requires_auth_header(apigw_v1, lam):
lam.delete_function(FunctionName=fname)
+def _second_access_key(user):
+ """A second access key of an ``iam_user`` user, to sign raw requests with; deleted on exit."""
+ iam = make_client("iam")
+ key = iam.create_access_key(UserName=user.name)["AccessKey"]
+ try:
+ yield key["AccessKeyId"], key["SecretAccessKey"]
+ finally:
+ iam.delete_access_key(UserName=user.name, AccessKeyId=key["AccessKeyId"])
+
+
+def _signed_rest_get(url, key, secret, *, session=False, header=False, expires=300, skew=0):
+ """Status and x-amzn-ErrorType of a GET signed by botocore for execute-api, in its query string by default."""
+ import datetime as dt
+ import urllib.error
+ import urllib.request
+ from unittest import mock
+
+ import botocore.auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
+ token = None
+ if session:
+ credentials = boto3.client("sts", endpoint_url=_endpoint, aws_access_key_id=key, aws_secret_access_key=secret,
+ region_name="us-east-1").get_session_token()["Credentials"]
+ key, secret, token = credentials["AccessKeyId"], credentials["SecretAccessKey"], credentials["SessionToken"]
+ request = AWSRequest(method="GET", url=url)
+ signer = botocore.auth.SigV4Auth(Credentials(key, secret, token), "execute-api", "us-east-1") if header else (
+ botocore.auth.SigV4QueryAuth(Credentials(key, secret, token), "execute-api", "us-east-1", expires=expires))
+ now = botocore.auth.get_current_datetime() + dt.timedelta(seconds=skew)
+ with mock.patch.object(botocore.auth, "get_current_datetime", return_value=now):
+ signer.add_auth(request)
+ prepared = request.prepare()
+ try:
+ with urllib.request.urlopen(urllib.request.Request(prepared.url, headers=dict(prepared.headers))) as resp:
+ return resp.status, None
+ except urllib.error.HTTPError as e:
+ return e.code, e.headers.get("x-amzn-ErrorType", "").split(":")[0]
+
+
+_MISSING_TOKEN = (403, "MissingAuthenticationTokenException")
+_REST_SIGNED_CASES = { # case: (signing arguments, answer under AUTH=true, answer with AUTH off)
+ "presigned": ({}, (200, None), _MISSING_TOKEN),
+ "presigned-4-min-ago-expires-60": ({"skew": -240, "expires": 60}, (200, None), _MISSING_TOKEN),
+ "presigned-session": ({"session": True}, (200, None), _MISSING_TOKEN),
+ "presigned-wrong-secret": ({"secret": "wrong"}, (403, "InvalidSignatureException"), _MISSING_TOKEN),
+ "header-signed": ({"header": True}, (200, None), (200, None)),
+}
+
+
[email protected]("case", list(_REST_SIGNED_CASES))
+def test_apigwv1_aws_iam_presigned_call_contract(auth_enabled, iam_user, apigw_v1, case):
+ """Under AUTH=true a verified presigned call reaches an AWS_IAM method, as a header-signed one does."""
+ api_id = apigw_v1.create_rest_api(name=f"v1-presigned-{_uuid_mod.uuid4().hex[:8]}")["id"]
+ try:
+ root = next(r for r in apigw_v1.get_resources(restApiId=api_id)["items"] if r["path"] == "/")
+ apigw_v1.put_method(restApiId=api_id, resourceId=root["id"], httpMethod="GET", authorizationType="AWS_IAM")
+ apigw_v1.put_integration(restApiId=api_id, resourceId=root["id"], httpMethod="GET", type="MOCK",
+ requestTemplates={"application/json": '{"statusCode": 200}'})
+ apigw_v1.put_method_response(restApiId=api_id, resourceId=root["id"], httpMethod="GET", statusCode="200")
+ apigw_v1.put_integration_response(restApiId=api_id, resourceId=root["id"], httpMethod="GET",
+ statusCode="200")
+ apigw_v1.create_deployment(restApiId=api_id, stageName="p")
+ policy = {"Version": "2012-10-17", "Statement": [
+ {"Effect": "Allow", "Action": ["execute-api:Invoke", "sts:GetSessionToken"], "Resource": "*"}]}
+ args, strict, permissive = _REST_SIGNED_CASES[case]
+ with _second_access_key(iam_user(policy=policy)) as (key, secret):
+ args = {"key": key, "secret": secret, **args}
+ answer = _signed_rest_get(f"http://{api_id}.execute-api.localhost:{_EXECUTE_PORT}/p/",
+ args.pop("key"), args.pop("secret"), **args)
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+ assert answer == (strict if auth_enabled else permissive)
+
+
def _policy_api(policy, region="us-east-1"):
"""A deployed REST API with GET /iam (AWS_IAM) and GET /open (NONE), both MOCK, and *policy*."""
set_request_region(region)