Skip to content
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

Add HTML global attribute nonce #8764

Merged
merged 2 commits into from
Feb 1, 2021
Merged

Add HTML global attribute nonce #8764

merged 2 commits into from
Feb 1, 2021

Conversation

Elchi3
Copy link
Member

@Elchi3 Elchi3 commented Jan 15, 2021

Helping out on #7303 by adding the global attribute nonce.
mdn/content PR: mdn/content#1318

@github-actions github-actions bot added the data:html Compat data for HTML elements. https://developer.mozilla.org/docs/Web/HTML label Jan 15, 2021
@Elchi3 Elchi3 marked this pull request as ready for review January 15, 2021 14:05
@Elchi3
Copy link
Member Author

Elchi3 commented Jan 15, 2021

Marking as ready, I'm not sure what the story for initial nonce support is here. Probably sometime around the initial CSP support.

@foolip
Copy link
Contributor

foolip commented Jan 16, 2021

This was enabled in Chromium 36.

Longer version:

I failed to determine when support for the content attribute was shipped from source code archeology, and the tests in WPT didn't run going back to very old Chrome, so I wrote this test:

<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'nonce-abc'">
<pre>nonce test passes if one PASS and no FAIL logged:

</pre>
<script nonce=abc>
function log(msg) {
  document.querySelector('pre').textContent += '\n' + msg;
}
log('PASS script with correct nonce ran');
</script>
<script nonce=xyz>log('FAIL script with wrong nonce ran');</script>

Using that I bisected support to introduced in Chrome 36. In Chrome 35 there's a console message saying that the nonce-abc bit isn't recognized, so I'm pretty confident.

Chrome 36 was shipped on 2014-07-16 so the timing matched Intent to Ship: CSP 1.1 [script/style]-[hash/nonce], but I couldn't pinpoint the commit.

@foolip
Copy link
Contributor

foolip commented Jan 16, 2021

As for Safari, it was implemented in WebKit in https://trac.webkit.org/changeset/197944/webkit, behavior and reflected IDL attributes at the same time. (The content attribute was already in the code, probably left over after https://trac.webkit.org/changeset/121883/webkit.)

That was WebKit trunk 602.1.22 which would be Safari 10, matching api.HTMLElement.nonce.

Testing Safari 9.1 and 10.1 on BrowserStack is consistent with this, it's not supported in Safari 9.1 but is in 10.1.

@foolip
Copy link
Contributor

foolip commented Jan 16, 2021

One more thing came up when looking at WebKit. WebKit also has nonce in HTMLLinkElement.idl and behavior in HTMLLinkElement.cpp added in https://trac.webkit.org/changeset/197944/webkit. So at least in WebKit, nonce might also be supported on stylesheets loaded with <link>.

@foolip
Copy link
Contributor

foolip commented Jan 16, 2021

I see whatwg/html#1820 added <link rel=stylesheet nonce> to the spec, and it's now in https://html.spec.whatwg.org/#fetch-and-process-the-linked-resource. But I'm not sure how to test for it. @sideshowbarker do you know CSP well enough to test this?

@sideshowbarker
Copy link
Member

I see whatwg/html#1820 added <link rel=stylesheet nonce> to the spec, and it's now in html.spec.whatwg.org/#fetch-and-process-the-linked-resource.

aha — grrr, I really wish we didn’t split stuff across specs like that

But I'm not sure how to test for it. @sideshowbarker do you know CSP well enough to test this?

yeah I can — I’ll post a comment after trying it

@sideshowbarker
Copy link
Member

sideshowbarker commented Jan 17, 2021

I see whatwg/html#1820 added <link rel=stylesheet nonce> to the spec, and it's now in html.spec.whatwg.org/#fetch-and-process-the-linked-resource.

OK, after walking through the following:

  1. https://html.spec.whatwg.org/#fetch-and-process-the-linked-resource
  2. https://html.spec.whatwg.org/#default-fetch-and-process-the-linked-resource
  3. https://html.spec.whatwg.org/#create-a-potential-cors-request
  4. https://fetch.spec.whatwg.org/#concept-request-nonce-metadata
  5. https://w3c.github.io/webappsec-csp/#match-nonce-to-source-list

