Gathering detailed insights and metrics for aws4
Gathering detailed insights and metrics for aws4
Gathering detailed insights and metrics for aws4
Gathering detailed insights and metrics for aws4
Signs and prepares Node.js requests using AWS Signature Version 4
npm install aws4
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
704 Stars
163 Commits
176 Forks
9 Watching
2 Branches
11 Contributors
Updated on 20 Nov 2024
Minified
Minified + Gzipped
JavaScript (99.89%)
HTML (0.11%)
Cumulative downloads
Total Downloads
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
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.
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
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
or host
(will try to be determined from service
and region
if not given)method
(will use 'GET'
if not given or 'POST'
if there is a body
)path
(will use '/'
if not given)body
(will use ''
if not given)service
(will try to be calculated from hostname
or host
if not given)region
(will try to be calculated from hostname
or host
or use 'us-east-1'
if not given)signQuery
(to sign the query instead of adding an Authorization
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 use hostname
or host
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 a body
)headers['Date']
(used to calculate the signature date if given, otherwise new Date
is used)Your AWS credentials (which can be found in your AWS console) can be specified in one of two ways:
1aws4.sign(requestOptions, { 2 secretAccessKey: "<your-secret-access-key>", 3 accessKeyId: "<your-access-key-id>", 4 sessionToken: "<your-session-token>" 5})
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.
With npm do:
npm install aws4
Can also be used in the browser.
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
Reason
1 existing vulnerabilities detected
Details
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
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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