Skip to content

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

SECURITY: Reject literal backslash path separators in iframe src traversal guard

Isaac Janzen0b5a27e68992
diff --git a/frontend/discourse/tests/unit/lib/sanitizer-test.js b/frontend/discourse/tests/unit/lib/sanitizer-test.js
index 13b4a407222..b872b823b8b 100644
--- a/frontend/discourse/tests/unit/lib/sanitizer-test.js
+++ b/frontend/discourse/tests/unit/lib/sanitizer-test.js
@@ -218,6 +218,38 @@ module("Unit | Utility | sanitizer", function (hooks) {
);
});
+ test("sanitize disallows iframe src with path traversal segments", function (assert) {
+ const engine = build({
+ siteSettings: {
+ allowed_iframes: "https://www.example.com/*/preview/",
+ },
+ });
+ const blocked = (src, description) => {
+ assert.strictEqual(
+ engine.cook(`<iframe src="${src}"></iframe>`),
+ "",
+ description
+ );
+ };
+
+ blocked(
+ "https://www.example.com/wild/preview/%2e%2e",
+ "disallows iframe with trailing URL-encoded path traversal"
+ );
+ blocked(
+ "https://www.example.com/wild/preview/%2e%2e?foo=bar",
+ "disallows iframe with URL-encoded path traversal before query string"
+ );
+ blocked(
+ "https://www.example.com/wild/preview/%2e%2e#fragment",
+ "disallows iframe with URL-encoded path traversal before fragment"
+ );
+ blocked(
+ "https://www.example.com/wild/preview/..\\outside",
+ "disallows iframe with literal backslash path traversal"
+ );
+ });
+
test("ids on headings", function (assert) {
const engine = build({ siteSettings: {} });
assert.strictEqual(
diff --git a/frontend/pretty-text/addon/sanitizer.js b/frontend/pretty-text/addon/sanitizer.js
index fe147f719e8..364130b7ae9 100644
--- a/frontend/pretty-text/addon/sanitizer.js
+++ b/frontend/pretty-text/addon/sanitizer.js
@@ -141,7 +141,7 @@ export function sanitize(text, allowLister) {
const iframeUrls = [decoded];
iframeUrls.push(value);
return (
- !decoded.match(/\/\.+\//) &&
+ !decoded.match(/\/\.+(?:[\/\\?#]|$)/) &&
allowedIframes.some((i) => {
const regex = i
// escape regex, keeping *
diff --git a/spec/requests/embed_controller_spec.rb b/spec/requests/embed_controller_spec.rb
index c173f913d1c..c0e98396dc3 100644
--- a/spec/requests/embed_controller_spec.rb
+++ b/spec/requests/embed_controller_spec.rb
@@ -296,6 +296,26 @@ RSpec.describe EmbedController do
expect(document.css("img[onerror]")).to be_empty
end
end
+
+ it "does not publish an iframe that traverses an allowlist path boundary" do
+ iframe_source = "https://www.example.com/wild/preview/..\\outside"
+ SiteSetting.allowed_iframes = "https://www.example.com/*/preview/"
+ sign_in(attacker)
+
+ post "/posts.json",
+ params: {
+ raw: %(<iframe src="#{iframe_source}"></iframe>),
+ topic_id: topic.id,
+ }
+
+ created_post = Post.find(response.parsed_body["id"])
+
+ aggregate_failures do
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["id"]).to eq(created_post.id)
+ expect(created_post.cooked).not_to include(iframe_source)
+ end
+ end
end
describe "full_app redirect" do