…I find that I can’t actually locate a call site where that Does nonce match source list? algorithm gets called for link elements. I find call sites for style-src and style-src-elem and for CSP script directives. But I don’t find where it ever would get called for link elements.

To put it in other terms, it’s clear that steps 1 to 4 above — the HTML spec and Fetch spec steps — cause the right “cryptographic nonce metadata” from the nonce attribute to be set for the request; but it’s not clear to me where CSP states that “cryptographic nonce metadata” would get consumed/evaluated in the case of link elements.

(P.S. I’ve still not actually tested browsers for link nonce support.)

@foolip
Copy link
Contributor

foolip commented Jan 17, 2021

Hmm, sounds like there might be some dead code in the spec here. The code I found in WebKit also strikes me as unlikely to ever cause a request to be blocked, so maybe this feature was only ever half finished?

Testing the behavior would be great too, that's what I couldn't figure out how to do.

@Elchi3
Copy link
Member Author

Elchi3 commented Jan 19, 2021

I couldn't find much in WPT on nonce, it is used here though:
https://github.com/web-platform-tests/wpt/blob/master/preload/link-header-preload-nonce.html.headers
(results: https://wpt.fyi/results/preload/link-header-preload-nonce.html?label=master&label=experimental&aligned)

I believe the Link header is supposed to be semantically identical to <link>.

@ddbeck
Copy link
Collaborator

ddbeck commented Jan 28, 2021

I've read this through this a couple of times and I admit I'm still a bit confused. What questions remain to be answered to make this ready to be merged?

@Elchi3
Copy link
Member Author

Elchi3 commented Jan 28, 2021

I think we were figuring out if we need additional notes but actually I think we should merge this as is and follow up if needed.

@sideshowbarker
Copy link
Member

I think we were figuring out if we need additional notes but actually I think we should merge this as is and follow up if needed.

I agree. I’ve been planning to test but I’ve not managed to make time to do that yet — but I don’t think that should block this from being merged. I’ve opened #8919 to track that task.

@Elchi3
Copy link
Member Author

Elchi3 commented Feb 1, 2021

Forced pushed so CI runs and allows me to merge.

@Elchi3 Elchi3 merged commit 8eab7d6 into mdn:master Feb 1, 2021
@Elchi3 Elchi3 deleted the html-ga-nonce branch February 1, 2021 09:12
germain-gg pushed a commit to germain-gg/browser-compat-data that referenced this pull request Feb 1, 2021
…icture

* upstream/master: (1123 commits)
  Remove Chromium 89 from String.at / Array.at / TypedArray.at (mdn#8869)
  Add worker_support info for CacheStorage (mdn#8783)
  Remove several needless "Enabled by default" notes (mdn#8899)
  Add HTML global attribute nonce (mdn#8764)
  api.Navigator.vibrate - Firefox for Android doesn't vibrate (mdn#7172)
  Mark MediaSource's onsourceclose as not supported in Firefox (mdn#8881)
  Update Florian's ownership (mdn#8893)
  Mention fix for Chrome's broken PDF loading (mdn#8867)
  Fill out Chrome data for html.elements.source.{sizes,srcset} (mdn#8889)
  Weekly data release for 2021-01-28
  Add text-decoration-thickness for Opera 73+ (mdn#8872)
  Update :is and :where pseudo-classes for Chrome (mdn#7375)
  Add note re Safari <9 partial srcset/sizes support (mdn#7353)
  Update data for when href (not xlink:href) can be used in SVG (mdn#6603)
  Add top-level await (mdn#8807)
  TouchList: Add Safari Desktop and Safari iOS versions (mdn#8848)
  Update Firefox versions to account for Firefox 85 release (mdn#8864)
  Fix page_action.show_matches support for Android (mdn#8844)
  Update Safari support for devicechange_event (mdn#8863)
  Add HTTPS-only to privacy.network (mdn#8830)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data:html Compat data for HTML elements. https://developer.mozilla.org/docs/Web/HTML
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants