From 3b8200967675f1eb668b99096e31f285d2d5ea98 Mon Sep 17 00:00:00 2001 From: Partha Shankar <152790727+Partha-Shankar@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:58:29 +0530 Subject: [PATCH] fix: ignore a UTF-8 BOM in the Vault token file Read the refresh-token file with encoding="utf-8-sig" like the other text reads in the package. A BOM (e.g. from PowerShell 5.1 Set-Content -Encoding UTF8) was sent as part of the token. Adds a regression test. --- databusclient/api/download.py | 2 +- tests/test_download_auth.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/databusclient/api/download.py b/databusclient/api/download.py index f2aead78..7eeda001 100644 --- a/databusclient/api/download.py +++ b/databusclient/api/download.py @@ -1022,7 +1022,7 @@ def __get_vault_access__( if not refresh_token: if not os.path.exists(token_file): raise FileNotFoundError(f"Vault token file not found: {token_file}") - with open(token_file, "r") as f: + with open(token_file, "r", encoding="utf-8-sig") as f: refresh_token = f.read().strip() if len(refresh_token) < 80: print(f"Warning: token from {token_file} is short (<80 chars)") diff --git a/tests/test_download_auth.py b/tests/test_download_auth.py index 2505d827..b11cb1c7 100644 --- a/tests/test_download_auth.py +++ b/tests/test_download_auth.py @@ -167,3 +167,28 @@ def test_403_reports_insufficient_permissions(): dl._download_file(url, localDir=".", vault_token_file="/some/token/file") assert "permission" in str(exc.value) or "forbidden" in str(exc.value) + + +@pytest.mark.parametrize("bom", [b"\xef\xbb\xbf", b""], ids=["utf8-bom", "no-bom"]) +def test_vault_token_file_is_read_without_bom(tmp_path, monkeypatch, bom): + # Windows PowerShell 5.1 `Set-Content -Encoding UTF8` writes a UTF-8 BOM + token = "t" * 90 + token_file = tmp_path / "vault-token.dat" + token_file.write_bytes(bom + token.encode("utf-8") + b"\r\n") + monkeypatch.delenv("REFRESH_TOKEN", raising=False) + + post_resp_1 = Mock() + post_resp_1.json.return_value = {"access_token": "ACCESS"} + post_resp_2 = Mock() + post_resp_2.json.return_value = {"access_token": "VAULT"} + + with patch("requests.post", side_effect=[post_resp_1, post_resp_2]) as post: + vault_token = dl.__get_vault_access__( + "https://data.dbpedia.io/protected/file.ttl", + str(token_file), + "https://auth.example.org/token", + "vault-token-exchange", + ) + + assert vault_token == "VAULT" + assert post.call_args_list[0].kwargs["data"]["refresh_token"] == token