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
Trusted Types Violation Fixes #2
Conversation
'http://www.w3.org/2000/svg', | ||
'foreignObject' | ||
) | ||
foreignObject.setAttribute('width', 30) |
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.
Instead of using setAttribute
, you can use the standard SVGAnimatedLength
API:
foreignObject.width.baseVal.value = 30;
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.
Fixed
packages/react-dev-overlay/src/internal/components/Overlay/maintain--tab-focus.ts
Show resolved
Hide resolved
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.
Looks great, ship it!
var fragment = document.createElement('div') | ||
fragment.innerHTML = | ||
'<svg><foreignObject width="30" height="30">\n <input type="text"/>\n </foreignObject></svg>' | ||
// Returns <foreignObject width="30" height="30">\n <input type="text"/>\n </foreignObject> |
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.
Nit: Clean this comment up and add more context. E.g.
// Construct <foreignObject width="30" height="30"><input type="text"/></foreignObject>
// in a way that does not raise a Trusted Types violation.
1be0e28
to
6c87c89
Compare
2963d1c
to
e3374db
Compare
533ba1a
to
f59caf8
Compare
Feature
fixes #number
contributing.md
Documentation
There are three tsec violations that are fixed in this PR:
1. ban-element-innerhtml-assignments: maintain--tab-focus.ts
The innerHTML assignment here is unsafe as a string is being used that could contain an XSS attack. The solution chosen was to replace the string containing HTML with programmatically-created DOM elements. This removes the Trusted Types violation as there is no longer a string passed in that can contain an XSS attack.
Notes on solution:
<svg>
tag is omitted completely since the original snippet returns fragment.firstChild.firstChild. The first firstChild omits the<div>
, and the second firstChild omits the<svg>
, so to remove unnecessary code the created elements start at the foreignObject level.The code was tested to be equivalent by rendering both the original code and the re-written code in a browser to see if they evaluate to the same thing in the DOM. The DOM elements styles were then compared to ensure that they were identical.
2. ban-window-stringfunctiondef: packages/next/lib/recursive-delete.ts
The setTimeout function caused a Trusted Types violation because if a string is passed in as the callback, XSS can occur. The solution to this problem is to ensure that only functions can be passed to setTimeout. There is only one call to the sleep function and it passes in numbers only, so this can be enforced without breaking the application logic. In the process of doing this, promisify has been removed and the promise has been created explicitly.
The code was tested in a sample application to ensure it behaved as expected.
3. ban-window-stringfunctiondef: packages/next/client/dev/fouc.ts
This file also uses setTimeout, so the call was wrapped in a
safeSetTimeout
call that specifies that the callback argument is not a string.