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.

playgrounds: Validate language input for spaces and reserved keywords.

Nithyaraj Mudhaliyar83daf4aa576d
diff --git a/web/e2e-tests/realm-playground.test.ts b/web/e2e-tests/realm-playground.test.ts
index ada273e3e84..f9ce396f2bb 100644
--- a/web/e2e-tests/realm-playground.test.ts
+++ b/web/e2e-tests/realm-playground.test.ts
@@ -12,9 +12,7 @@ type Playground = {
async function _add_playground_and_return_status(page: Page, payload: Playground): Promise<string> {
await page.waitForSelector(".admin-playground-form", {visible: true});
- // Let's first ensure that the success/failure status from an earlier step has disappeared.
const admin_playground_status_selector = "div#admin-playground-status";
- await page.waitForSelector(admin_playground_status_selector, {hidden: true});
await common.select_item_via_typeahead(
page,
@@ -34,6 +32,9 @@ async function _add_playground_and_return_status(page: Page, payload: Playground
el.click();
});
+ // Wait for the request to complete by checking when the button is re-enabled.
+ await page.waitForSelector("button#submit_playground_button:not([disabled])");
+
// We return the success/failure status message back to the caller.
await page.waitForSelector(admin_playground_status_selector, {visible: true});
const admin_playground_status = await common.get_text_from_selector(
@@ -81,7 +82,7 @@ async function test_invalid_playground_parameters(page: Page): Promise<void> {
payload.url_template = "https://python.example.com?code={code}";
payload.pygments_language = "py!@%&";
status = await _add_playground_and_return_status(page, payload);
- assert.strictEqual(status, "Failed: Invalid characters in pygments language");
+ assert.strictEqual(status, "Failed: Invalid character in language: !");
}
async function test_successful_playground_deletion(page: Page): Promise<void> {
diff --git a/web/src/settings_playgrounds.ts b/web/src/settings_playgrounds.ts
index 2076351bc5e..05959e33925 100644
--- a/web/src/settings_playgrounds.ts
+++ b/web/src/settings_playgrounds.ts
@@ -146,12 +146,7 @@ function build_page(): void {
},
error(xhr) {
$add_playground_button.prop("disabled", false);
- ui_report.error(
- $t_html({defaultMessage: "Failed"}),
- xhr,
- $playground_status,
- 3000,
- );
+ ui_report.error($t_html({defaultMessage: "Failed"}), xhr, $playground_status);
},
});
});
diff --git a/zerver/lib/markdown/fenced_code.py b/zerver/lib/markdown/fenced_code.py
index f412c98966e..2efcd347e62 100644
--- a/zerver/lib/markdown/fenced_code.py
+++ b/zerver/lib/markdown/fenced_code.py
@@ -94,6 +94,7 @@ from typing_extensions import override
from zerver.lib.exceptions import MarkdownRenderingError
from zerver.lib.markdown.priorities import PREPROCESSOR_PRIORITIES
from zerver.lib.tex import render_tex
+from zerver.models.realm_playgrounds import PLAYGROUND_LANGUAGE_REGEX
# Global vars
FENCE_RE = re.compile(
@@ -109,7 +110,9 @@ FENCE_RE = re.compile(
# language, like ".py" or "{javascript}"
\{?\.?
(?P<lang>
- [a-zA-Z0-9_+-./#]+
+ """
+ + PLAYGROUND_LANGUAGE_REGEX
+ + r"""
) # "py" or "javascript"
[ ]* # spaces
diff --git a/zerver/migrations/0001_squashed_0569.py b/zerver/migrations/0001_squashed_0569.py
index f22f41aab08..f025c83e0e1 100644
--- a/zerver/migrations/0001_squashed_0569.py
+++ b/zerver/migrations/0001_squashed_0569.py
@@ -3099,7 +3099,7 @@ class Migration(migrations.Migration):
validators=[
django.core.validators.RegexValidator(
message="Invalid characters in pygments language",
- regex="^[ a-zA-Z0-9_+-./#]*$",
+ regex="^[a-zA-Z0-9_+-./#]+$",
)
],
),
diff --git a/zerver/migrations/0315_realmplayground.py b/zerver/migrations/0315_realmplayground.py
index c795d11909d..555e0253fad 100644
--- a/zerver/migrations/0315_realmplayground.py
+++ b/zerver/migrations/0315_realmplayground.py
@@ -33,7 +33,7 @@ class Migration(migrations.Migration):
validators=[
django.core.validators.RegexValidator(
message="Invalid characters in pygments language",
- regex="^[ a-zA-Z0-9_+-./#]*$",
+ regex="^[a-zA-Z0-9_+-./#]+$",
)
],
),
diff --git a/zerver/models/realm_playgrounds.py b/zerver/models/realm_playgrounds.py
index ddd297b9102..64c8aeee01d 100644
--- a/zerver/models/realm_playgrounds.py
+++ b/zerver/models/realm_playgrounds.py
@@ -10,6 +10,8 @@ from zerver.lib.types import RealmPlaygroundDict
from zerver.models.linkifiers import url_template_validator
from zerver.models.realms import Realm
+PLAYGROUND_LANGUAGE_REGEX = r"[a-zA-Z0-9_+-./#]+"
+
class RealmPlayground(models.Model):
"""Server side storage model to store playground information needed by our
@@ -18,6 +20,8 @@ class RealmPlayground(models.Model):
MAX_PYGMENTS_LANGUAGE_LENGTH = 40
+ RESTRICTED_KEYWORDS = {"latex", "math", "quote", "spoiler"}
+
realm = models.ForeignKey(Realm, on_delete=CASCADE)
url_template = models.TextField(validators=[url_template_validator])
@@ -33,7 +37,8 @@ class RealmPlayground(models.Model):
# language in the code block.
validators=[
RegexValidator(
- regex=r"^[ a-zA-Z0-9_+-./#]*$", message=_("Invalid characters in pygments language")
+ regex=rf"^{PLAYGROUND_LANGUAGE_REGEX}$",
+ message=_("Invalid characters in pygments language"),
)
],
)
diff --git a/zerver/tests/test_realm_playgrounds.py b/zerver/tests/test_realm_playgrounds.py
index 7a4f240a0f6..3cef3ef7d3f 100644
--- a/zerver/tests/test_realm_playgrounds.py
+++ b/zerver/tests/test_realm_playgrounds.py
@@ -60,7 +60,10 @@ class RealmPlaygroundTests(ZulipTestCase):
"url_template": "https://template.com{code}",
}
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
- self.assert_json_error(resp, "Invalid characters in pygments language")
+ self.assert_json_error(
+ resp,
+ "Invalid character in language: $",
+ )
payload = {
"name": "Template with an unexpected variable",
@@ -88,6 +91,30 @@ class RealmPlaygroundTests(ZulipTestCase):
resp = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
self.assert_json_error(resp, 'Missing the required variable "code" in the URL template')
+ def test_add_realm_playground_validation(self) -> None:
+ iago = self.example_user("iago")
+
+ # Test to check that spaces are rejected
+ payload = {
+ "name": "Bad Name",
+ "pygments_language": "rust lang",
+ "url_template": "https://example.com",
+ }
+ result = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
+ self.assert_json_error(
+ result,
+ "Invalid character in language: ",
+ )
+
+ # Test to check that restricted keywords are rejected
+ payload = {
+ "name": "Bad Keyword",
+ "pygments_language": "math",
+ "url_template": "https://example.com",
+ }
+ result = self.api_post(iago, "/api/v1/realm/playgrounds", payload)
+ self.assert_json_error(result, "Language 'math' is not allowed.")
+
def test_create_already_existing_playground(self) -> None:
iago = self.example_user("iago")
diff --git a/zerver/views/realm_playgrounds.py b/zerver/views/realm_playgrounds.py
index 7242d8f2a29..5adcb7f5856 100644
--- a/zerver/views/realm_playgrounds.py
+++ b/zerver/views/realm_playgrounds.py
@@ -13,6 +13,7 @@ from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import PathOnly, typed_endpoint
from zerver.lib.validator import check_capped_string
from zerver.models import Realm, RealmPlayground, UserProfile
+from zerver.models.realm_playgrounds import PLAYGROUND_LANGUAGE_REGEX
def check_pygments_language(var_name: str, val: object) -> str:
@@ -21,10 +22,14 @@ def check_pygments_language(var_name: str, val: object) -> str:
# Pygments languages. Keeping it open would allow us to hook up a "playground"
# for custom "languages" that aren't known to Pygments. We use a similar strategy
# even in our fenced_code Markdown processor.
- valid_pygments_language = re.compile(r"^[ a-zA-Z0-9_+-./#]*$")
- matched_results = valid_pygments_language.match(s)
- if not matched_results:
- raise JsonableError(_("Invalid characters in pygments language"))
+ if not re.match(rf"^{PLAYGROUND_LANGUAGE_REGEX}$", s):
+ for char in s:
+ if not re.match(rf"^{PLAYGROUND_LANGUAGE_REGEX}$", char):
+ raise JsonableError(
+ _("Invalid character in language: {character}").format(character=char)
+ )
+ if s in RealmPlayground.RESTRICTED_KEYWORDS:
+ raise JsonableError(_("Language '{language}' is not allowed.").format(language=s))
return s