Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

AWSv4 post request should include the body when signing #49

Closed
YOU54F opened this issue Dec 9, 2019 · 4 comments
Closed

AWSv4 post request should include the body when signing #49

YOU54F opened this issue Dec 9, 2019 · 4 comments

Comments

@YOU54F
Copy link

YOU54F commented Dec 9, 2019

Hi,

When we send AWSv4 POST requests using scripts which have been converted from a postman collections using this tool, we should include the stringified body in the signing request as part of the auth method

    body: config.data

in const options

      const options = {
        method: "POST",
        protocol: address.protocol(),
        hostname: address.hostname(),
        port: address.port(),
        path: address.path() + address.search(),
        region: `${pm[Var]("awsRegion")}`,
        service: `${pm[Var]("awsServiceName")}`,
        body: config.data
      };

So this generated portion of the script

    auth(config, Var) {
      const address = new URI(config.address);
      const options = {
        method: "POST",
        protocol: address.protocol(),
        hostname: address.hostname(),
        port: address.port(),
        path: address.path() + address.search(),
        region: `${pm[Var]("awsRegion")}`,
        service: `${pm[Var]("awsServiceName")}`
      };
      const credential = {
        accessKeyId: `${pm[Var]("AccessKeyId")}`,
        secretAccessKey: `${pm[Var]("SecretAccessKey")}`,
        sessionToken: `${pm[Var]("SessionToken")}`
      };
      const signed = aws4.sign(options, credential);
      const [path, query = ""] = signed.path.split("?");
      config.address = new URI()
        .protocol(address.protocol())
        .hostname(signed.hostname)
        .path(path)
        .query(query)
        .toString();
      Object.assign(config.headers, signed.headers);
    }

Would become

    auth(config, Var) {
      const address = new URI(config.address);
      const options = {
        method: "POST",
        protocol: address.protocol(),
        hostname: address.hostname(),
        port: address.port(),
        path: address.path() + address.search(),
        region: `${pm[Var]("awsRegion")}`,
        service: `${pm[Var]("awsServiceName")}`,
        body: config.data
      };
      const credential = {
        accessKeyId: `${pm[Var]("AccessKeyId")}`,
        secretAccessKey: `${pm[Var]("SecretAccessKey")}`,
        sessionToken: `${pm[Var]("SessionToken")}`
      };
      const signed = aws4.sign(options, credential);
      const [path, query = ""] = signed.path.split("?");
      config.address = new URI()
        .protocol(address.protocol())
        .hostname(signed.hostname)
        .path(path)
        .query(query)
        .toString();
      Object.assign(config.headers, signed.headers);
    }

Otherwise we will see the error come back from the API gateway

The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.\\n\\nThe Canonical String for this request should have been <redacted>

PS. Thank you for the tool!!!

@simskij
Copy link
Contributor

simskij commented Feb 3, 2020

Hi, and thank you for the report! 🙏🏼

Would you mind checking out branch fix/49 and see whether the introduced change solves the issue you're experiencing?

@YOU54F
Copy link
Author

YOU54F commented Feb 11, 2020

@simskij change is spot on my mate, feel free to merge!

@YOU54F
Copy link
Author

YOU54F commented Feb 12, 2020

Please let me know when this is published to npm @simskij thanks again

@YOU54F YOU54F closed this as completed Feb 12, 2020
@simskij
Copy link
Contributor

simskij commented Feb 22, 2020

Sure! I think it will be in about a week or so 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants