Skip to content

armgit / BYOC

Public Imported from GitHub
0

One storage API for Google Drive, Nextcloud, and S3-compatible clouds, in TypeScript and Python.

feat(security): reject XML entity expansion, and scan for it in CI

Ajay Raminenic142645b972a
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 76cc89a..ca5686c 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -71,6 +71,14 @@ jobs:
- name: Run unit and conformance suites
run: pytest -q
+ - name: Static security scan
+ if: matrix.python-version == '3.13'
+ run: bandit -q -r src/byoc
+
+ - name: Dependency vulnerability audit
+ if: matrix.python-version == '3.13'
+ run: pip-audit
+
integration:
name: Live integration (MinIO + WebDAV)
runs-on: ubuntu-latest
diff --git a/packages/s3-compatible/src/auth/signer.ts b/packages/s3-compatible/src/auth/signer.ts
index 500f157..3210352 100644
--- a/packages/s3-compatible/src/auth/signer.ts
+++ b/packages/s3-compatible/src/auth/signer.ts
@@ -12,6 +12,8 @@ export interface SignRequestParams {
url: string;
headers?: Record<string, string>;
body?: Uint8Array | string;
+ /** Precomputed payload hash, including the SigV4 `UNSIGNED-PAYLOAD` sentinel. */
+ payloadHash?: string;
datetime?: Date;
}
@@ -80,9 +82,11 @@ export function signS3Request(
const canonicalUri = parsedUrl.pathname || "/";
const canonicalQuery = buildCanonicalQueryString(parsedUrl);
- const bodyHash = params.body
- ? sha256Hex(params.body)
- : "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+ const bodyHash = params.payloadHash ?? (
+ params.body !== undefined
+ ? sha256Hex(params.body)
+ : "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
+ );
// Normalize all incoming headers to lowercase keys and trimmed values
const normalizedHeaders: Record<string, string> = {
diff --git a/packages/s3-compatible/tests/auth/signer.test.ts b/packages/s3-compatible/tests/auth/signer.test.ts
index 866cd65..cd4b2ea 100644
--- a/packages/s3-compatible/tests/auth/signer.test.ts
+++ b/packages/s3-compatible/tests/auth/signer.test.ts
@@ -60,6 +60,18 @@ describe("AWS SigV4 Signer & Canonicalization", () => {
expect(signedWithUpper.Authorization).toContain("content-type;host;x-amz-content-sha256;x-amz-date;x-amz-meta-author");
});
+ it("signs one-shot streams with the SigV4 UNSIGNED-PAYLOAD sentinel", () => {
+ const signed = signS3Request(options, {
+ method: "PUT",
+ url: "https://examplebucket.s3.amazonaws.com/large.bin",
+ payloadHash: "UNSIGNED-PAYLOAD",
+ datetime: fixedDate
+ });
+
+ expect(signed["x-amz-content-sha256"]).toBe("UNSIGNED-PAYLOAD");
+ expect(signed.Authorization).toContain("x-amz-content-sha256");
+ });
+
it("correctly encodes and sorts multi-parameter query strings with %20 (Bug #4 fix)", () => {
const url = new URL("https://examplebucket.s3.amazonaws.com/?prefix=my+folder&list-type=2&delimiter=%2F");
const canonicalQuery = buildCanonicalQueryString(url);
diff --git a/python/pyproject.toml b/python/pyproject.toml
index dde8a9c..0068438 100644
--- a/python/pyproject.toml
+++ b/python/pyproject.toml
@@ -24,6 +24,7 @@ classifiers = [
dependencies = [
"httpx>=0.27",
"cryptography>=42.0",
+ "defusedxml>=0.7.1",
]
[project.optional-dependencies]
@@ -35,6 +36,9 @@ dev = [
"cheroot>=10.0",
"mypy>=1.9",
"ruff>=0.4",
+ "bandit>=1.8",
+ "pip-audit>=2.7",
+ "types-defusedxml>=0.7",
]
[project.urls]
diff --git a/python/tests/test_xml_security.py b/python/tests/test_xml_security.py
new file mode 100644
index 0000000..2b80570
--- /dev/null
+++ b/python/tests/test_xml_security.py
@@ -0,0 +1,50 @@
+"""Provider-controlled XML must not expand DTD entities."""
+
+from __future__ import annotations
+
+import httpx
+from defusedxml.common import EntitiesForbidden
+
+from byoc.errors import ProviderUnavailableError
+from byoc.providers.s3 import S3CompatibleProvider
+from byoc.providers.webdav import WebDAVProvider
+
+_ENTITY_XML = """<?xml version="1.0"?>
+<!DOCTYPE response [<!ENTITY injected "attacker-controlled">]>
+<response>&injected;</response>
+"""
+
+
+async def test_s3_list_rejects_xml_entities() -> None:
+ transport = httpx.MockTransport(lambda request: httpx.Response(200, text=_ENTITY_XML))
+ async with httpx.AsyncClient(transport=transport) as client:
+ provider = S3CompatibleProvider(
+ endpoint="https://s3.example.test",
+ bucket="bucket",
+ region="us-east-1",
+ access_key_id="key",
+ secret_access_key="secret",
+ client=client,
+ )
+ try:
+ await provider.list()
+ except ProviderUnavailableError as exc:
+ assert isinstance(exc.raw_error, EntitiesForbidden)
+ else:
+ raise AssertionError("unsafe S3 XML was accepted")
+
+
+async def test_webdav_list_rejects_xml_entities() -> None:
+ transport = httpx.MockTransport(lambda request: httpx.Response(207, text=_ENTITY_XML))
+ async with httpx.AsyncClient(transport=transport) as client:
+ provider = WebDAVProvider(
+ endpoint="https://dav.example.test",
+ root_folder="",
+ client=client,
+ )
+ try:
+ await provider.list()
+ except ProviderUnavailableError as exc:
+ assert isinstance(exc.raw_error, EntitiesForbidden)
+ else:
+ raise AssertionError("unsafe WebDAV XML was accepted")