github-advanced-security[bot] commented on code in PR #41468: URL: https://github.com/apache/superset/pull/41468#discussion_r3484690980
########## tests/unit_tests/security/csp_test.py: ########## @@ -0,0 +1,160 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from unittest.mock import patch + +import pytest +from flask import Response + +from superset.security import csp as csp_module +from superset.security.csp import ( + apply_runtime_csp_allowlist, + is_valid_csp_directive, + is_valid_csp_origin, + merge_allowlist_into_csp, +) + + [email protected]( + "origin", + [ + "https://example.com", + "http://example.com", + "https://example.com:8443", + "http://localhost:9000", + ], +) +def test_is_valid_csp_origin_accepts_bare_origins(origin: str) -> None: + assert is_valid_csp_origin(origin) is True + + [email protected]( + "origin", + [ + "", + "example.com", # missing scheme + "ftp://example.com", # disallowed scheme + "https://*.example.com", # wildcard + "https://example.com/path", # path + "https://example.com?q=1", # query + "https://example.com#frag", # fragment + "https://user:[email protected]", # credentials + "javascript:alert(1)", # non-network scheme + "https://example.com ", # whitespace + ], +) +def test_is_valid_csp_origin_rejects_unsafe_values(origin: str) -> None: + assert is_valid_csp_origin(origin) is False + + +def test_is_valid_csp_directive() -> None: + assert is_valid_csp_directive("frame-src") is True + assert is_valid_csp_directive("img-src") is True + # script-src is intentionally not allowlistable + assert is_valid_csp_directive("script-src") is False + assert is_valid_csp_directive("nonsense") is False + + +def test_merge_seeds_a_missing_directive_with_self() -> None: + base = "default-src 'self'; img-src 'self' data:" + merged = merge_allowlist_into_csp(base, {"frame-src": ["https://maps.example"]}) + assert "frame-src 'self' https://maps.example" in merged + # existing directives are preserved + assert "default-src 'self'" in merged + assert "img-src 'self' data:" in merged + + +def test_merge_appends_to_existing_directive_without_duplicates() -> None: + base = "default-src 'self'; img-src 'self' data:" + merged = merge_allowlist_into_csp( + base, {"img-src": ["https://cdn.example", "data:"]} + ) + assert "https://cdn.example" in merged Review Comment: ## CodeQL / Incomplete URL substring sanitization The string [https://cdn.example](1) may be at an arbitrary position in the sanitized URL. [Show more details](https://github.com/apache/superset/security/code-scanning/2554) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
