Problem/Motivation
Filed when discussing #2501975: Determine how to update code that currently joins strings in SafeMarkup::set().
When providing test coverage for XSS, we often use an example script tag containing JS like:
alert('XSS');
When this is checking for the sanitization of a single complete string this is fine. E.g. with this:
<script>alert('XSS')</script>
If we had a test where $field_output were this:
alert('XSS');
Sanitization for the placeholders would escape the single quotes, the bad markup <script>alert('XSS');</script> would not be found, and our presumed test assertion would pass.
However, I believe it is possible in JavaScript to assemble a string without using single quotes. Suppose instead we tested $field_output as this:
var foo = String(/Haha sucker/); foo = foo.substring(1, foo.length-1); alert(foo);
In that case, $field_output would not be escaped, the exact string of the exploit would be found by the test assertion, and the test would fail, informing us that we had some insecure code.
Proposed resolution
Use a JS snippet that does not contain any HTML special characters including single quotes.
Remaining tasks
- Determine if this is actually worth considering.
- See if there are any tests in core that would provide more complete converage if they used a JS snippet that did not contain quotes.
- Update those tests.
- Document this recommendation for testing somewhere?
- Look for places where this as additional test coverage would be good?
User interface changes
N/A
API changes
None, unless we wanted to make some API addition of an XSS testing helper or something.
Comments
Comment #1
JvE commentedIn my testing I use
String.fromCharCode(88,83,83)Comment #2
pwolanin commentedshould this be higher priority?
Comment #3
pwolanin commentedbump
Comment #4
dawehnerAt least our xss tests do have some checks for that, see
core/tests/Drupal/Tests/Component/Utility/XssTest.php:271Comment #18
smustgrave commentedThank you for creating this issue to improve Drupal.
We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #19
xjmYes, still a valid issue. We shouldn't auto-close security improvements.
Comment #20
xjmRemoved IS reference to
SafeMarkupthat might make it seem like this was outdated when in fact it's just about XSS, which unfortunately remains perennially with us.Comment #21
xjmMore complete removal of references to old API.
Comment #22
xjmThere's an example of a relevant test case that we should add in
core/modules/system/tests/modules/test_page_test/src/Controller/Test.php-- see theescapedScript()andunEscapedScript()methods on that fixture. We should probably add an additional pair of test cases for (e.g.)escapedScriptWithoutQuotes()or something.Comment #24
xjmAdding some keywords to the title so that this issue is findable. ("XSS" is three characters and therefore excluded from the search index.)
Comment #25
xjm#1 is really what we should be doing, and we should add an API method for it.
Comment #26
xjmComment #27
smustgrave commentedJust want to keep the tag for stats, want to show how many were responded to. Thank you for following up!
Comment #28
xjmOops, sorry. :)