Skip to content

Commit fd28f0f

Browse files
committed
feat: disallow relative paths with any depth other than zero
fix: update test fix test fix again
1 parent 03f4f29 commit fd28f0f

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

diracx-core/src/diracx/core/models/replica_catalog.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@
5757
def _validate_lfn(value: str) -> str:
5858
"""Validate and normalize Logical File Name.
5959
60-
Removes LFN: prefix if present and ensures it's a valid absolute path.
60+
Removes LFN: prefix if present and ensures it's a valid absolute path or a filename without slashes.
6161
"""
6262
value = value.removeprefix("LFN:")
63-
# if not value.startswith("/"):
64-
# raise ValueError(f"LFN must start with '/': {value}")
6563
if not value:
6664
raise ValueError("LFN cannot be empty")
65+
# Either it has a slash at the start or there can be no slashes at all
66+
if not value.startswith("/"):
67+
if "/" in value:
68+
raise ValueError(
69+
"LFN must be an absolute path starting with '/' "
70+
"or have no slashes at all (e.g. refers to a file in the current working directory)."
71+
)
6772
return value
6873

6974

@@ -192,8 +197,8 @@ def validate_replicas(cls, v: list) -> list:
192197
@field_validator("size_bytes")
193198
@classmethod
194199
def validate_size_bytes(cls, v: int | None) -> int | None:
195-
if v is not None and v < 0:
196-
raise ValueError(f"Size in bytes cannot be negative: {v}")
200+
if v is not None and v <= 0:
201+
raise ValueError(f"Size in bytes cannot be zero or negative: {v}")
197202
return v
198203

199204
root: dict[LFN, CatalogEntry]

diracx-core/tests/test_replica_catalog.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ def test_entry_empty_replicas_raises_error(self):
184184

185185
def test_entry_negative_size_raises_error(self):
186186
"""Test that negative size_bytes raises ValueError."""
187-
with pytest.raises(ValueError, match="Size in bytes cannot be negative"):
187+
with pytest.raises(
188+
ValueError, match="Size in bytes cannot be zero or negative"
189+
):
188190
ReplicaCatalog.CatalogEntry(
189191
replicas=[
190192
ReplicaCatalog.CatalogEntry.Replica(
@@ -194,20 +196,20 @@ def test_entry_negative_size_raises_error(self):
194196
size_bytes=-1,
195197
)
196198

197-
def test_entry_zero_size_is_valid(self):
198-
"""Test that zero size_bytes is valid."""
199-
entry = ReplicaCatalog.CatalogEntry(
200-
replicas=[
201-
ReplicaCatalog.CatalogEntry.Replica(
202-
url="https://example.com/file.dst", se="SE1"
203-
)
204-
],
205-
size_bytes=0,
206-
)
207-
assert entry.size_bytes == 0
208-
199+
def test_entry_zero_size_is_invalid(self):
200+
"""Test that zero size_bytes is invalid."""
201+
with pytest.raises(
202+
ValueError, match="Size in bytes cannot be zero or negative"
203+
):
204+
ReplicaCatalog.CatalogEntry(
205+
replicas=[
206+
ReplicaCatalog.CatalogEntry.Replica(
207+
url="https://example.com/file.dst", se="SE1"
208+
)
209+
],
210+
size_bytes=0,
211+
)
209212

210-
class TestReplicaCatalogReplica:
211213
"""Tests for ReplicaCatalog.CatalogEntry.Replica model."""
212214

213215
def test_valid_replica(self):

0 commit comments

Comments
 (0)