Skip to content

Commit 46e5e8d

Browse files
authored
fix(core): add error metadata indicating clock skew correction (#5830)
1 parent e1ba507 commit 46e5e8d

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { AwsSdkSigV4Signer } from "./AwsSdkSigV4Signer";
2+
3+
describe(AwsSdkSigV4Signer.name, () => {
4+
it("sets clockSkewCorrected metadata in error handler if systemClockOffset was updated", async () => {
5+
const signer = new AwsSdkSigV4Signer();
6+
7+
let error: Error | any;
8+
try {
9+
signer.errorHandler({
10+
config: {
11+
systemClockOffset: 30 * 60 * 1000,
12+
},
13+
})(
14+
Object.assign(new Error("uh oh"), {
15+
$metadata: {},
16+
$response: {
17+
headers: {
18+
date: new Date().toISOString(),
19+
},
20+
statusCode: 500,
21+
},
22+
})
23+
);
24+
} catch (e) {
25+
error = e as Error;
26+
}
27+
28+
expect((error as any).$metadata.clockSkewCorrected).toBe(true);
29+
});
30+
});

packages/core/src/httpAuthSchemes/aws_sdk/AwsSdkSigV4Signer.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ interface AwsSdkSigV4AuthSigningProperties {
4545
*/
4646
interface AwsSdkSigV4Exception extends ServiceException {
4747
ServerTime?: string;
48+
$metadata: ServiceException["$metadata"] & {
49+
clockSkewCorrected?: boolean;
50+
};
4851
}
4952

5053
/**
@@ -104,11 +107,13 @@ export class AwsSdkSigV4Signer implements HttpSigner {
104107
const serverTime: string | undefined =
105108
(error as AwsSdkSigV4Exception).ServerTime ?? getDateHeader((error as AwsSdkSigV4Exception).$response);
106109
if (serverTime) {
107-
const config = throwSigningPropertyError(
108-
"config",
109-
signingProperties.config as AwsSdkSigV4Config | undefined
110-
);
110+
const config = throwSigningPropertyError("config", signingProperties.config as AwsSdkSigV4Config | undefined);
111+
const initialSystemClockOffset = config.systemClockOffset;
111112
config.systemClockOffset = getUpdatedSystemClockOffset(serverTime, config.systemClockOffset);
113+
const clockSkewCorrected = config.systemClockOffset !== initialSystemClockOffset;
114+
if (clockSkewCorrected && (error as AwsSdkSigV4Exception).$metadata) {
115+
(error as AwsSdkSigV4Exception).$metadata.clockSkewCorrected = true;
116+
}
112117
}
113118
throw error;
114119
};
@@ -117,10 +122,7 @@ export class AwsSdkSigV4Signer implements HttpSigner {
117122
successHandler(httpResponse: HttpResponse | unknown, signingProperties: Record<string, unknown>): void {
118123
const dateHeader = getDateHeader(httpResponse);
119124
if (dateHeader) {
120-
const config = throwSigningPropertyError(
121-
"config",
122-
signingProperties.config as AwsSdkSigV4Config | undefined
123-
);
125+
const config = throwSigningPropertyError("config", signingProperties.config as AwsSdkSigV4Config | undefined);
124126
config.systemClockOffset = getUpdatedSystemClockOffset(dateHeader, config.systemClockOffset);
125127
}
126128
}

0 commit comments

Comments
 (0)