Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: addressed greptile review comment

=97820fb71621
diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.ts
index 92c3d0494d..55fdcafd7a 100644
--- a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.ts
+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.test.ts
@@ -77,6 +77,47 @@ describe("parseSignedGetCallerIdentityRequest", () => {
expect(parse({ headers: { ...signedHeaders, "Content-Length": 43 } }).headers).not.toHaveProperty("content-length");
});
+ test("targets the global STS endpoint when the request was signed for it", () => {
+ expect(parse({ headers: { ...signedHeaders, Host: "STS.amazonaws.com" } })).toMatchObject({
+ url: "https://sts.amazonaws.com"
+ });
+ });
+
+ test("rejects the global STS host signed for a region other than us-east-1", () => {
+ expect(() =>
+ parse({ headers: { Authorization: authorizationFor("eu-west-1"), Host: "sts.amazonaws.com" } })
+ ).toThrow("invalid host header");
+ });
+
+ test.each([
+ "attacker-bucket.s3.amazonaws.com",
+ "sts.us-west-2.amazonaws.com",
+ "sts.us-east-1.amazonaws.com.evil.com",
+ "sts.us-east-1.amazonaws.com:443",
+ ""
+ ])("rejects host %j", (host) => {
+ expect(() => parse({ headers: { ...signedHeaders, Host: host } })).toThrow("invalid host header");
+ });
+
+ test("rejects a second spelling or a non-string host", () => {
+ expect(() => parse({ headers: { ...signedHeaders, host: "attacker-bucket.s3.amazonaws.com" } })).toThrow(
+ "invalid host header"
+ );
+ expect(() => parse({ headers: { ...signedHeaders, Host: null } })).toThrow("invalid host header");
+ });
+
+ test("normalizes space-padded header names the way the HTTP client does", () => {
+ expect(() => parse({ headers: { ...signedHeaders, " Host ": "attacker-bucket.s3.amazonaws.com" } })).toThrow(
+ "invalid host header"
+ );
+ expect(() => parse({ headers: { ...signedHeaders, " authorization": authorizationFor("us-west-2") } })).toThrow(
+ "invalid authorization header"
+ );
+ expect(parse({ headers: { ...signedHeaders, " Content-Length ": "0" } }).headers).not.toHaveProperty(
+ " content-length "
+ );
+ });
+
test.each(["us-east-1", "us-gov-west-1", "eu-central-2", "ap-southeast-4"])("accepts region %s", (region) => {
expect(parse({ headers: { Authorization: authorizationFor(region) } }).url).toBe(
`https://sts.${region}.amazonaws.com`
diff --git a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts
index bdad01e74e..4c1fcf2c6a 100644
--- a/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts
+++ b/backend/src/services/identity-aws-auth/identity-aws-auth-fns.ts
@@ -113,10 +113,9 @@ const awsRegionFromAuthorizationHeader = (authorizationHeader: string): string |
return credentialParts && credentialParts.length >= 3 ? credentialParts[2] : null;
};
-// Left to the HTTP client, which derives them from the STS URL and the body it sends; forwarding them lets the
-// caller pick the server (Host) or leave bytes on a pooled connection for the next request (framing headers)
+// Left to the HTTP client, which derives them from the body it sends; forwarding them lets the caller leave bytes on a
+// pooled connection that the next request on it would pick up
const STRIPPED_STS_HEADERS = new Set([
- "host",
"content-length",
"transfer-encoding",
"connection",
@@ -128,6 +127,9 @@ const STRIPPED_STS_HEADERS = new Set([
"expect"
]);
+const GLOBAL_STS_HOST = "sts.amazonaws.com";
+const GLOBAL_STS_SIGNING_REGION = "us-east-1";
+
// The caller's input is replayed to STS and the answer is trusted, so it is narrowed to the one request STS must answer
export const parseSignedGetCallerIdentityRequest = ({
iamHttpRequestMethod,
@@ -155,8 +157,17 @@ export const parseSignedGetCallerIdentityRequest = ({
}
const headers: Record<string, string> = {};
+ let signedHost: string | undefined;
Object.entries(rawHeaders).forEach(([name, value]) => {
- const headerName = name.toLowerCase();
+ // The HTTP client trims names too, so " Host " would otherwise slip past the checks below and still be sent
+ const headerName = name.trim().toLowerCase();
+ if (headerName === "host") {
+ if (signedHost !== undefined || typeof value !== "string") {
+ throw new BadRequestError({ message: "Signed STS request has an invalid host header" });
+ }
+ signedHost = value.trim().toLowerCase();
+ return;
+ }
if (STRIPPED_STS_HEADERS.has(headerName)) return;
// A second spelling of a name would let the value that is validated differ from the one that is sent
if (headerName in headers || typeof value !== "string") {
@@ -170,5 +181,16 @@ export const parseSignedGetCallerIdentityRequest = ({
throw new BadRequestError({ message: "Invalid AWS region" });
}
- return { url: `https://sts.${region}.amazonaws.com`, headers, body };
+ // SigV4 always signs Host, so the URL is built from the signed value; only the two names STS answers on are allowed
+ const regionalStsHost = `sts.${region}.amazonaws.com`;
+ let stsHost: string;
+ if (signedHost === undefined || signedHost === regionalStsHost) {
+ stsHost = regionalStsHost;
+ } else if (signedHost === GLOBAL_STS_HOST && region === GLOBAL_STS_SIGNING_REGION) {
+ stsHost = GLOBAL_STS_HOST;
+ } else {
+ throw new BadRequestError({ message: "Signed STS request has an invalid host header" });
+ }
+
+ return { url: `https://${stsHost}`, headers, body };
};