Repository navigation
fix(parser): validate the region parsed from Host and handle api.* hosts (#403) - #417
Merged
Merged
Conversation
…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.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Jul 31, 2026
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #403
Refs #401 — this is one of that issue's two acceptance criteria; the Price List
GetProductsemulation is the other and stays on v0.80.0.Branched off
mainand 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.comstripped,partsis[api, pricing, us-east-1]: thes3loop doesn't fire,parts[1]isn'texecute-api, so it fell through to the<service>.<region>assumption and returnedparts[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:
us,ap,il,mx,cn, …)us-east-1and the four-segment partition formsus-gov-west-1/us-iso-east-1A layout that doesn't match yields
"", which routes the request through the fallbacksextractRegionalready had (parser.go:392-406): the SigV4 credential scope, thendefaultRegion. The credential scope is the more trustworthy signal anyway, so failing closed also means it now gets its turn —TestExtractRegion_UnparseableHostFallsBackToAuthpins that.Hand-rolled rather than a regex: the
emulatorpackage usesregexpnowhere 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.comandmy-domain.us-east-1.es.amazonaws.comreach the<service>.<region>branch and keep returningus-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
TestExtractRegionFromHostis table-driven over 28 rows in three groups:us-east-1,ap-south-1— the only two the real Price List API has).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).Plus two tests at the
ParseAWSRequestboundary, since the helper being right doesn't prove theRequestContexta plugin sees is.TestExtractRegion_APIPrefixedHostdeliberately sends noAuthorizationheader — a real pricing client signs forus-east-1, so the SigV4 fallback would have masked a still-broken parser.extractRegionFromHost,regionCandidate,isLowerAlphaandextractRegionare all at 100%.Gates
go build✅ ·go vet✅ ·golangci-lint run ./...0 issues ✅ ·make test(race) ✅ ·test/e2e✅ ·make docs-reference-check✅ ·make scan-fs0 vulns ✅