Skip to content

chore(middleware): make base64Encoder/Decoder optional #4138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/middleware-apply-body-checksum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/util-base64": "*",
"@aws-sdk/is-array-buffer": "*",
"@aws-sdk/protocol-http": "*",
"@aws-sdk/types": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MetadataBearer,
Pluggable,
} from "@aws-sdk/types";
import { toBase64 } from "@aws-sdk/util-base64";

import { Md5BodyChecksumResolvedConfig } from "./md5Configuration";

Expand All @@ -30,11 +31,12 @@ export const applyMd5BodyChecksumMiddleware =
digest = options.streamHasher(options.md5, body);
}

const base64Encoder = options.base64Encoder ?? toBase64;
request = {
...request,
headers: {
...headers,
"content-md5": options.base64Encoder(await digest),
"content-md5": base64Encoder(await digest),
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Encoder, HashConstructor, StreamHasher } from "@aws-sdk/types";
export interface Md5BodyChecksumInputConfig {}
interface PreviouslyResolved {
md5: HashConstructor;
base64Encoder: Encoder;
base64Encoder?: Encoder;
streamHasher: StreamHasher<any>;
}

Expand All @@ -17,7 +17,7 @@ export interface Md5BodyChecksumResolvedConfig {
* The function that will be used to convert binary data to a base64-encoded string.
* @internal
*/
base64Encoder: Encoder;
base64Encoder?: Encoder;
/**
* A function that, given a hash constructor and a stream, calculates the hash of the streamed value.
* @internal
Expand Down
1 change: 1 addition & 0 deletions packages/middleware-flexible-checksums/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@aws-crypto/crc32": "2.0.0",
"@aws-crypto/crc32c": "2.0.0",
"@aws-sdk/util-base64": "*",
"@aws-sdk/is-array-buffer": "*",
"@aws-sdk/protocol-http": "*",
"@aws-sdk/types": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface PreviouslyResolved {
* The function that will be used to convert binary data to a base64-encoded string.
* @internal
*/
base64Encoder: Encoder;
base64Encoder?: Encoder;

/**
* A function that can calculate the length of a body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,21 @@ describe(flexibleChecksumsMiddleware.name, () => {
const mockRequestValidationModeMember = "mockRequestValidationModeMember";
const mockInput = { [mockRequestValidationModeMember]: "ENABLED" };
const mockResponseAlgorithms = ["ALGO1", "ALGO2"];

const handler = flexibleChecksumsMiddleware(mockConfig, {
...mockMiddlewareConfig,
input: mockInput,
requestValidationModeMember: mockRequestValidationModeMember,
responseAlgorithms: mockResponseAlgorithms,
})(mockNext, {});
const mockBase64Encoder = jest.fn().mockReturnValue(mockChecksum);

const handler = flexibleChecksumsMiddleware(
{ ...mockConfig, base64Encoder: mockBase64Encoder },
{
...mockMiddlewareConfig,
input: mockInput,
requestValidationModeMember: mockRequestValidationModeMember,
responseAlgorithms: mockResponseAlgorithms,
}
)(mockNext, {});

await handler(mockArgs);
expect(validateChecksumFromResponse).toHaveBeenCalledWith(mockResult.response, {
config: mockConfig,
config: { ...mockConfig, base64Encoder: mockBase64Encoder },
responseAlgorithms: mockResponseAlgorithms,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BuildMiddleware,
MetadataBearer,
} from "@aws-sdk/types";
import { toBase64 } from "@aws-sdk/util-base64";

import { PreviouslyResolved } from "./configuration";
import { getChecksumAlgorithmForRequest } from "./getChecksumAlgorithmForRequest";
Expand All @@ -27,7 +28,8 @@ export const flexibleChecksumsMiddleware =

const { request } = args;
const { body: requestBody, headers } = request;
const { base64Encoder, streamHasher } = config;
const { streamHasher } = config;
const base64Encoder = config.base64Encoder ?? toBase64;
const { input, requestChecksumRequired, requestAlgorithmMember } = middlewareConfig;

const checksumAlgorithm = getChecksumAlgorithmForRequest(input, {
Expand Down Expand Up @@ -80,7 +82,7 @@ export const flexibleChecksumsMiddleware =
// @ts-ignore Element implicitly has an 'any' type for input[requestValidationModeMember]
if (requestValidationModeMember && input[requestValidationModeMember] === "ENABLED") {
validateChecksumFromResponse(result.response as HttpResponse, {
config,
config: { ...config, base64Encoder },
responseAlgorithms,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { AwsCrc32 } from "@aws-crypto/crc32";
import { AwsCrc32c } from "@aws-crypto/crc32c";
import { HashConstructor } from "@aws-sdk/types";

import { PreviouslyResolved } from "./configuration";
import { ChecksumAlgorithm } from "./constants";

export interface SelectChecksumAlgorithmInputConfig {
md5: HashConstructor;
sha1: HashConstructor;
sha256: HashConstructor;
}
/**
* Returns the function that will compute the checksum for the given {@link ChecksumAlgorithm}.
*/
export const selectChecksumAlgorithmFunction = (
checksumAlgorithm: ChecksumAlgorithm,
config: PreviouslyResolved
config: SelectChecksumAlgorithmInputConfig
): HashConstructor =>
({
[ChecksumAlgorithm.MD5]: config.md5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe(validateChecksumFromResponse.name, () => {
const mockConfig = {
streamHasher: jest.fn(),
base64Encoder: jest.fn(),
} as unknown as PreviouslyResolved;
} as any;

const mockBody = {};
const mockHeaders = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpResponse } from "@aws-sdk/protocol-http";
import { HashConstructor } from "@aws-sdk/types";
import { Encoder, HashConstructor } from "@aws-sdk/types";

import { PreviouslyResolved } from "./configuration";
import { ChecksumAlgorithm } from "./constants";
Expand All @@ -9,7 +9,7 @@ import { getChecksumLocationName } from "./getChecksumLocationName";
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";

export interface ValidateChecksumFromResponseOptions {
config: PreviouslyResolved;
config: PreviouslyResolved & { base64Encoder: Encoder };

/**
* Defines the checksum algorithms clients SHOULD look for when validating checksums
Expand Down