Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/map_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/map_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}$")
Expand Down
8 changes: 8 additions & 0 deletions src/map_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
Loading