Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
aws4-axios
Advanced tools
The aws4-axios package is a middleware for Axios that automatically signs AWS requests using AWS Signature Version 4. It simplifies the process of making authenticated HTTP requests to AWS services.
AWS Request Signing
This feature allows you to make signed HTTP requests to AWS services using Axios. The aws4-axios middleware automatically handles the signing process, making it easier to interact with AWS APIs securely.
const axios = require('axios');
const aws4 = require('aws4-axios');
const client = aws4(axios);
client.get('https://my-api.execute-api.us-east-1.amazonaws.com/prod/resource')
.then(response => console.log(response.data))
.catch(error => console.error(error));
The aws-sdk package is the official AWS SDK for JavaScript, which provides a comprehensive set of tools for interacting with AWS services. Unlike aws4-axios, which focuses on request signing, aws-sdk offers a wide range of functionalities including service-specific clients, request signing, and more.
Axios is a popular HTTP client for Node.js and the browser. While it does not provide AWS request signing out of the box, it can be used in conjunction with other libraries like aws4 to achieve similar functionality. aws4-axios builds on top of Axios to provide seamless AWS request signing.
The aws4 package is a standalone library for signing AWS requests using Signature Version 4. It can be used with any HTTP client, including Axios, to manually sign requests. aws4-axios integrates this functionality directly into Axios, providing a more streamlined experience.
This is a request interceptor for the Axios HTTP request library to allow requests to be signed with an AWSv4 signature.
This may be useful for accessing AWS services protected with IAM auth such as an API Gateway.
yarn | npm |
---|---|
yarn add aws4-axios | npm install --save aws4-axios |
This interceptor is heavily dependent on Axios internals, so minor changes to them can cause the interceptor to fail.
Please make sure you are using one of the following versions of Axios before submitting issues etc.
Axios Version | Supported? |
---|---|
< 1.4.0 | ❌ No |
>= 1.4.0 <= 1.6.7 | ✅ Yes |
> 1.6.7 | Unknown |
To add an interceptor to the default Axios client:
import axios from "axios";
import { aws4Interceptor } from "aws4-axios";
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
},
});
axios.interceptors.request.use(interceptor);
// Requests made using Axios will now be signed
axios.get("https://example.com/foo").then((res) => {
// ...
});
Or you can add the interceptor to a specific instance of an Axios client:
import axios from "axios";
import { aws4Interceptor } from "aws4-axios";
const client = axios.create();
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
},
});
client.interceptors.request.use(interceptor);
// Requests made using Axios will now be signed
client.get("https://example.com/foo").then((res) => {
// ...
});
You can also pass AWS credentials in explicitly (otherwise taken from process.env)
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
},
credentials: {
accessKeyId: "",
secretAccessKey: "",
},
});
You can also pass a custom CredentialsProvider
factory instead
const customCredentialsProvider = {
getCredentials: async () => {
return Promise.resolve({
accessKeyId: "custom-provider-access-key-id",
secretAccessKey: "custom-provider-secret-access-key",
});
},
};
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
},
credentials: customCredentialsProvider,
});
The interface for options changed in v3. You should now pass a single object with configuration.
The previous options object is now nested under the property options
.
E.g (v2).
const interceptor = aws4Interceptor({
region: "eu-west-2",
service: "execute-api",
assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
});
would become (v3):
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
},
});
If you passed a custom credential provider, this is now done via the credentials
property.
E.g (v2).
const interceptor = aws4Interceptor(
{
region: "eu-west-2",
service: "execute-api",
},
{
accessKeyId: "AKIAIOSFODNN7EXAMPLE",
secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
}
);
would become (v3):
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
},
credentials: {
accessKeyId: "AKIAIOSFODNN7EXAMPLE",
secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
},
});
You can pass a parameter to assume the IAM Role with AWS STS and use the assumed role credentials to sign the request. This is useful when doing cross-account requests.
const interceptor = aws4Interceptor({
options: {
region: "eu-west-2",
service: "execute-api",
assumeRoleArn: "arn:aws:iam::111111111111:role/MyRole",
assumeRoleSessionName: "MyApiClient", // optional, default value is "axios"
},
});
Obtained credentials are cached and refreshed as needed after they expire.
You can use expirationMarginSec
parameter to set the number of seconds
before the received credentials expiration time to invalidate the cache.
This allows setting a safety margin. Default to 5 seconds.
Thanks goes to these wonderful people (emoji key):
Florian Bischoff 💻 | Ruben van Rooij 💻 | Roman 👀 | Maciej Radzikowski ⚠️ 💻 | Christopher Kruse ⚠️ | James Hu 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
Axios request interceptor for signing requests with AWSv4
The npm package aws4-axios receives a total of 106,020 weekly downloads. As such, aws4-axios popularity was classified as popular.
We found that aws4-axios demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.