-
Notifications
You must be signed in to change notification settings - Fork 1k
USWDS - File Input: Replace inline javascript to meet content security policy (CSP) standards #5997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
24bf5d2
to
91937e9
Compare
91937e9
to
8356ad9
Compare
Sorry @mahoneycm I didn't realize there was a formatting issue in my last push. Fixed now! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there @jeffpw-goog
Thanks for your continued work here! The PR description and testing steps were great.
This is working like a charm and follows standard USWDS code practices. Flagging others for additional review now!
Testing repoTo streamline testing, I copied @jeffpw-goog demo repo work in our repo so we can utilize the preview build! You can use this link and complete 1-3 without the second group of instructions to recreate the CSP policy demo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this with different file types and did not have any problems. .doc and .pages had the same icon (W), .xls and .numbers had the same icon (X) and movie and .mp4 used the same camera. all filenames are correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this contribution. This is a good security fix in general.
I've tested on the website comparing develop with this branch on uswds/uswds-site#2923. I did discover some small differences.
Before | After |
---|---|
live | feature |
All variants using different types of files. Also used the file selector and drag and drop.
8356ad9
to
4a14b39
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jeffpw-goog! Confirming that xlsx and doc icons are appearing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Summary
Fixed the file input component to meet content security policy standards. Inline Javascript is forbidden by typical content security policies (CSPs), and this replaces several inline
onerror
handlers in the file input component with addEventListener to avoid that violation.Breaking change
This is not a breaking change.
Related issue
Closes #5990
Related pull requests
Changelog: uswds/uswds-site#2912
Preview link
None
Problem statement
The USWDS library should be compatible with sites that apply a content security policy (CSP). Currently the file input component uses
onerror
inline event handlers, which are prohibited by CSP (source), so the component does not work correctly under a CSP.Solution
onerror
with calls toaddEventListener
.setPreviewFallback
to remove code duplicationMajor changes
None
Testing and review
Steps to test:
npm start
Steps to test with a CSP policy:
Tip
To streamline testing, I copied @jeffpw-goog demo repo work in our sandbox repo so we can utilize the preview build!
CSP Preview link →
You can use this link and complete the steps about without reproducing a CSP policy demo via the steps below.
Leaving the steps below which outline how Jeff made the testing repo
Added by @mahoneycm
src/_includes/head.html
:<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-mynonce' 'strict-dynamic' https:" />
nonce="mynonce"
to the<link rel="preload" ... as="script">
tagsrc/_layouts/default.html
:nonce="mynonce"
to the<script>
tagnpm install "https://github.com/jeffpw-goog/uswds/tree/file-input-csp" --save
npm install && npm init && npm start