Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Disallow URL-encoded path traversal in iframe URLs

Penar Musarajc4762a4fac2f
diff --git a/frontend/discourse/tests/unit/lib/sanitizer-test.js b/frontend/discourse/tests/unit/lib/sanitizer-test.js
index b4567108c42..13b4a407222 100644
--- a/frontend/discourse/tests/unit/lib/sanitizer-test.js
+++ b/frontend/discourse/tests/unit/lib/sanitizer-test.js
@@ -101,6 +101,18 @@ module("Unit | Utility | sanitizer", function (hooks) {
"disallows iframe to example.com not matching wild card"
);
+ cooked(
+ '<iframe src="https://www.example.com/wild/preview/%2e%2e/other"></iframe>',
+ "",
+ "disallows iframe with URL-encoded path traversal"
+ );
+
+ cooked(
+ '<iframe src="https://www.example.com/wild/preview/%2E%2E/other"></iframe>',
+ "",
+ "disallows iframe with uppercase URL-encoded path traversal"
+ );
+
cooked(
`BEFORE\n\n<iframe src=http://example.com>\n\nINSIDE\n\n</iframe>\n\nAFTER`,
`<p>BEFORE</p>\n\n<p>AFTER</p>`,
diff --git a/frontend/pretty-text/addon/sanitizer.js b/frontend/pretty-text/addon/sanitizer.js
index 880a413ff2d..c9a8220526c 100644
--- a/frontend/pretty-text/addon/sanitizer.js
+++ b/frontend/pretty-text/addon/sanitizer.js
@@ -131,14 +131,24 @@ export function sanitize(text, allowLister) {
hrefAllowed(value, extraHrefMatchers)) ||
(tag === "iframe" &&
name === "src" &&
- !value.match(/\/\.+\//) &&
- allowedIframes.some((i) => {
- const regex = i
- // escape regex, keeping *
- .replace(/[.+?^${}()|[\]\\]/g, "\\$&")
- .replace(/\*/g, "[^/]+");
- return new RegExp(`^${regex}.*$`, "i").test(value);
- }))
+ (() => {
+ let decoded;
+ try {
+ decoded = decodeURIComponent(value);
+ } catch {
+ return false;
+ }
+ return (
+ !decoded.match(/\/\.+\//) &&
+ allowedIframes.some((i) => {
+ const regex = i
+ // escape regex, keeping *
+ .replace(/[.+?^${}()|[\]\\]/g, "\\$&")
+ .replace(/\*/g, "[^/]+");
+ return new RegExp(`^${regex}.*$`, "i").test(decoded);
+ })
+ );
+ })())
) {
return attr(name, value);
}
diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb
index 26f9fddaa67..59ab4a2f8c5 100644
--- a/spec/lib/pretty_text_spec.rb
+++ b/spec/lib/pretty_text_spec.rb
@@ -2643,6 +2643,21 @@ HTML
expect(PrettyText.cook(raw).strip).to eq(html.strip)
end
+ it "can skip URL-encoded relative paths in allowlist iframes" do
+ SiteSetting.allowed_iframes = "https://bob.com/abc/def"
+ raw = <<~HTML
+ <iframe src='https://bob.com/abc/def'></iframe>
+ <iframe src='https://bob.com/abc/def/%2e%2e/ghi'></iframe>
+ <iframe src='https://bob.com/abc/def/%2E%2E/ghi'></iframe>
+ HTML
+
+ html = <<~HTML
+ <iframe src="https://bob.com/abc/def"></iframe>
+ HTML
+
+ expect(PrettyText.cook(raw).strip).to eq(html.strip)
+ end
+
it "You can disable linkify" do
md = "www.cnn.com test.it http://test.com https://test.ab https://a"
cooked = PrettyText.cook(md)