Skip to content

Commit 7a4dfea

Browse files
committed
fix validation
1 parent 1a0308e commit 7a4dfea

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pymongo/auth_shared.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,20 @@ def __hash__(self) -> int:
8383
"""Mechanism properties for GSSAPI authentication."""
8484

8585

86-
_CANONICALIZE_HOST_NAME_VALUES = ["false", "true", "none", "forward", "forwardAndReverse"]
87-
8886
_AWSProperties = namedtuple("_AWSProperties", ["aws_session_token"])
8987
"""Mechanism properties for MONGODB-AWS authentication."""
9088

9189

90+
def _validate_canonicalize_host_name(value: str | bool) -> str | bool:
91+
valid_names = [False, True, "none", "forward", "forwardAndReverse"]
92+
if value in ["true", "false", True, False]:
93+
return value in ["true", True]
94+
95+
if value not in valid_names:
96+
raise ValueError(f"CANONICALIZE_HOST_NAME '{value}' not in valid options: {valid_names}")
97+
return value
98+
99+
92100
def _build_credentials_tuple(
93101
mech: str,
94102
source: Optional[str],
@@ -107,10 +115,7 @@ def _build_credentials_tuple(
107115
service_name = properties.get("SERVICE_NAME", "mongodb")
108116
service_host = properties.get("SERVICE_HOST", None)
109117
canonicalize = properties.get("CANONICALIZE_HOST_NAME", "false")
110-
if canonicalize not in _CANONICALIZE_HOST_NAME_VALUES:
111-
raise ConfigurationError(
112-
f"CANONICALIZE_HOST_NAME '{canonicalize}' not in valid options: {_CANONICALIZE_HOST_NAME_VALUES}"
113-
)
118+
canonicalize = _validate_canonicalize_host_name(canonicalize)
114119
service_realm = properties.get("SERVICE_REALM")
115120
props = GSSAPIProperties(
116121
service_name=service_name,

pymongo/common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ def validate_auth_mechanism_properties(option: str, value: Any) -> dict[str, Uni
477477
)
478478

479479
if key == "CANONICALIZE_HOST_NAME":
480-
props[key] = validate_boolean_or_string(key, val)
480+
from pymongo.auth_shared import _validate_canonicalize_host_name
481+
482+
props[key] = _validate_canonicalize_host_name(val)
481483
else:
482484
props[key] = val
483485

0 commit comments

Comments
 (0)