Buckets:

download
raw
4.35 kB
"""Tests for FQN component validation (security: prevents FQN injection)."""
import pytest
from tessera.services.fqn import (
FQNComponentError,
sanitize_proto_package,
validate_fqn_component,
)
class TestValidateFqnComponent:
"""Tests for validate_fqn_component."""
def test_valid_simple(self) -> None:
assert validate_fqn_component("users", "field") == "users"
def test_valid_with_underscores(self) -> None:
assert validate_fqn_component("my_service", "field") == "my_service"
def test_valid_with_hyphens(self) -> None:
assert validate_fqn_component("my-service", "field") == "my-service"
def test_valid_alphanumeric(self) -> None:
assert validate_fqn_component("Service123", "field") == "Service123"
def test_valid_starts_with_underscore(self) -> None:
assert validate_fqn_component("_internal", "field") == "_internal"
def test_rejects_empty(self) -> None:
with pytest.raises(FQNComponentError, match="must not be empty"):
validate_fqn_component("", "field")
def test_rejects_dots(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
validate_fqn_component("evil.inject", "field")
def test_rejects_slashes(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
validate_fqn_component("path/inject", "field")
def test_rejects_backslashes(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
validate_fqn_component("path\\inject", "field")
def test_rejects_spaces(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
validate_fqn_component("has space", "field")
def test_rejects_at_sign(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
validate_fqn_component("user@evil", "field")
def test_rejects_starts_with_digit(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
validate_fqn_component("123abc", "field")
def test_error_message_includes_field_name(self) -> None:
with pytest.raises(FQNComponentError, match="service name"):
validate_fqn_component("bad.value", "service name")
def test_error_message_lists_unsafe_chars(self) -> None:
with pytest.raises(FQNComponentError, match=r"\['\.'"):
validate_fqn_component("has.dot", "field")
class TestSanitizeProtoPackage:
"""Tests for sanitize_proto_package."""
def test_empty_package(self) -> None:
assert sanitize_proto_package("") == ""
def test_simple_package(self) -> None:
assert sanitize_proto_package("users") == "users"
def test_dotted_package_becomes_underscored(self) -> None:
assert sanitize_proto_package("com.example.api") == "com_example_api"
def test_single_segment(self) -> None:
assert sanitize_proto_package("mypackage") == "mypackage"
def test_two_segments(self) -> None:
assert sanitize_proto_package("com.users") == "com_users"
def test_deeply_nested_package(self) -> None:
assert sanitize_proto_package("com.example.api.v1") == "com_example_api_v1"
def test_rejects_unsafe_segment(self) -> None:
with pytest.raises(FQNComponentError, match="unsafe characters"):
sanitize_proto_package("com.evil/path.api")
def test_rejects_empty_segment_from_consecutive_dots(self) -> None:
with pytest.raises(FQNComponentError, match="must not be empty"):
sanitize_proto_package("com..api")
def test_prevents_impersonation(self) -> None:
"""Different dotted packages produce distinct sanitized outputs."""
# These must not collide with each other
assert sanitize_proto_package("team.service") == "team_service"
assert sanitize_proto_package("team_service") == "team_service"
# NOTE: team.service and team_service DO produce the same output.
# This is acceptable — the ambiguity is between two packages that
# map to the same logical name, not between packages of different
# segment depth being interpreted as extra FQN segments.
# The critical fix is that dots no longer create extra FQN segments.

Xet Storage Details

Size:
4.35 kB
·
Xet hash:
0d29f83e3a69bac2e3ec88179fa00304489834aea54e5b30c7780879571eb25b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.