|
| 1 | +# Drakkar-Software OctoBot-Tentacles |
| 2 | +# Copyright (c) Drakkar-Software, All rights reserved. |
| 3 | +# |
| 4 | +# This library is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU Lesser General Public |
| 6 | +# License as published by the Free Software Foundation; either |
| 7 | +# version 3.0 of the License, or (at your option) any later version. |
| 8 | +# |
| 9 | +# This library is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +# Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public |
| 15 | +# License along with this library. |
| 16 | + |
| 17 | +import flask |
| 18 | + |
| 19 | +import tentacles.Services.Interfaces.web_interface.security as security |
| 20 | + |
| 21 | +_FLASK_APP = flask.Flask(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def _https_request_at_safe_example(): |
| 25 | + return _FLASK_APP.test_request_context( |
| 26 | + "/", |
| 27 | + environ_base={ |
| 28 | + "HTTP_HOST": "safe.example", |
| 29 | + "wsgi.url_scheme": "https", |
| 30 | + }, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class TestIsSafeRedirectUrl: |
| 35 | + def test_none_is_safe(self): |
| 36 | + assert security.is_safe_redirect_url(None) is True |
| 37 | + |
| 38 | + def test_empty_and_whitespace_only_are_safe(self): |
| 39 | + assert security.is_safe_redirect_url("") is True |
| 40 | + assert security.is_safe_redirect_url(" ") is True |
| 41 | + |
| 42 | + def test_non_string_is_unsafe(self): |
| 43 | + assert security.is_safe_redirect_url(42) is False |
| 44 | + assert security.is_safe_redirect_url(["/"]) is False |
| 45 | + |
| 46 | + def test_single_slash_path_is_unsafe(self): |
| 47 | + with _https_request_at_safe_example(): |
| 48 | + assert security.is_safe_redirect_url("/") is False |
| 49 | + |
| 50 | + def test_double_leading_slash_is_unsafe(self): |
| 51 | + with _https_request_at_safe_example(): |
| 52 | + assert security.is_safe_redirect_url("//evil.test/") is False |
| 53 | + |
| 54 | + def test_absolute_path_same_host_is_safe(self): |
| 55 | + with _https_request_at_safe_example(): |
| 56 | + assert security.is_safe_redirect_url("/dashboard") is True |
| 57 | + assert security.is_safe_redirect_url("/a/b") is True |
| 58 | + |
| 59 | + def test_encoded_tab_octets_are_unsafe(self): |
| 60 | + with _https_request_at_safe_example(): |
| 61 | + assert security.is_safe_redirect_url("/%09/%09/%09/evil.test") is False |
| 62 | + |
| 63 | + def test_full_url_string_is_unsafe(self): |
| 64 | + with _https_request_at_safe_example(): |
| 65 | + assert security.is_safe_redirect_url("https://evil.test/") is False |
| 66 | + |
| 67 | + def test_leading_whitespace_fails_regex_on_raw_target(self): |
| 68 | + """Regex is applied to ``target``, not stripped form (caller may pass unstripped values).""" |
| 69 | + with _https_request_at_safe_example(): |
| 70 | + assert security.is_safe_redirect_url(" /ok") is False |
| 71 | + |
| 72 | + |
| 73 | +class TestRedirectTargetOr: |
| 74 | + def test_returns_default_for_none_non_string_or_blank(self): |
| 75 | + default_path = "/default" |
| 76 | + assert security.redirect_target_or(None, default_path) == default_path |
| 77 | + assert security.redirect_target_or(123, default_path) == default_path |
| 78 | + assert security.redirect_target_or("", default_path) == default_path |
| 79 | + assert security.redirect_target_or(" \t ", default_path) == default_path |
| 80 | + |
| 81 | + def test_returns_safe_stripped_target(self): |
| 82 | + with _https_request_at_safe_example(): |
| 83 | + assert security.redirect_target_or("/panel", "/home") == "/panel" |
| 84 | + assert security.redirect_target_or(" /panel ", "/home") == "/panel" |
| 85 | + |
| 86 | + def test_returns_default_when_redirect_would_be_unsafe(self): |
| 87 | + with _https_request_at_safe_example(): |
| 88 | + fallback = "/home" |
| 89 | + assert security.redirect_target_or("//evil", fallback) == fallback |
| 90 | + assert security.redirect_target_or("/", fallback) == fallback |
| 91 | + |
| 92 | + |
| 93 | +class TestIsSafeUrl: |
| 94 | + def test_delegates_to_same_rules_as_is_safe_redirect_url(self): |
| 95 | + with _https_request_at_safe_example(): |
| 96 | + for candidate in ( |
| 97 | + None, |
| 98 | + "/safe", |
| 99 | + "/", |
| 100 | + "//bad", |
| 101 | + "https://other/", |
| 102 | + ): |
| 103 | + assert security.is_safe_url(candidate) == security.is_safe_redirect_url(candidate) |
0 commit comments