Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

integrations: Support colon and comma characters in jotform payloads.

Varun-Kolanu93913dd6e797
diff --git a/zerver/webhooks/jotform/fixtures/response_with_colon_comma_characters.multipart b/zerver/webhooks/jotform/fixtures/response_with_colon_comma_characters.multipart
new file mode 100644
index 00000000000..0380fa5d408
--- /dev/null
+++ b/zerver/webhooks/jotform/fixtures/response_with_colon_comma_characters.multipart
@@ -0,0 +1,93 @@
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="action"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="webhookURL"
+
+https://a395-103-151-209-147.ngrok-free.app/api/v1/external/jotform?api_key=6SuIPsdyiL6Bgljov4wp67wD4rqHRWOg&stream=15&topic=jotform
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="username"
+
+kolanuvarun739
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="formID"
+
+243490908500051
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="type"
+
+WEB
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="customParams"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="product"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="formTitle"
+
+Sample testing
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="customTitle"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="submissionID"
+
+6197916587414311452
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="event"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="documentID"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="teamID"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="subject"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="isSilent"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="customBody"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="rawRequest"
+
+{"slug":"submit\/243490908500051","jsExecutionTracker":"build-date-1743981991368=>init-started:1743982445902=>validator-called:1743982445907=>validator-mounted-false:1743982445907=>init-complete:1743982445910=>onsubmit-fired:1743982457902=>observerSubmitHandler_received-submit-event:1743982457903=>submit-validation-passed:1743982457907=>observerSubmitHandler_validation-passed-submitting-form:1743982457914","submitSource":"form","buildDate":"1743981991368","uploadServerUrl":"https:\/\/upload.jotform.com\/upload","eventObserver":"1","q10_key1With":"Value1 with colon: and comma, end","q11_sameKey":"Value 1","q12_sameKey2":"Value 2","q13_sameKeyValue":"Same Key-Value","q14_value2":"Value 3","q23_multipleChoice":["Option; 1","Option2"],"event_id":"1743982445903_243490908500051_4IYtoJz","timeToSubmit":"11","temp_upload":{"q22_fileUpload":["error; frontend, UI.png#jotformfs-e4f4ece4d0a90#01960d74-6a25-7398-87c7-df56ce6711f1","Screenshot_20250331_201054.png#jotformfs-e4f4ece4d0a90#01960d74-6bbc-7e1b-8187-9028f55b6ef6"]},"file_server":"jotformfs-e4f4ece4d0a90#01960d74-6bbc-7e1b-8187-9028f55b6ef6","validatedNewRequiredFieldIDs":"{\"new\":1}","path":"\/submit\/243490908500051","fileUpload":["https:\/\/www.jotform.com\/uploads\/kolanuvarun739\/243490908500051\/6197916587414311452\/error%3B%20frontend%2C%20UI.png","https:\/\/www.jotform.com\/uploads\/kolanuvarun739\/243490908500051\/6197916587414311452\/Screenshot_20250331_201054.png"]}
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="fromTable"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="appID"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="pretty"
+
+Key1 with colon: and comma, end:Value1 with colon: and comma, end, Same Key:Value 1, Same Key:Value 2, Same Key-Value:Same Key-Value, Value 2:Value 3, Multiple Choice Question, options::Option; 1 Option2, File Upload with colon : and comma , end:error; frontend, UI.png Screenshot_20250331_201054.png
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="unread"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="parent"
+
+
+--------------------------BIoxRptelgempTlOi8T0XA
+Content-Disposition: form-data; name="ip"
+
+103.151.209.147
+--------------------------BIoxRptelgempTlOi8T0XA--
diff --git a/zerver/webhooks/jotform/tests.py b/zerver/webhooks/jotform/tests.py
index cd78cf16480..3a59ddf0df2 100644
--- a/zerver/webhooks/jotform/tests.py
+++ b/zerver/webhooks/jotform/tests.py
@@ -45,6 +45,25 @@ class JotformHookTests(WebhookTestCase):
content_type="multipart/form-data",
)
+ def test_response_with_colon_comma_characters(self) -> None:
+ expected_title = "Sample testing"
+ expected_message = """
+* **Key1 with colon: and comma, end**: Value1 with colon: and comma, end
+* **Same Key**: Value 1
+* **Same Key**: Value 2
+* **Same Key-Value**: Same Key-Value
+* **Value 2**: Value 3
+* **Multiple Choice Question, options:**: Option; 1 Option2
+
+* **File Upload with colon : and comma , end**: [error; frontend, UI.png](https://www.jotform.com/uploads/kolanuvarun739/243490908500051/6197916587414311452/error%3B%20frontend%2C%20UI.png), [Screenshot_20250331_201054.png](https://www.jotform.com/uploads/kolanuvarun739/243490908500051/6197916587414311452/Screenshot_20250331_201054.png)""".strip()
+
+ self.check_webhook(
+ "response_with_colon_comma_characters",
+ expected_title,
+ expected_message,
+ content_type="multipart/form-data",
+ )
+
def test_bad_payload(self) -> None:
with self.assertRaisesRegex(AssertionError, "Unable to handle Jotform payload"):
self.check_webhook("response")
diff --git a/zerver/webhooks/jotform/view.py b/zerver/webhooks/jotform/view.py
index c3d20afefa1..f7c0ebc816d 100644
--- a/zerver/webhooks/jotform/view.py
+++ b/zerver/webhooks/jotform/view.py
@@ -33,6 +33,12 @@ def format_uploaded_file_links(file_question_value: str, file_url_mappings: dict
def is_non_file_upload_question_with_response(field: str, raw_request: dict[str, Any]) -> bool:
+ """
+ File upload questions are in raw_request["temp_upload"].
+ So, the other questions are the keys in raw_request starting
+ with "q". We exclude any questions with a zero length value,
+ because they are not in the "pretty" data of the payload.
+ """
field_value = raw_request.get(field)
return bool(
field
@@ -55,6 +61,58 @@ def is_non_file_upload_question_with_response(field: str, raw_request: dict[str,
)
+def format_value(value: str | list[str] | dict[str, str]) -> str:
+ if isinstance(value, str):
+ return value
+ elif isinstance(value, list):
+ return " ".join(value)
+
+ return " ".join(value.values())
+
+
+def get_pretty_fields(pretty: str, values: list[str]) -> list[tuple[str, str]]:
+ """
+ The format of "pretty" data in the payload is "key1:value1, key2:value2, ..."
+ The parameter "values" contains all substrings that appear as values
+ in the pretty data. Extracts each key-value pair by locating the position
+ of each value in the string and separating the corresponding key and value,
+ by tracking the starting position of the next key-value pair.
+ This approach is necessary because keys and values may contain colons
+ or commas, making it unreliable to simply split the string using ', '.
+
+ Returns a list of (key, value) pairs to preserve order and allow duplicate keys.
+ """
+
+ pretty_fields = []
+ pair_starting_index = 0
+
+ for value in values:
+ value_length = len(value)
+ possible_match_index = pretty.find(value + ":", pair_starting_index)
+
+ # If the current pair starts with a key that is equal to the value
+ if possible_match_index == pair_starting_index:
+ key = value
+ val = value
+
+ # 3 characters for colon, comma and space.
+ pair_starting_index += 2 * value_length + 3
+ else:
+ value_occurence_index = pretty.find(value, pair_starting_index)
+ value_ending_index = value_occurence_index + value_length
+
+ # -1 for colon, separating key and value
+ key = pretty[pair_starting_index : value_occurence_index - 1]
+ val = pretty[value_occurence_index:value_ending_index]
+
+ # 2 characters for comma and space, which separate the fields
+ pair_starting_index = value_ending_index + 2
+
+ pretty_fields.append((key, val))
+
+ return pretty_fields
+
+
@webhook_view("Jotform")
@typed_endpoint_without_parameters
def api_jotform_webhook(
@@ -63,41 +121,42 @@ def api_jotform_webhook(
) -> HttpResponse:
payload = request.POST
topic_name = payload.get("formTitle")
- fields = payload.get("pretty", "").split(", ")
-
- if not topic_name or not fields:
- raise JsonableError(_("Unable to handle Jotform payload"))
raw_request = json.loads(payload.get("rawRequest", "{}"))
+ non_file_upload_values_in_pretty_data = [
+ format_value(value)
+ for field, value in raw_request.items()
+ if is_non_file_upload_question_with_response(field, raw_request)
+ ]
+
url_mappings = {}
+ file_values_in_pretty_data = []
if raw_request.get("temp_upload"):
upload_keys = raw_request.get("temp_upload").keys()
file_questions_keys = [key.split("_", 1)[-1] for key in upload_keys]
+ file_values_in_pretty_data = [
+ " ".join(unquote(url.split("/")[-1]) for url in raw_request.get(key, []))
+ for key in file_questions_keys
+ ]
url_mappings = {
unquote(url.split("/")[-1]): url
for key in file_questions_keys
for url in raw_request.get(key, [])
}
- # File upload questions are in raw_request["temp_upload"].
- # So, the other questions are the keys in raw_request starting
- # with "q". We exclude any questions with a zero length value,
- # because they are not in the "pretty" data of the payload.
- count_non_file_upload_questions = len(
- [
- field
- for field in raw_request
- if is_non_file_upload_question_with_response(field, raw_request)
- ]
- )
+ # Non file values come earlier than file values in the "pretty" data of the payload.
+ pretty_values = non_file_upload_values_in_pretty_data + list(file_values_in_pretty_data)
+ pretty_fields = get_pretty_fields(payload.get("pretty", ""), pretty_values)
+
+ if not topic_name or not pretty_fields:
+ raise JsonableError(_("Unable to handle Jotform payload"))
form_response = ""
- for index, field in enumerate(fields):
- label, value = field.split(":", 1)
+ for index, (label, value) in enumerate(pretty_fields):
separator = " " if label.endswith("?") else ": "
# File upload fields are last in the "pretty" payload data.
- if index >= count_non_file_upload_questions:
+ if index >= len(non_file_upload_values_in_pretty_data):
value = format_uploaded_file_links(value, url_mappings)
# We add a new line so that image files are rendered
# correctly in the message.