Skip to content

fix(parser): validate the region parsed from Host and handle api.* hosts (#403) - #417

Merged
scttfrdmn merged 3 commits into
mainfrom
fix/403-region-from-host
Jul 31, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
fix/403-region-from-host

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #403
Refs #401 — this is one of that issue's two acceptance criteria; the Price List GetProducts emulation is the other and stays on v0.80.0.

Branched off main and independent of the other open PRs, so it gets CI immediately.

The bug

extractRegionFromHost("api.pricing.us-east-1.amazonaws.com") returned "pricing". With .amazonaws.com stripped, parts is [api, pricing, us-east-1]: the s3 loop doesn't fire, parts[1] isn't execute-api, so it fell through to the <service>.<region> assumption and returned parts[1] — the second label of a three-label host.

The part worth more than the symptom

The reporter's second suggestion is the one that matters, so this PR leads with it. Handling the api. prefix fixes one host layout; the function failing open is the class of bug. It returned whichever label sat where a region usually sits, so a caller had no way to distinguish a parse failure from a real region — the wrong answer arrived looking exactly like a right one, which is the same shape of defect as the rest of this batch.

Every candidate is now checked against the shape of a region code before being returned:

  • two-letter geography (us, ap, il, mx, cn, …)
  • one or two lowercase words, which covers both us-east-1 and the four-segment partition forms us-gov-west-1 / us-iso-east-1
  • a trailing numeric ordinal

A layout that doesn't match yields "", which routes the request through the fallbacks extractRegion already had (parser.go:392-406): the SigV4 credential scope, then defaultRegion. The credential scope is the more trustworthy signal anyway, so failing closed also means it now gets its turn — TestExtractRegion_UnparseableHostFallsBackToAuth pins that.

Hand-rolled rather than a regex: the emulator package uses regexp nowhere in non-test source, and this is four cheap checks on a hot path (every request parses its Host).

Two hosts that look like regressions and aren't

my-lb-1234.us-east-1.elb.amazonaws.com and my-domain.us-east-1.es.amazonaws.com reach the <service>.<region> branch and keep returning us-east-1. That is correct — both genuinely carry the region in the second label, just not by the <service>.<region> convention — and the shape check passes it through. Called out because they read like collateral damage at a glance; there are table rows asserting each.

Verified against

The region table in the AWS General Reference, which is where the accepted shapes come from: the longest ordinary code is ap-southeast-7, the two-word geographies are the GovCloud pair, and the two-letter geography holds across all 36 regions listed plus the China regions.

Tests

TestExtractRegionFromHost is table-driven over 28 rows in three groups:

  • The fix: both Pricing endpoints (us-east-1, ap-south-1 — the only two the real Price List API has).
  • Shapes that must pass: us-gov-west-1, ap-southeast-4, il-central-1, mx-central-1, cn-northwest-1, and every layout that already worked (path-style and virtual-hosted S3, a dotted bucket name, execute-api, plain <service>.<region>, and a host with a port).
  • Shapes that must fail, one row per check so a regression names its own cause: two segments, five segments, a three-letter geography, a non-numeric ordinal, digits inside a compass word, an uppercase region, a trailing hyphen.

Plus two tests at the ParseAWSRequest boundary, since the helper being right doesn't prove the RequestContext a plugin sees is. TestExtractRegion_APIPrefixedHost deliberately sends no Authorization header — a real pricing client signs for us-east-1, so the SigV4 fallback would have masked a still-broken parser.

extractRegionFromHost, regionCandidate, isLowerAlpha and extractRegion are all at 100%.

Gates

go build ✅ · go vet ✅ · golangci-lint run ./... 0 issues ✅ · make test (race) ✅ · test/e2e ✅ · make docs-reference-check ✅ · make scan-fs 0 vulns ✅

…sts (#403)

extractRegionFromHost returned "pricing" for api.pricing.us-east-1.amazonaws.com.
With ".amazonaws.com" stripped, parts is [api, pricing, us-east-1]: the s3 loop
does not fire, parts[1] is not "execute-api", so it fell through to the
"<service>.<region>" assumption and returned parts[1] — the second label of a
three-label host.

The reported symptom is one host layout, but the shape of the bug is the whole
function failing open: it returned whichever label sat where a region usually
sits, so a caller could not tell a parse failure from a real region. Every
candidate is now checked against the shape of a region code — two-letter
geography, one or two lowercase words, trailing ordinal — covering the
four-segment partition forms (us-gov-west-1) as well as the ordinary ones.
A layout that does not match yields "", which routes the request through the
fallbacks extractRegion already had: the SigV4 credential scope, then
defaultRegion.

Two hosts that look like they should now fail keep working, and deliberately so:
my-lb-1234.us-east-1.elb.amazonaws.com and my-domain.us-east-1.es.amazonaws.com
carry a genuine region in the second label, and the shape check passes it.

Refs #401, whose acceptance criteria include this fix.
@scttfrdmn scttfrdmn added this to the v0.79.0 milestone Jul 31, 2026
@scttfrdmn scttfrdmn added type: bug Something is not working correctly area: server Issues relating to the server component labels Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 059016f into main Jul 31, 2026
12 checks passed
@scttfrdmn
scttfrdmn deleted the fix/403-region-from-host branch August 1, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: server Issues relating to the server component type: bug Something is not working correctly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parser: extractRegionFromHost returns the service name for api.<service>.<region> hosts

1 participant