|
1 | 1 | import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src"; |
| 2 | +import type { S3ExtensionConfiguration } from "@aws-sdk/client-s3"; |
2 | 3 | import { ChecksumAlgorithm, S3 } from "@aws-sdk/client-s3"; |
3 | 4 | import type { HttpHandler, HttpRequest } from "@smithy/protocol-http"; |
4 | 5 | import { HttpResponse } from "@smithy/protocol-http"; |
| 6 | +import type { Checksum } from "@smithy/types"; |
| 7 | +import { toBase64 } from "@smithy/util-base64"; |
| 8 | +import { ChecksumStream } from "@smithy/util-stream"; |
| 9 | +import { fromUtf8 } from "@smithy/util-utf8"; |
5 | 10 | import { Readable, Transform } from "node:stream"; |
6 | 11 | import { describe, expect, test as it } from "vitest"; |
7 | 12 |
|
@@ -236,4 +241,177 @@ describe("middleware-flexible-checksums", () => { |
236 | 241 | }); |
237 | 242 | }); |
238 | 243 | }); |
| 244 | + |
| 245 | + describe("Novel checksums", () => { |
| 246 | + /** |
| 247 | + * This highly performant checksum algorithm always returns the bytes of the string "Hello, world." |
| 248 | + * and the number of bytes seen. |
| 249 | + */ |
| 250 | + class HelloWorldChecksum implements Checksum { |
| 251 | + private hash = "Hello, world."; |
| 252 | + private bytesSeen = 0; |
| 253 | + |
| 254 | + public constructor() {} |
| 255 | + |
| 256 | + public reset(): void { |
| 257 | + this.bytesSeen = 0; |
| 258 | + } |
| 259 | + |
| 260 | + public update(data: Uint8Array): void { |
| 261 | + this.bytesSeen += data.byteLength; |
| 262 | + } |
| 263 | + |
| 264 | + async digest(): Promise<Uint8Array> { |
| 265 | + return fromUtf8(this.hash + this.bytesSeen); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + class HelloWorldChecksumExtension { |
| 270 | + configure(extensions: S3ExtensionConfiguration) { |
| 271 | + extensions.addChecksumAlgorithm({ |
| 272 | + algorithmId() { |
| 273 | + return "HELLOWORLD"; |
| 274 | + }, |
| 275 | + checksumConstructor() { |
| 276 | + return HelloWorldChecksum; |
| 277 | + }, |
| 278 | + }); |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + describe("novel request checksum", () => { |
| 283 | + it("should send a request with the novel checksum in the header if the implementation is provided", async () => { |
| 284 | + const s3 = new S3({ |
| 285 | + credentials: { |
| 286 | + accessKeyId: "INTEG", |
| 287 | + secretAccessKey: "INTEG", |
| 288 | + }, |
| 289 | + extensions: [new HelloWorldChecksumExtension()], |
| 290 | + }); |
| 291 | + requireRequestsFrom(s3).toMatch({ |
| 292 | + headers: { |
| 293 | + "x-amz-checksum-helloworld": toBase64(fromUtf8("Hello, world.2")), |
| 294 | + }, |
| 295 | + }); |
| 296 | + |
| 297 | + await s3.putObject({ |
| 298 | + Bucket: "bucket", |
| 299 | + Key: "key", |
| 300 | + Body: "hi", |
| 301 | + ChecksumAlgorithm: "HELLOWORLD" as any, |
| 302 | + }); |
| 303 | + |
| 304 | + expect.assertions(1); |
| 305 | + }); |
| 306 | + |
| 307 | + it("should throw an error if the requested algorithm implementation is not available", async () => { |
| 308 | + const s3 = new S3({ |
| 309 | + credentials: { |
| 310 | + accessKeyId: "INTEG", |
| 311 | + secretAccessKey: "INTEG", |
| 312 | + }, |
| 313 | + extensions: [], |
| 314 | + }); |
| 315 | + requireRequestsFrom(s3).toMatch({ |
| 316 | + headers: { |
| 317 | + "x-amz-checksum-helloworld": toBase64(fromUtf8("Hello, world.2")), |
| 318 | + }, |
| 319 | + }); |
| 320 | + |
| 321 | + try { |
| 322 | + await s3.putObject({ |
| 323 | + Bucket: "bucket", |
| 324 | + Key: "key", |
| 325 | + Body: "hi", |
| 326 | + ChecksumAlgorithm: "HELLOWORLD" as any, |
| 327 | + }); |
| 328 | + } catch (e) { |
| 329 | + expect(e.message).toMatch(/The checksum algorithm "HELLOWORLD" is not supported by the client/); |
| 330 | + } |
| 331 | + |
| 332 | + expect.assertions(1); |
| 333 | + }); |
| 334 | + }); |
| 335 | + |
| 336 | + describe("novel response checksum", () => { |
| 337 | + it("should receive a request and verify the novel checksum", async () => { |
| 338 | + const s3 = new S3({ |
| 339 | + credentials: { |
| 340 | + accessKeyId: "INTEG", |
| 341 | + secretAccessKey: "INTEG", |
| 342 | + }, |
| 343 | + extensions: [new HelloWorldChecksumExtension()], |
| 344 | + }); |
| 345 | + |
| 346 | + requireRequestsFrom(s3) |
| 347 | + .toMatch({ |
| 348 | + hostname: "bucket.s3.us-west-2.amazonaws.com", |
| 349 | + }) |
| 350 | + .respondWith( |
| 351 | + new HttpResponse({ |
| 352 | + statusCode: 200, |
| 353 | + headers: { |
| 354 | + "x-amz-checksum-helloworld": toBase64(fromUtf8("Hello, world.2")), |
| 355 | + }, |
| 356 | + body: Readable.from(Buffer.from("hi_extra_bytes")), |
| 357 | + }) |
| 358 | + ); |
| 359 | + |
| 360 | + const get = await s3.getObject({ |
| 361 | + Bucket: "bucket", |
| 362 | + Key: "key", |
| 363 | + }); |
| 364 | + |
| 365 | + expect.assertions(3); |
| 366 | + |
| 367 | + expect(get.Body).toBeInstanceOf(ChecksumStream); |
| 368 | + try { |
| 369 | + await get.Body?.transformToByteArray(); |
| 370 | + } catch (e) { |
| 371 | + const [ex, ac] = [toBase64(fromUtf8("Hello, world.2")), toBase64(fromUtf8("Hello, world.14"))]; |
| 372 | + expect(e.message).toEqual( |
| 373 | + ` |
| 374 | +Checksum mismatch: expected "${ex}" but received "${ac}" in response header "x-amz-checksum-helloworld". |
| 375 | +`.trim() |
| 376 | + ); |
| 377 | + } |
| 378 | + }); |
| 379 | + |
| 380 | + it("should ignore the checksum header and perform no checksum validation if no matching algorithm implementation is available", async () => { |
| 381 | + const s3 = new S3({ |
| 382 | + credentials: { |
| 383 | + accessKeyId: "INTEG", |
| 384 | + secretAccessKey: "INTEG", |
| 385 | + }, |
| 386 | + extensions: [], |
| 387 | + }); |
| 388 | + |
| 389 | + requireRequestsFrom(s3) |
| 390 | + .toMatch({ |
| 391 | + hostname: "bucket.s3.us-west-2.amazonaws.com", |
| 392 | + }) |
| 393 | + .respondWith( |
| 394 | + new HttpResponse({ |
| 395 | + statusCode: 200, |
| 396 | + headers: { |
| 397 | + "x-amz-checksum-helloworld": toBase64(fromUtf8("Hello, world.2")), |
| 398 | + }, |
| 399 | + body: Readable.from(Buffer.from("hi_extra_bytes")), |
| 400 | + }) |
| 401 | + ); |
| 402 | + |
| 403 | + const get = await s3.getObject({ |
| 404 | + Bucket: "bucket", |
| 405 | + Key: "key", |
| 406 | + }); |
| 407 | + |
| 408 | + expect.assertions(3); |
| 409 | + |
| 410 | + expect(get.Body).not.toBeInstanceOf(ChecksumStream); |
| 411 | + |
| 412 | + const objectContent = await get.Body?.transformToString(); |
| 413 | + expect(objectContent).toEqual("hi_extra_bytes"); |
| 414 | + }); |
| 415 | + }); |
| 416 | + }); |
239 | 417 | }); |
0 commit comments