Security header CSP: add nonce support
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 38/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- aws, javascript
- Domain
- cloud
Research direction
Start by reviewing the CloudFront Functions and Lambda@Edge viewer request/response limitations described in the issue, along with the S3, API Gateway, and CloudFront flow. Compare the requested response-body manipulation with the existing platform entry points. Done means a supported function path can modify the origin response body before delivery, including adding nonce attributes to HTML blocks.
Written by the indexing model from the issue text.
Description
My organization needs for compliancy a really secure CSP to be on our cloudfront distribution. Currently I host my index.html file on a API gateway instead of our s3 bucket to allow me add nonces to style an scripts blocks.
Lambda function I use on this API gate way is as follows:
const crypto = require('crypto')
const fs = require('fs')
const cheerio = require('cheerio')
exports.handler = (event, context, callback) => {
const $ = cheerio.load(fs.readFileSync('./index.html').toString('utf-8'))
const nonce = crypto.randomBytes(16).toString('base64')
$('script').attr('nonce', nonce)
$('style').attr('nonce', nonce)
$('meta[property="csp-nonce"]').attr('content', nonce)
callback(null, {
statusCode: 200,
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Content-Security-Policy': `default-src 'none'; img-src 'self'; script-src 'nonce-${nonce}' 'self'; 'nonce-${nonce}' 'self'; object-src 'none'`,
'Content-Type': 'text/html; charset=utf-8',
'Expires': '0',
'Pragma': 'no-cache',
'Referrer-Policy': 'same-origin',
'Strict-Transport-Security': 'max-age=31536001; includeSubDomains',
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
'X-XSS-Protection': '1; mode=block'
},
body: $.html()
})
}
This method is made by @csarkosh you can also read his medium post about this issue he tried to solve:
https://medium.com/@csarkosh/my-experience-getting-an-a-from-mozillas-observatory-tool-on-aws-f0abf12811a1
For my stack it would be really beneficial that cloudfront support adding a nonce to CSP headers natively.
Currently Cloudfront functions / Lambda edge let you only change viewer requests/viewer response. What I need is to be able to change the body of the origin response.
So that I can:
Scan the HTML file I get from S3 on <script> and <style> blocks
Add via the function a attribute to these blocks ex <style nonce="test">
So what I ask is to:
Response body manipulation from within a CloudFront Function and/or Lambda@Edge function. You fetch the content from cache or the origin and I want to be able to manipulate that response body inside a Cloudfront function before the response is sent to the client.
Edit 12-5-2021:
added more clarification based on comments below.
- Dominant language
- JavaScript
- Stars
- 533
- Forks
- 76
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws-samples/amazon-cloudfront-functions
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Change request body Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
-
Redirection problem with Nextjs application deployed on aws s3 and cloundfront as static application Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
aws-samples/amazon-cloudfront-functions#44 · 1 reaction ·
-
Decrypt a Cookie Open
Difficulty 5/5 Over a week Newbie friendliness 25/100
aws-samples/amazon-cloudfront-functions#41 · 1 comment ·
All issues in aws-samples/amazon-cloudfront-functions
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·