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
29 changes: 28 additions & 1 deletion src/notebooklm/_android/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
import tempfile
import time
import unicodedata
from collections.abc import AsyncIterator, Awaitable, Callable, Mapping
from contextlib import asynccontextmanager
from dataclasses import dataclass
Expand Down Expand Up @@ -360,6 +361,32 @@ def validate_upload_session_url(raw_url: str, project_id: str) -> str:
return raw_url


def _header_safe_filename(filename: str) -> str:
"""Return ``filename`` in a form that can travel in an HTTP header value.

httpx encodes header values as ASCII, and h11 rejects values with leading
or trailing whitespace or control characters, so a name with an accented
letter, a CJK character or a leading space never reaches the wire (#2547).
The source keeps its real name through the tentative registration; this
form is used only for ``X-Goog-Upload-File-Name``.
"""

def _fold(value: str) -> str:
decomposed = unicodedata.normalize("NFKD", value)
ascii_value = decomposed.encode("ascii", "ignore").decode("ascii")
return "".join(
character
for character in ascii_value
if character == "\t" or 0x20 <= ord(character) < 0x7F
)

folded = _fold(filename).strip()
suffix = _fold(Path(filename).suffix).strip()
if folded in {"", suffix}:
return f"upload{suffix}"
return folded


def _upload_failure(filename: str, state: _UploadState, detail: str) -> SourceAddError:
error = SourceAddError(
filename,
Expand Down Expand Up @@ -1217,7 +1244,7 @@ async def _start_worker(
"X-Goog-AuthUser": "0",
"X-Goog-Upload-Command": "start",
"X-Goog-Upload-Content-Length": str(file_size),
"X-Goog-Upload-File-Name": filename,
"X-Goog-Upload-File-Name": _header_safe_filename(filename),
"X-Goog-Upload-Header-Content-Length": str(file_size),
"X-Goog-Upload-Header-Content-Type": content_type,
"X-Goog-Upload-Protocol": "resumable",
Expand Down
30 changes: 15 additions & 15 deletions tests/fixtures/baselines/backend_static_coupling.json
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@
},
{
"kind": "from",
"lineno": 24,
"lineno": 25,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -1967,7 +1967,7 @@
},
{
"kind": "from",
"lineno": 25,
"lineno": 26,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -1976,7 +1976,7 @@
},
{
"kind": "from",
"lineno": 26,
"lineno": 27,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -1985,7 +1985,7 @@
},
{
"kind": "from",
"lineno": 27,
"lineno": 28,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -1994,7 +1994,7 @@
},
{
"kind": "from",
"lineno": 28,
"lineno": 29,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2003,7 +2003,7 @@
},
{
"kind": "from",
"lineno": 29,
"lineno": 30,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2012,7 +2012,7 @@
},
{
"kind": "from",
"lineno": 30,
"lineno": 31,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2021,7 +2021,7 @@
},
{
"kind": "from",
"lineno": 31,
"lineno": 32,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2030,7 +2030,7 @@
},
{
"kind": "from",
"lineno": 32,
"lineno": 33,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2039,7 +2039,7 @@
},
{
"kind": "from",
"lineno": 38,
"lineno": 39,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2048,7 +2048,7 @@
},
{
"kind": "from",
"lineno": 47,
"lineno": 48,
"scope": null,
"scope_kind": "module",
"source": "notebooklm._android.upload",
Expand All @@ -2057,7 +2057,7 @@
},
{
"kind": "from",
"lineno": 541,
"lineno": 568,
"scope": "AndroidUploadPipeline._client_factory",
"scope_kind": "function",
"source": "notebooklm._android.upload",
Expand All @@ -2066,7 +2066,7 @@
},
{
"kind": "from",
"lineno": 1321,
"lineno": 1348,
"scope": "AndroidUploadPipeline._finalize_worker",
"scope_kind": "function",
"source": "notebooklm._android.upload",
Expand Down Expand Up @@ -11949,7 +11949,7 @@
],
"subsystems": {
"android": {
"lines": 16846,
"lines": 16873,
"modules": 56
},
"app": {
Expand Down Expand Up @@ -11994,7 +11994,7 @@
}
},
"summary": {
"authored_lines": 157157,
"authored_lines": 157184,
"authored_modules": 499,
"class_local_edges": 0,
"cross_subsystem_edges": 1316,
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/android/test_source_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,67 @@ async def test_filename_title_is_not_custom_and_performs_zero_project_reads(tmp_
assert [call[0] for call in session.calls] == [ADD_TENTATIVE_SOURCES_METHOD]


@pytest.mark.asyncio
@pytest.mark.parametrize(
("filename", "header_name"),
[
("Annual Report.pdf", "Annual Report.pdf"),
("Caf\u00e9.pdf", "Cafe.pdf"),
("The Team\u2019s Plan.pdf", "The Teams Plan.pdf"),
("\u4f1a\u8bae\u8bb0\u5f55.pdf", "upload.pdf"),
(" Annual Report.pdf", "Annual Report.pdf"),
],
)
@pytest.mark.parametrize("curl", [False, True], ids=["httpx", "curl-cffi"])
async def test_file_name_header_is_header_safe_and_registration_keeps_real_name(
tmp_path: Path,
filename: str,
header_name: str,
curl: bool,
) -> None:
harness = HTTPHarness()
session, _, _, api = await _graph(harness, curl=curl)
_, content = _write_pdf(tmp_path)
path = tmp_path / filename
path.write_bytes(content)

result = await api.add_file(NOTEBOOK_ID, path)

assert result.title == filename
assert session.calls[0][1].tentative_sources_metadata[0].name == filename
start = next(call for call in harness.calls if call.method == "POST")
value = start.headers["X-Goog-Upload-File-Name"]
assert value == header_name
assert value == value.strip()
assert all(0x20 <= ord(character) < 0x7F for character in value)
assert httpx.Headers({"X-Goog-Upload-File-Name": value}).raw


@pytest.mark.parametrize(
("filename", "header_name"),
[
("document.pdf", "document.pdf"),
("Annual * Report.pdf", "Annual * Report.pdf"),
("The Team\u2019s Plan * Draft.pdf", "The Teams Plan * Draft.pdf"),
("a .pdf", "a .pdf"),
("tab\there.pdf", "tab\there.pdf"),
(".pdf", ".pdf"),
("notes", "notes"),
("Cafe\u0301.pdf", "Cafe.pdf"),
("Annual Report.pdf ", "Annual Report.pdf"),
("\ttab.pdf", "tab.pdf"),
("bell\x07.pdf", "bell.pdf"),
("line\r\nX-Evil: 1.pdf", "lineX-Evil: 1.pdf"),
("\u4f1a\u8bae", "upload"),
],
)
def test_header_safe_filename_keeps_valid_names_and_folds_the_rest(
filename: str,
header_name: str,
) -> None:
assert upload_module._header_safe_filename(filename) == header_name


@pytest.mark.parametrize(
"url",
[
Expand Down
Loading