Signs and prepares Node.js requests using AWS Signature Version 4
Installations
npm install aws4
Developer
mhart
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
22.5.1
NPM Version
10.8.2
Statistics
704 Stars
163 Commits
176 Forks
9 Watching
2 Branches
11 Contributors
Updated on 20 Nov 2024
Bundle Size
8.16 kB
Minified
2.85 kB
Minified + Gzipped
Languages
JavaScript (99.89%)
HTML (0.11%)
Total Downloads
Cumulative downloads
Total Downloads
6,098,618,173
Last day
-1.2%
4,510,567
Compared to previous day
Last week
5.9%
25,663,010
Compared to previous week
Last month
25.1%
96,874,185
Compared to previous month
Last year
-8%
950,754,594
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
aws4
A small utility to sign vanilla Node.js http(s) request options using Amazon's AWS Signature Version 4.
If you want to sign and send AWS requests using fetch()
, then check out aws4fetch – otherwise you can also bundle this library for use in older browsers.
The only AWS service I know of that doesn't support v4 is SimpleDB (it only supports AWS Signature Version 2).
It also provides defaults for a number of core AWS headers and request parameters, making it very easy to query AWS services, or build out a fully-featured AWS library.
Example
1var https = require('https') 2var aws4 = require('aws4') 3 4// to illustrate usage, we'll create a utility function to request and pipe to stdout 5function request(opts) { https.request(opts, function(res) { res.pipe(process.stdout) }).end(opts.body || '') } 6 7// aws4 will sign an options object as you'd pass to http.request, with an AWS service and region 8var opts = { host: 'my-bucket.s3.us-west-1.amazonaws.com', path: '/my-object', service: 's3', region: 'us-west-1' } 9 10// aws4.sign() will sign and modify these options, ready to pass to http.request 11aws4.sign(opts, { accessKeyId: '', secretAccessKey: '' }) 12 13// or it can get credentials from process.env.AWS_ACCESS_KEY_ID, etc 14aws4.sign(opts) 15 16// for most AWS services, aws4 can figure out the service and region if you pass a host 17opts = { host: 'my-bucket.s3.us-west-1.amazonaws.com', path: '/my-object' } 18 19// usually it will add/modify request headers, but you can also sign the query: 20opts = { host: 'my-bucket.s3.amazonaws.com', path: '/?X-Amz-Expires=12345', signQuery: true } 21 22// and for services with simple hosts, aws4 can infer the host from service and region: 23opts = { service: 'sqs', region: 'us-east-1', path: '/?Action=ListQueues' } 24 25// and if you're using us-east-1, it's the default: 26opts = { service: 'sqs', path: '/?Action=ListQueues' } 27 28aws4.sign(opts) 29console.log(opts) 30/* 31{ 32 host: 'sqs.us-east-1.amazonaws.com', 33 path: '/?Action=ListQueues', 34 headers: { 35 Host: 'sqs.us-east-1.amazonaws.com', 36 'X-Amz-Date': '20121226T061030Z', 37 Authorization: 'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/sqs/aws4_request, ...' 38 } 39} 40*/ 41 42// we can now use this to query AWS 43request(opts) 44/* 45<?xml version="1.0"?> 46<ListQueuesResponse xmlns="https://queue.amazonaws.com/doc/2012-11-05/"> 47... 48*/ 49 50// aws4 can infer the HTTP method if a body is passed in 51// method will be POST and Content-Type: 'application/x-www-form-urlencoded; charset=utf-8' 52request(aws4.sign({ service: 'iam', body: 'Action=ListGroups&Version=2010-05-08' })) 53/* 54<ListGroupsResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/"> 55... 56*/ 57 58// you can specify any custom option or header as per usual 59request(aws4.sign({ 60 service: 'dynamodb', 61 region: 'ap-southeast-2', 62 method: 'POST', 63 path: '/', 64 headers: { 65 'Content-Type': 'application/x-amz-json-1.0', 66 'X-Amz-Target': 'DynamoDB_20120810.ListTables' 67 }, 68 body: '{}' 69})) 70/* 71{"TableNames":[]} 72... 73*/ 74 75// you can also specify extra headers to ignore during signing 76request(aws4.sign({ 77 host: '07tjusf2h91cunochc.us-east-1.aoss.amazonaws.com', 78 method: 'PUT', 79 path: '/my-index', 80 body: '{"mappings":{}}', 81 headers: { 82 'Content-Type': 'application/json', 83 'X-Amz-Content-Sha256': 'UNSIGNED-PAYLOAD' 84 }, 85 extraHeadersToIgnore: { 86 'content-length': true 87 } 88})) 89 90// and headers to include that would normally be ignored 91request(aws4.sign({ 92 service: 'mycustomservice', 93 path: '/whatever', 94 headers: { 95 'Range': 'bytes=200-1000, 2000-6576, 19000-' 96 }, 97 extraHeadersToInclude: { 98 'range': true 99 } 100})) 101 102 103// The raw RequestSigner can be used to generate CodeCommit Git passwords 104var signer = new aws4.RequestSigner({ 105 service: 'codecommit', 106 host: 'git-codecommit.us-east-1.amazonaws.com', 107 method: 'GIT', 108 path: '/v1/repos/MyAwesomeRepo', 109}) 110var password = signer.getDateTime() + 'Z' + signer.signature() 111 112// see example.js for examples with other services
API
aws4.sign(requestOptions, [credentials])
Calculates and populates any necessary AWS headers and/or request
options on requestOptions
. Returns requestOptions
as a convenience for chaining.
requestOptions
is an object holding the same options that the Node.js
http.request
function takes.
The following properties of requestOptions
are used in the signing or
populated if they don't already exist:
hostname
orhost
(will try to be determined fromservice
andregion
if not given)method
(will use'GET'
if not given or'POST'
if there is abody
)path
(will use'/'
if not given)body
(will use''
if not given)service
(will try to be calculated fromhostname
orhost
if not given)region
(will try to be calculated fromhostname
orhost
or use'us-east-1'
if not given)signQuery
(to sign the query instead of adding anAuthorization
header, defaults to false)extraHeadersToIgnore
(an object with lowercase header keys to ignore when signing, eg{ 'content-length': true }
)extraHeadersToInclude
(an object with lowercase header keys to include when signing, overriding any ignores)headers['Host']
(will usehostname
orhost
or be calculated if not given)headers['Content-Type']
(will use'application/x-www-form-urlencoded; charset=utf-8'
if not given and there is abody
)headers['Date']
(used to calculate the signature date if given, otherwisenew Date
is used)
Your AWS credentials (which can be found in your AWS console) can be specified in one of two ways:
- As the second argument, like this:
1aws4.sign(requestOptions, { 2 secretAccessKey: "<your-secret-access-key>", 3 accessKeyId: "<your-access-key-id>", 4 sessionToken: "<your-session-token>" 5})
- From
process.env
, such as this:
export AWS_ACCESS_KEY_ID="<your-access-key-id>"
export AWS_SECRET_ACCESS_KEY="<your-secret-access-key>"
export AWS_SESSION_TOKEN="<your-session-token>"
(will also use AWS_ACCESS_KEY
and AWS_SECRET_KEY
if available)
The sessionToken
property and AWS_SESSION_TOKEN
environment variable are optional for signing
with IAM STS temporary credentials.
Installation
With npm do:
npm install aws4
Can also be used in the browser.
Thanks
Thanks to @jed for his dynamo-client lib where I first committed and subsequently extracted this code.
Also thanks to the official Node.js AWS SDK for giving me a start on implementing the v4 signature.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
Reason
4 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 3
Reason
Found 1/27 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/mhart/aws4/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/mhart/aws4/build.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/build.yml:41
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More