Skip to content

Commit dabbfaa

Browse files
authored
Refine the transform interceptor to only match amazon location service URLs (#39)
* Refine the transform interceptor to only match amazon location service urls * Fixed to account for GovCloud and some minor feedback fixes
1 parent 05a548e commit dabbfaa

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/cognito/index.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ describe("AuthHelper for Cognito", () => {
1313
const region = "us-west-2";
1414
const cognitoIdentityPoolId = `${region}:TEST-IDENTITY-POOL-ID`;
1515
const url = "https://maps.geo.us-west-2.amazonaws.com/";
16+
const govCloudUrl = "https://maps.geo-fips.us-gov-west-1.amazonaws.com/";
1617
const nonAWSUrl = "https://example.com/";
18+
const nonLocationAWSUrl = "https://my.cool.service.us-west-2.amazonaws.com/";
1719
const mockedCredentials = {
1820
identityId: "identityId",
1921
accessKeyId: "accessKeyId",
@@ -162,6 +164,35 @@ describe("AuthHelper for Cognito", () => {
162164
expect(credential).toContain(mockedCredentials.accessKeyId);
163165
});
164166

167+
it("getMapAuthenticationOptions should contain transformRequest function to sign the AWS GovCloud Urls using our custom signer", async () => {
168+
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
169+
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;
170+
const originalUrl = new URL(govCloudUrl);
171+
const signedUrl = new URL(transformRequest(govCloudUrl).url);
172+
173+
// Host and pathname should still be the same
174+
expect(signedUrl.hostname).toStrictEqual(originalUrl.hostname);
175+
expect(signedUrl.pathname).toStrictEqual(originalUrl.pathname);
176+
177+
const searchParams = signedUrl.searchParams;
178+
expect(searchParams.size).toStrictEqual(6);
179+
180+
// Verify these search params exist on the signed url
181+
// We don't need to test the actual values since they are non-deterministic or constants
182+
const expectedSearchParams = ["X-Amz-Algorithm", "X-Amz-Date", "X-Amz-SignedHeaders", "X-Amz-Signature"];
183+
expectedSearchParams.forEach((value) => {
184+
expect(searchParams.has(value)).toStrictEqual(true);
185+
});
186+
187+
// We can expect the session token to match exactly as passed in
188+
const securityToken = searchParams.get("X-Amz-Security-Token");
189+
expect(securityToken).toStrictEqual(mockedCredentials.sessionToken);
190+
191+
// The credential starts with our access key, the rest is generated
192+
const credential = searchParams.get("X-Amz-Credential");
193+
expect(credential).toContain(mockedCredentials.accessKeyId);
194+
});
195+
165196
it("getMapAuthenticationOptions transformRequest function should pass-through non AWS Urls unchanged", async () => {
166197
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
167198
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;
@@ -171,6 +202,15 @@ describe("AuthHelper for Cognito", () => {
171202
});
172203
});
173204

205+
it("getMapAuthenticationOptions transformRequest function should pass-through AWS Urls that aren't for the Amazon Location Service unchanged", async () => {
206+
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
207+
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;
208+
209+
expect(transformRequest(nonLocationAWSUrl)).toStrictEqual({
210+
url: nonLocationAWSUrl,
211+
});
212+
});
213+
174214
it("getLocationClientConfig should provide credentials from cognito", async () => {
175215
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
176216
const additionalLocationClientConfig = authHelper.getLocationClientConfig();

src/cognito/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export async function withIdentityPoolId(
4242
return {
4343
getMapAuthenticationOptions: () => ({
4444
transformRequest: (url: string) => {
45-
// Only sign aws URLs
46-
if (url.includes("amazonaws.com")) {
45+
// Only sign Amazon Location Service URLs
46+
if (url.match("https://maps.(geo|geo-fips).(.*).amazonaws.com")) {
4747
return {
4848
url: Signer.signUrl(url, region, {
4949
access_key: credentials.accessKeyId,

0 commit comments

Comments
 (0)