Skip to content

Commit

Permalink
TP2000-1644 Strengthen Content Security Policy configuration (#1393)
Browse files Browse the repository at this point in the history
* Bump django-csp to 3.8

* Add missing object-src CSP directive

* Add missing base-uri CSP directive

* Add missing trusted-types CSP directive

* Add missing strict-dynamic CSP directive

* Create default Trusted Types policy
  • Loading branch information
dalecannon authored Jan 27, 2025
1 parent 2cfaeb2 commit eef617b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions common/static/common/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const imagePath = (name) => images(name, true);

require.context("govuk-frontend/govuk/assets");

import "./trustedTypes";
import { initAll } from "govuk-frontend";

import showHideCheckboxes from "./showHideCheckboxes";
Expand Down
12 changes: 12 additions & 0 deletions common/static/common/js/trustedTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import DOMPurify from "dompurify";

/**
* Creates a default Trusted Types policy that serves as a fallback policy
* to sanitise direct sink usage in third-party dependencies.
*/
if (typeof window.trustedTypes !== "undefined") {
window.trustedTypes.createPolicy("default", {
createHTML: (to_escape) =>
DOMPurify.sanitize(to_escape, { RETURN_TRUSTED_TYPE: true }),
});
}
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"chart.js": "^3.9.1",
"chartjs-adapter-moment": "^1.0.0",
"css-loader": "^5.2.6",
"dompurify": "^3.2.3",
"file-loader": "^6.2.0",
"govuk-frontend": "^3.15.0",
"govuk-react": "^0.10.6",
Expand Down Expand Up @@ -78,4 +79,4 @@
"pre-commit": "^1.2.2",
"react-test-renderer": "^18.2.0"
}
}
}
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-r requirements.txt

django_debug_toolbar
django_debug_toolbar==5.0.1
pre-commit
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defusedxml==0.7.*
dj-database-url==0.5.0
django-chunk-upload-handlers==0.0.13
django-crispy-forms==1.14.0
django-csp==3.6
django-csp==3.8
django-cte==1.3.1
django-extensions==3.2.3
django-filter==23.5
Expand Down
5 changes: 5 additions & 0 deletions settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
"https://tagmanager.google.com/",
)
CSP_SCRIPT_SRC = (
"'strict-dynamic'",
"'self'",
"'unsafe-eval'",
"'unsafe-inline'",
Expand All @@ -231,6 +232,10 @@
"ajax.googleapis.com/",
)
CSP_FONT_SRC = ("'self'", "'unsafe-inline'")
CSP_OBJECT_SRC = ("'none'",)
CSP_BASE_URI = ("'none'",)
CSP_REQUIRE_TRUSTED_TYPES_FOR = ("'script'",)
CSP_TRUSTED_TYPES = ("tap#webpack", "dompurify", "default")
CSP_INCLUDE_NONCE_IN = ("script-src",)
CSP_REPORT_ONLY = False

Expand Down
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = {
// (they are picked up by `collectstatic`)
publicPath: "/assets/webpack_bundles/",
filename: "[name]-[hash].js",
trustedTypes: {
policyName: "tap#webpack",
},
},

plugins: [
Expand Down

0 comments on commit eef617b

Please sign in to comment.