Skip to content

Commit

Permalink
Merge pull request #1259 from twm/style-nonce
Browse files Browse the repository at this point in the history
Fix missing <style nonce> attrs
  • Loading branch information
twm authored Jan 7, 2025
2 parents 59fceef + e4abe88 commit 9888d0b
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 28 deletions.
10 changes: 5 additions & 5 deletions yarrharr/application.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2013, 2015, 2016, 2017, 2018, 2020, 2022, 2023 Tom Most <[email protected]>
# Copyright © 2013, 2015, 2016, 2017, 2018, 2020, 2022, 2023, 2025 Tom Most <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -303,20 +303,20 @@ def getChildWithDefault(self, name, request):
request.setHeader(b"X-Content-Type-Options", b"nosniff")
request.setHeader(b"Cross-Origin-Opener-Policy", b"same-origin")

script_nonce = b64encode(os.urandom(32))
request.requestHeaders.setRawHeaders(b"Yarrharr-Script-Nonce", [script_nonce])
csp_nonce = b64encode(os.urandom(32))
request.requestHeaders.setRawHeaders(b"Yarrharr-Csp-Nonce", [csp_nonce])
request.setHeader(
b"Content-Security-Policy",
(
# b"default-src 'none'; "
b"img-src *; "
b"script-src 'self' 'nonce-%s'; "
b"style-src 'self'; "
b"style-src 'self' 'nonce-%s'; "
b"frame-ancestors 'none'; "
b"form-action 'self'; "
b"report-uri /csp-report"
)
% (script_nonce,),
% (csp_nonce, csp_nonce),
)

return super().getChildWithDefault(name, request)
Expand Down
10 changes: 5 additions & 5 deletions yarrharr/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2022 Tom Most <[email protected]>
# Copyright © 2022, 2025 Tom Most <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -29,13 +29,13 @@ def csp(request):
"""
if settings.YARRHARR_SCRIPT_NONCE:
try:
nonce = request.headers["Yarrharr-Script-Nonce"]
csp_nonce = request.headers["Yarrharr-Csp-Nonce"]
except KeyError:
if os.environ.get("YARRHARR_TESTING") == "yes":
# Only ignore this in unit tests so we fail safe in production.
nonce = None
csp_nonce = None
else:
raise
else:
nonce = None
return {"script_nonce": nonce}
csp_nonce = None
return {"csp_nonce": csp_nonce}
2 changes: 1 addition & 1 deletion yarrharr/templates/components.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</template>


<script type="module" nonce="{{ script_nonce }}">
<script type="module" nonce="{{ csp_nonce }}">
{% url 'api-flags' as flag_api_url %}
const flagsApi = "{{ flag_api_url|escapejs }}";

Expand Down
2 changes: 1 addition & 1 deletion yarrharr/templates/feed_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block content %}

<style>
<style nonce="{{ csp_nonce }}">
.feed-add {
margin: 2rem auto;
padding: 1.5rem 2rem 2rem 2rem;
Expand Down
4 changes: 0 additions & 4 deletions yarrharr/templates/feed_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
{% block title %}{{ feed.title }} - Edit{% endblock %}

{% block content %}
<style>
/* TODO */
</style>

<div id="yarrharr" class="layout-narrow">

{% include "header.html" %}
Expand Down
4 changes: 0 additions & 4 deletions yarrharr/templates/feed_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
{% block title %}Feeds{% endblock %}

{% block content %}
<style>
/* TODO */
</style>

<div id="yarrharr" class="layout-narrow">

{% include "header.html" %}
Expand Down
4 changes: 2 additions & 2 deletions yarrharr/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

</div>

<script type="module" nonce="{{ script_nonce }}">
<script type="module" nonce="{{ csp_nonce }}">
const b = document.getElementById("layout-button");
const y = document.getElementById("yarrharr");

Expand Down Expand Up @@ -79,7 +79,7 @@
readFromStorage();
</script>

<script type="module" nonce="{{ script_nonce }}">
<script type="module" nonce="{{ csp_nonce }}">
const b = document.getElementById("fullscreen-button");
b.onclick = e => {
if (document.fullscreenElement) {
Expand Down
2 changes: 1 addition & 1 deletion yarrharr/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block title %}Home{% endblock %}

{% block content %}
<style>
<style nonce="{{ csp_nonce }}">
.home-view {
margin: 0 auto;
max-width: var(--layout-max-width);
Expand Down
4 changes: 0 additions & 4 deletions yarrharr/templates/label_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
{% block title %}{{ label.text }} - Edit{% endblock %}

{% block content %}
<style>
/* TODO */
</style>

<div id="yarrharr" class="layout-narrow">

{% include "header.html" %}
Expand Down
2 changes: 1 addition & 1 deletion yarrharr/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{% block content %}

<style>
<style nonce="{{ csp_nonce }}">
.login-page {
display: flex;
flex-flow: column nowrap;
Expand Down

0 comments on commit 9888d0b

Please sign in to comment.