-
Notifications
You must be signed in to change notification settings - Fork 7
Improve validation of client ID and env #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,22 +44,19 @@ def __init__(self, client_id: str, env: str, request_logging_config: Optional[Re | |||||||||||||
| if hasattr(self, "client_id"): | ||||||||||||||
| raise RuntimeError("Apitally client is already initialized") # pragma: no cover | ||||||||||||||
|
|
||||||||||||||
| client_id_valid = True | ||||||||||||||
| env_valid = True | ||||||||||||||
| self.client_id = str(client_id) | ||||||||||||||
| self.env = str(env) | ||||||||||||||
| self.enabled = True | ||||||||||||||
|
|
||||||||||||||
| if not self.validate_client_id(self.client_id): | ||||||||||||||
| self.enabled = False | ||||||||||||||
| logger.error(f"invalid client_id '{self.client_id}' (expecting string in hexadecimal UUID format)") | ||||||||||||||
| if not self.validate_env(self.env): | ||||||||||||||
| self.enabled = False | ||||||||||||||
| logger.error( | ||||||||||||||
| f"invalid env '{self.env}' (expecting string with 1-32 alphanumeric characters and hyphens only)" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| try: | ||||||||||||||
| UUID(client_id) | ||||||||||||||
| except ValueError: | ||||||||||||||
| logger.error(f"invalid client_id '{client_id}' (expecting hexadecimal UUID format)") | ||||||||||||||
| client_id_valid = False | ||||||||||||||
|
|
||||||||||||||
| if re.match(r"^[\w-]{1,32}$", env) is None: | ||||||||||||||
| logger.error(f"invalid env '{env}' (expecting 1-32 alphanumeric lowercase characters and hyphens only)") | ||||||||||||||
| env_valid = False | ||||||||||||||
|
|
||||||||||||||
| self.client_id = client_id | ||||||||||||||
| self.env = env | ||||||||||||||
| self.enabled = client_id_valid and env_valid | ||||||||||||||
| self.instance_uuid = str(uuid4()) | ||||||||||||||
| self.request_counter = RequestCounter() | ||||||||||||||
| self.validation_error_counter = ValidationErrorCounter() | ||||||||||||||
|
|
@@ -104,3 +101,15 @@ def get_sync_data(self) -> Dict[str, Any]: | |||||||||||||
| "consumers": self.consumer_registry.get_and_reset_updated_consumers(), | ||||||||||||||
| } | ||||||||||||||
| return self.add_uuids_to_data(data) | ||||||||||||||
|
|
||||||||||||||
| @staticmethod | ||||||||||||||
| def validate_client_id(client_id: str) -> bool: | ||||||||||||||
| try: | ||||||||||||||
| UUID(client_id) | ||||||||||||||
| return True | ||||||||||||||
| except ValueError: | ||||||||||||||
| return False | ||||||||||||||
|
|
||||||||||||||
| @staticmethod | ||||||||||||||
| def validate_env(env: str) -> bool: | ||||||||||||||
| return re.match(r"^[\w-]{1,32}$", env) is not None | ||||||||||||||
|
Comment on lines
+113
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regex pattern includes underscores but error message doesn't mention them. The regex pattern Choose one of the following fixes: Option 1: Exclude underscores from the pattern (if not intended): - def validate_env(env: str) -> bool:
- return re.match(r"^[\w-]{1,32}$", env) is not None
+ def validate_env(env: str) -> bool:
+ return re.match(r"^[a-zA-Z0-9-]{1,32}$", env) is not NoneOption 2: Update the error message to reflect that underscores are allowed: logger.error(
- f"invalid env '{self.env}' (expecting string with 1-32 alphanumeric characters and hyphens only)"
+ f"invalid env '{self.env}' (expecting string with 1-32 alphanumeric characters, hyphens, and underscores)"
)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Good approach to prevent TypeError, but consider warning about type mismatches.
The string conversion prevents the TypeError that was causing 500 errors, which directly addresses issue #173. However, silently converting non-string types (e.g.,
None→"None",1234→"1234") may hide configuration errors that developers should be aware of.Consider adding type checks before conversion to log warnings:
Additionally, verify that the
enabledflag is checked in the middleware to ensure that when validation fails, the client is actually disabled and doesn't process requests:🏁 Script executed:
Length of output: 9171
Length of output: 765
Fix regex pattern or error message inconsistency in validate_env method.
The
enabledflag is properly checked throughout all middleware implementations before processing requests, which addresses the concern from the original review. However, there's an inconsistency in thevalidate_envmethod:The regex pattern
r"^[\w-]{1,32}$"includes underscores (since\wmatches[a-zA-Z0-9_]), but the error message states: "expecting string with 1-32 alphanumeric characters and hyphens only" — which doesn't mention underscores.Either update the error message to include underscores, or change the regex to
r"^[a-zA-Z0-9-]{1,32}$"to match the documented behavior. Lines 114-115 inapitally/client/client_base.pyand lines 57-58 must be kept in sync.🤖 Prompt for AI Agents