From 4a52a71956a8d46fcb7294ac71734504bb09bcc2 Mon Sep 17 00:00:00 2001 From: S. Solomon Darnell Date: Fri, 28 Mar 2025 21:52:21 -0500 Subject: two version of R2R are here --- .../sendgrid/helpers/mail/validators.py | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 .venv/lib/python3.12/site-packages/sendgrid/helpers/mail/validators.py (limited to '.venv/lib/python3.12/site-packages/sendgrid/helpers/mail/validators.py') diff --git a/.venv/lib/python3.12/site-packages/sendgrid/helpers/mail/validators.py b/.venv/lib/python3.12/site-packages/sendgrid/helpers/mail/validators.py new file mode 100644 index 00000000..00e3276e --- /dev/null +++ b/.venv/lib/python3.12/site-packages/sendgrid/helpers/mail/validators.py @@ -0,0 +1,69 @@ +from .exceptions import ApiKeyIncludedException + + +class ValidateApiKey(object): + """Validates content to ensure SendGrid API key is not present""" + + regexes = None + + def __init__(self, regex_strings=None, use_default=True): + """Create an API key validator + + :param regex_strings: list of regex strings + :type regex_strings: list(str) + :param use_default: Whether or not to include default regex + :type use_default: bool + """ + + import re + self.regexes = set() + + # Compile the regex strings into patterns, add them to our set + if regex_strings is not None: + for regex_string in regex_strings: + self.regexes.add(re.compile(regex_string)) + + if use_default: + default_regex_string = r'SG\.[0-9a-zA-Z]+\.[0-9a-zA-Z]+' + self.regexes.add(re.compile(default_regex_string)) + + def validate_message_dict(self, request_body): + """With the JSON dict that will be sent to SendGrid's API, + check the content for SendGrid API keys - throw exception if found. + + :param request_body: The JSON dict that will be sent to SendGrid's + API. + :type request_body: JSON serializable structure + :raise ApiKeyIncludedException: If any content in request_body + matches regex + """ + + # Handle string in edge-case + if isinstance(request_body, str): + self.validate_message_text(request_body) + + # Default param + elif isinstance(request_body, dict): + + contents = request_body.get("content", list()) + + for content in contents: + if content is not None: + if (content.get("type") == "text/html" or + isinstance(content.get("value"), str)): + message_text = content.get("value", "") + self.validate_message_text(message_text) + + def validate_message_text(self, message_string): + """With a message string, check to see if it contains a SendGrid API Key + If a key is found, throw an exception + + :param message_string: message that will be sent + :type message_string: string + :raises ApiKeyIncludedException: If message_string matches a regex + string + """ + if isinstance(message_string, str): + for regex in self.regexes: + if regex.match(message_string) is not None: + raise ApiKeyIncludedException() -- cgit v1.2.3