|
| 1 | +/// <reference types="mocha" /> |
| 2 | +import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http"; |
| 3 | +import { HttpHandlerOptions } from "@smithy/types"; |
| 4 | +import { S3 } from "../../src/S3"; |
| 5 | +import chai from "chai"; |
| 6 | +import chaiAsPromised from "chai-as-promised"; |
| 7 | + |
| 8 | +chai.use(chaiAsPromised); |
| 9 | +const { expect } = chai; |
| 10 | + |
| 11 | +/** |
| 12 | + * Throws an expected exception that contains the serialized request. |
| 13 | + */ |
| 14 | +class ExpectedRequestSerializationError extends Error { |
| 15 | + constructor(readonly request: HttpRequest) { |
| 16 | + super(); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class RequestSerializationTestHandler implements HttpHandler { |
| 21 | + async handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }> { |
| 22 | + throw new ExpectedRequestSerializationError(request); |
| 23 | + } |
| 24 | + updateHttpClientConfig(key: never, value: never): void {} |
| 25 | + httpHandlerConfigs() { |
| 26 | + return {}; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +describe("Dot Segment in URI Label", () => { |
| 31 | + const client = new S3({ |
| 32 | + credentials: { accessKeyId: "mockAccessKeyId", secretAccessKey: "mockSecretAccessKey" }, |
| 33 | + requestHandler: new RequestSerializationTestHandler(), |
| 34 | + }); |
| 35 | + |
| 36 | + it("S3PreservesLeadingDotSegmentInUriLabel", async () => { |
| 37 | + try { |
| 38 | + await client.getObject({ |
| 39 | + Bucket: "mybucket", |
| 40 | + Key: "../key.txt", |
| 41 | + }); |
| 42 | + fail("Expected an EXPECTED_REQUEST_SERIALIZATION_ERROR to be thrown"); |
| 43 | + } catch (err) { |
| 44 | + if (!(err instanceof ExpectedRequestSerializationError)) { |
| 45 | + fail(err); |
| 46 | + } |
| 47 | + const r = err.request; |
| 48 | + expect(r.method).to.eql("GET"); |
| 49 | + expect(r.path).to.eql("/../key.txt"); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it("S3PreservesEmbeddedDotSegmentInUriLabel", async () => { |
| 54 | + try { |
| 55 | + await client.getObject({ |
| 56 | + Bucket: "mybucket", |
| 57 | + Key: "foo/../key.txt", |
| 58 | + }); |
| 59 | + fail("Expected an EXPECTED_REQUEST_SERIALIZATION_ERROR to be thrown"); |
| 60 | + } catch (err) { |
| 61 | + if (!(err instanceof ExpectedRequestSerializationError)) { |
| 62 | + fail(err); |
| 63 | + } |
| 64 | + const r = err.request; |
| 65 | + expect(r.method).to.eql("GET"); |
| 66 | + expect(r.path).to.eql("/foo/../key.txt"); |
| 67 | + } |
| 68 | + }); |
| 69 | +}); |
0 commit comments