1 parent
94408fd2dd
commit
c7dee8d67d
4 files changed
+57
-20
No files matched your search
@@ -207,3 +207,6 @@ class ServerConfig(BaseModel):
|
||||
|
||||
cookies: CookiesConfig = CookiesConfig()
|
||||
"""Settings for further configuration of the cookies that OctoPrint sets (login, remember me, ...)."""
|
||||
|
||||
allowedLoginRedirectPaths: List[str] = []
|
||||
"""List of paths that are allowed to be used as redirect targets for the login page, in addition to the default ones (`/`, `/recovery/` and `/plugin/appkeys/auth/`)"""
|
||||
@@ -452,7 +452,7 @@ def validate_local_redirect(url, allowed_paths):
|
||||
Args:
|
||||
url (str): URL to validate
|
||||
allowed_paths (List[str]): List of allowed paths, only paths contained
|
||||
will be considered valid.
|
||||
or prefixed (if allowed path ends with "*") will be considered valid.
|
||||
|
||||
Returns:
|
||||
bool: Whether the `url` passed validation or not.
|
||||
@@ -460,4 +460,15 @@ def validate_local_redirect(url, allowed_paths):
|
||||
from urllib.parse import urlparse
|
||||
|
||||
parsed = urlparse(url)
|
||||
return parsed.scheme == "" and parsed.netloc == "" and parsed.path in allowed_paths
|
||||
return (
|
||||
parsed.scheme == ""
|
||||
and parsed.netloc == ""
|
||||
and any(
|
||||
map(
|
||||
lambda x: parsed.path.startswith(x[:-1])
|
||||
if x.endswith("*")
|
||||
else parsed.path == x,
|
||||
allowed_paths,
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -181,7 +181,22 @@ def login():
|
||||
|
||||
default_redirect_url = request.script_root + url_for("index")
|
||||
redirect_url = request.args.get("redirect", default_redirect_url)
|
||||
allowed_paths = [url_for("index"), url_for("recovery")]
|
||||
|
||||
configured_allowed_paths = settings().get(["server", "allowedLoginRedirectPaths"])
|
||||
if configured_allowed_paths is None or not isinstance(configured_allowed_paths, list):
|
||||
configured_allowed_paths = []
|
||||
configured_allowed_paths = list(
|
||||
filter(
|
||||
lambda x: isinstance(x, str),
|
||||
configured_allowed_paths,
|
||||
)
|
||||
)
|
||||
|
||||
allowed_paths = [
|
||||
url_for("index"),
|
||||
url_for("recovery"),
|
||||
url_for("plugin.appkeys.handle_auth_dialog", app_token="*"),
|
||||
] + configured_allowed_paths
|
||||
|
||||
if not validate_local_redirect(redirect_url, allowed_paths):
|
||||
_logger.warning(
|
||||
|
||||
@@ -3,31 +3,39 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms
|
||||
|
||||
import pytest
|
||||
|
||||
DEFAULT_ALLOWED_PATHS = ["/", "/recovery/", "/plugin/appkeys/auth/*"]
|
||||
PREFIXED_ALLOWED_PATHS = list(map(lambda x: "/octoprint" + x, DEFAULT_ALLOWED_PATHS))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"url,paths,expected",
|
||||
[
|
||||
# various default UI URLs
|
||||
("/", ["/", "/recovery/"], True),
|
||||
("/?", ["/", "/recovery/"], True),
|
||||
("/?l10n=de", ["/", "/recovery/"], True),
|
||||
("/?l10n=de&", ["/", "/recovery/"], True),
|
||||
("/octoprint/", ["/octoprint/", "/octoprint/recovery/"], True),
|
||||
("/", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/?", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/?l10n=de", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/?l10n=de&", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/octoprint/", PREFIXED_ALLOWED_PATHS, True),
|
||||
# various recovery URLs
|
||||
("/recovery/", ["/", "/recovery/"], True),
|
||||
("/recovery/?", ["/", "/recovery/"], True),
|
||||
("/recovery/?l10n=de", ["/", "/recovery/"], True),
|
||||
("/octoprint/recovery/?l10n=de", ["/octoprint/", "/octoprint/recovery/"], True),
|
||||
("/recovery/", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/recovery/?", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/recovery/?l10n=de", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/octoprint/recovery/?l10n=de", PREFIXED_ALLOWED_PATHS, True),
|
||||
# various appkeys URLs
|
||||
("/plugin/appkeys/auth/1234567890", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/plugin/appkeys/auth/1234567890?", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/plugin/appkeys/auth/1234567890?l10n=de", DEFAULT_ALLOWED_PATHS, True),
|
||||
("/octoprint/plugin/appkeys/auth/1234567890", PREFIXED_ALLOWED_PATHS, True),
|
||||
# various external URLs
|
||||
("http://example.com", ["/", "/recovery/"], False),
|
||||
("https://example.com", ["/", "/recovery/"], False),
|
||||
("//example.com", ["/", "/recovery/"], False),
|
||||
("/\\/\\example.com", ["/", "/recovery/"], False),
|
||||
(" /\\/\\example.com", ["/", "/recovery/"], False),
|
||||
("\\/\\/example.com", ["/", "/recovery/"], False),
|
||||
(" \\/\\/example.com", ["/", "/recovery/"], False),
|
||||
("http://example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
("https://example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
("//example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
("/\\/\\example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
(" /\\/\\example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
("\\/\\/example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
(" \\/\\/example.com", DEFAULT_ALLOWED_PATHS, False),
|
||||
# other stuff
|
||||
("javascript:alert(document.cookie)", ["/", "/recovery/"], False),
|
||||
("javascript:alert(document.cookie)", DEFAULT_ALLOWED_PATHS, False),
|
||||
],
|
||||
)
|
||||
def test_validate_local_redirect(url, paths, expected):
|
||||
|
||||
Reference in new issue
Block a user