From 0c0634991798add106ffac1303ec2b92b883eca5 Mon Sep 17 00:00:00 2001 From: vishalpatil-45 Date: Mon, 16 Feb 2026 01:53:46 +0530 Subject: [PATCH] Security: Implement native Django 6.0 Content Security Policy - Integrated ContentSecurityPolicyMiddleware into settings. - Configured SECURE_CSP using Django 6.0 utilities for SELF and NONCE. - Added csp_nonce to base templates for inline script compatibility. - Enabled SECURE_CSP_REPORT_ONLY for initial policy auditing. - Fixes #468 Signed-off-by: vishalpatil-45 --- CSP_VERIFICATION_GUIDE.md | 119 ++++++++++++++++++ add_nonces_to_templates.py | 61 +++++++++ .../component_catalog/add_to_product.html | 2 +- .../component/delete_packages_js.html | 2 +- .../templates/admin/set_policy_base.html | 2 +- .../base_component_package_details.html | 2 +- .../base_component_package_list.html | 2 +- .../includes/add_package_modal.html | 2 +- .../component_catalog/includes/add_to.js.html | 2 +- .../includes/scan_matches_modal.html | 2 +- .../scan_summary_to_package_modal.html | 2 +- .../includes/scan_to_package_modal.html | 2 +- .../component_catalog/package_form.html | 2 +- .../component_catalog/scan_list.html | 2 +- .../tabs/field_key_files_table.html | 2 +- .../component_catalog/tabs/tab_scan.html | 4 +- dejacode/settings.py | 22 ++++ dje/templates/account/profile.html | 2 +- dje/templates/admin/base_site.html | 2 +- dje/templates/admin/docs/models.html | 2 +- .../admin/includes/activity_log_dialog.html | 2 +- .../admin/includes/search_help_dialog.html | 2 +- dje/templates/admin/mass_update.html | 2 +- dje/templates/admin/object_import.html | 2 +- dje/templates/bootstrap_base_js.html | 2 +- dje/templates/global_search.html | 2 +- dje/templates/hierarchy_base.js.html | 2 +- .../includes/dependencies-json-viewer.js.html | 2 +- dje/templates/includes/require_js.html | 2 +- dje/templates/object_details_base.html | 2 +- dje/templates/object_form.html | 2 +- dje/templates/object_list_base.html | 2 +- dje/templates/version_grouping_script.html | 2 +- dje/templates/widgets/autocomplete.html | 2 +- .../license_library/license_details.html | 2 +- .../license_library/license_list.html | 2 +- .../includes/owner_hierarchy.js.html | 2 +- .../product/change_form.html | 2 +- .../product_portfolio/import_from_scan.html | 2 +- .../import_manifests_form.html | 2 +- .../product_portfolio/load_sboms_form.html | 2 +- .../modals/pull_project_data_modal.html | 2 +- .../modals/scancode_project_status_modal.html | 2 +- .../product_portfolio/object_manage_grid.html | 2 +- .../product_portfolio/product_details.html | 12 +- .../product_portfolio/product_list.html | 2 +- .../product_tree_comparison.html | 2 +- .../product_portfolio/tabs/tab_codebase.html | 2 +- .../product_portfolio/tabs/tab_inventory.html | 2 +- .../reporting/columntemplate/change_form.html | 2 +- .../admin/reporting/inline_sortable_fix.html | 2 +- .../admin/reporting/query/change_form.html | 2 +- .../workflow/requesttemplate/change_form.html | 4 +- .../templates/workflow/request_details.html | 2 +- workflow/templates/workflow/request_form.html | 2 +- workflow/templates/workflow/request_list.html | 2 +- 56 files changed, 262 insertions(+), 60 deletions(-) create mode 100644 CSP_VERIFICATION_GUIDE.md create mode 100644 add_nonces_to_templates.py diff --git a/CSP_VERIFICATION_GUIDE.md b/CSP_VERIFICATION_GUIDE.md new file mode 100644 index 00000000..de4c914e --- /dev/null +++ b/CSP_VERIFICATION_GUIDE.md @@ -0,0 +1,119 @@ +# Django 6.0 CSP Security Implementation - Verification Guide + +## Summary of Changes + +### Step 4: Middleware Configuration ✅ +- **File**: `dejacode/settings.py` +- **Change**: Added `django.middleware.security.ContentSecurityPolicyMiddleware` after `SecurityMiddleware` +- **Location**: Line 176 +- **Status**: ✅ Complete + +### Step 5: CSP Dictionary Configuration ✅ +- **File**: `dejacode/settings.py` +- **Changes**: + - Imported CSP utility: `from django.utils.csp import CSP` (Line 18) + - Added CSP configuration starting at line 205 + - Set `SECURE_CSP_REPORT_ONLY = True` for initial audit phase + - Configured CSP directives: + - `default-src`: `[CSP.SELF]` - Only allow same-origin content by default + - `script-src`: Allows self, nonces, and CloudFront CDN + - `style-src`: Allows self, Google Fonts, and CloudFront CDN + - `img-src`: Allows self, data URIs, and HTTPS sources + - `connect-src`: Allows self (for API calls to PurlDB/VulnerableCode) +- **Status**: ✅ Complete + +### Step 6: Template Updates with Nonce Support ✅ +- **Method**: Automated Python script (`add_nonces_to_templates.py`) +- **Results**: + - Processed: 254 HTML template files + - Updated: 52 files with nonce attributes + - Pattern Applied: ` {% include 'includes/dependencies-json-viewer.js.html' %} {% if open_add_to_package_modal %} - - - - {% if include_purldb %} - - - \ No newline at end of file diff --git a/license_library/templates/license_library/license_details.html b/license_library/templates/license_library/license_details.html index e219dde7..ae87c6d9 100644 --- a/license_library/templates/license_library/license_details.html +++ b/license_library/templates/license_library/license_details.html @@ -18,7 +18,7 @@ {% if use_annotator %} - - - - - {% endif %} - - -