diff --git a/src/map_server/client.py b/src/map_server/client.py index a754e21..c9be0cf 100644 --- a/src/map_server/client.py +++ b/src/map_server/client.py @@ -373,7 +373,7 @@ def _api_request( json_body = {} if not isinstance(json_body, dict): raise ValueError("AMP json_body must be an object.") - if relative.lower() == "core/login": + if relative.strip("/").lower() == "core/login": raise ValueError("AMP login is managed automatically by MAP; call the desired native API method.") json_body = dict(json_body) json_body["SESSIONID"] = _amp_session(panel) diff --git a/src/map_server/config.py b/src/map_server/config.py index fc41028..87777bd 100644 --- a/src/map_server/config.py +++ b/src/map_server/config.py @@ -24,8 +24,6 @@ MAX_REQUEST_BODY_BYTES = int(os.getenv("MAP_MAX_REQUEST_BODY_BYTES", "2000000")) MAX_RESPONSE_BYTES = int(os.getenv("MAP_MAX_RESPONSE_BYTES", "5000000")) MAX_INLINE_FILE_BYTES = int(os.getenv("MAP_MAX_INLINE_FILE_BYTES", "6000000")) -MAX_REQUEST_BODY_BYTES = int(os.getenv("MAP_MAX_REQUEST_BODY_BYTES", "2000000")) -MAX_RESPONSE_BYTES = int(os.getenv("MAP_MAX_RESPONSE_BYTES", "5000000")) MAX_CONFIGURED_PANELS = 20 PANEL_IDS_RE = re.compile(r"^[A-Za-z0-9_-]{1,48}$") SERVER_ID_RE = re.compile(r"^[A-Za-z0-9_-]{1,80}$") diff --git a/src/map_server/server.py b/src/map_server/server.py index a975db3..a3234c7 100644 --- a/src/map_server/server.py +++ b/src/map_server/server.py @@ -389,6 +389,14 @@ def server_upload_file_base64( raise ValueError("server_id contains unsupported characters.") if not filename or filename in {".", ".."} or "/" in filename or "\\" in filename or len(filename) > 255: raise ValueError("filename must be a single basename, not a path.") + if not isinstance(content_base64, str): + raise ValueError("content_base64 must be a base64-encoded string.") + # Reject oversized encodings before decoding them into an equally large bytes object. + max_encoded_chars = 4 * ((MAX_INLINE_FILE_BYTES + 2) // 3) + if len(content_base64) > max_encoded_chars: + raise ValueError( + f"Encoded file exceeds the inline upload limit of {MAX_INLINE_FILE_BYTES} bytes." + ) try: contents = base64.b64decode(content_base64, validate=True) except Exception as exc: diff --git a/tests/test_safety.py b/tests/test_safety.py index 56ade7b..0f916a9 100644 --- a/tests/test_safety.py +++ b/tests/test_safety.py @@ -872,3 +872,37 @@ def test_auth_scheme_rejects_header_control_characters(monkeypatch): ) with pytest.raises(RuntimeError, match="auth_scheme"): server.list_panels() + + +def test_upload_base64_size_is_checked_before_decoding(monkeypatch): + configured_panel(monkeypatch) + monkeypatch.setattr(server, "MAX_INLINE_FILE_BYTES", 12) + decoded = {"called": False} + + def fake_decode(*args, **kwargs): + decoded["called"] = True + raise AssertionError("decoder must not run for oversized input") + + monkeypatch.setattr(server.base64, "b64decode", fake_decode) + oversized_encoded = "A" * 17 # 12-byte maximum permits at most 16 base64 characters. + with pytest.raises(ValueError, match="Encoded file exceeds"): + server.server_upload_file_base64("bp", "abc12345", "test.bin", oversized_encoded) + assert decoded["called"] is False + + +def test_amp_login_path_rejects_trailing_slash_variant(monkeypatch): + monkeypatch.setenv( + "MAP_PANELS_JSON", + json.dumps([{ + "id": "amp", + "provider": "amp", + "base_url": "https://amp.example.test", + "auth_username": "service-user", + "auth_password": "test-password", + }]), + ) + calls = [] + monkeypatch.setattr(client.requests, "post", lambda *args, **kwargs: calls.append(args)) + with pytest.raises(ValueError, match="login is managed automatically"): + client._api_request("amp", "native", "POST", "Core/Login/", json_body={}) + assert calls == []