A platform for community discussion. Free, open, simple.
SECURITY: Sanitize graphviz SVG anchor links to prevent XSS
Penar Musaraj472f9e1f7855
diff --git a/plugins/discourse-graphviz/assets/javascripts/initializers/discourse-graphviz.js b/plugins/discourse-graphviz/assets/javascripts/initializers/discourse-graphviz.jsindex 612297a6a65..20c31aee642 100644--- a/plugins/discourse-graphviz/assets/javascripts/initializers/discourse-graphviz.js+++ b/plugins/discourse-graphviz/assets/javascripts/initializers/discourse-graphviz.js@@ -37,6 +37,7 @@ export default {format: "svg",engine,});+$container.html(svgChart);} catch (e) {const $error = $(diff --git a/plugins/discourse-graphviz/db/post_migrate/20260218104617_rebake_graphviz_posts.rb b/plugins/discourse-graphviz/db/post_migrate/20260218104617_rebake_graphviz_posts.rbnew file mode 100644index 00000000000..1b0b28bd5b8--- /dev/null+++ b/plugins/discourse-graphviz/db/post_migrate/20260218104617_rebake_graphviz_posts.rb@@ -0,0 +1,17 @@+# frozen_string_literal: true++class RebakeGraphvizPosts < ActiveRecord::Migration[7.2]+ def up+ # Rebake posts with graphviz graphs to apply updated link sanitization+ execute <<~SQL+ UPDATE posts+ SET baked_version = 0+ WHERE raw LIKE '%[graphviz]%'+ OR cooked LIKE '%class="graphviz%'+ SQL+ end++ def down+ # Do nothing+ end+enddiff --git a/plugins/discourse-graphviz/plugin.rb b/plugins/discourse-graphviz/plugin.rbindex 3df62598575..bbe5c80dc2f 100644--- a/plugins/discourse-graphviz/plugin.rb+++ b/plugins/discourse-graphviz/plugin.rb@@ -13,6 +13,8 @@ register_svg_icon "diagram-project"register_asset "stylesheets/common/graphviz.scss"module ::DiscourseGraphviz+ ALLOWED_URL_SCHEMES = %w[http https].freeze+def self.contextcontext = MiniRacer::Context.newcontext.load("#{Rails.root}/plugins/discourse-graphviz/public/javascripts/viz-3.0.1.js")@@ -23,6 +25,28 @@ module ::DiscourseGraphviz@@allowed_svg_xpath ||="//*[#{UploadCreator::ALLOWED_SVG_ELEMENTS.map { |e| "name()!='#{e}'" }.join(" and ")}]"end++ def self.sanitize_svg_links(svg_node)+ svg_node+ .css("a")+ .each do |anchor|+ href = anchor["href"] || anchor["xlink:href"]+ next unless href++ begin+ uri = URI.parse(href)+ scheme = uri.scheme&.downcase++ if scheme && !ALLOWED_URL_SCHEMES.include?(scheme)+ anchor.replace(anchor.children)+ elsif scheme.nil? && href.strip.match?(/\A\s*javascript:/i)+ anchor.replace(anchor.children)+ end+ rescue URI::InvalidURIError+ anchor.replace(anchor.children)+ end+ end+ endendafter_initialize do@@ -52,6 +76,7 @@ after_initialize donew_graph_node = Nokogiri::HTML.fragment(svg_graph).css("svg").first# rubocop:enable Discourse/NoNokogiriHtmlFragmentnew_graph_node["class"] = "graphviz-svg-render"+ DiscourseGraphviz.sanitize_svg_links(new_graph_node)new_graph_node.xpath(DiscourseGraphviz.allowed_svg_xpath).removegraph.replace new_graph_nodenextdiff --git a/plugins/discourse-graphviz/spec/integration/graphviz_spec.rb b/plugins/discourse-graphviz/spec/integration/graphviz_spec.rbnew file mode 100644index 00000000000..a3c1c337520--- /dev/null+++ b/plugins/discourse-graphviz/spec/integration/graphviz_spec.rb@@ -0,0 +1,123 @@+# frozen_string_literal: true++RSpec.describe "Graphviz" do+ before do+ SiteSetting.discourse_graphviz_enabled = true+ SiteSetting.graphviz_default_svg = true+ end++ it "strips anchor elements with javascript: URLs to prevent XSS" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="XSS", URL="javascript:alert(1)"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).not_to include("javascript:")+ expect(cp.html).to include("graphviz-svg-render")+ expect(cp.html).to include("<text")+ end++ it "preserves legitimate HTTP and HTTPS URLs" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G {+ a [label="HTTP", URL="http://example.com"]+ b [label="HTTPS", URL="https://example.com"]+ a -> b+ }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).to include('href="http://example.com"')+ expect(cp.html).to include('href="https://example.com"')+ expect(cp.html).to include("<a")+ end++ it "strips mailto: URLs" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="Email", URL="mailto:[email protected]"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).not_to include("mailto:")+ end++ it "strips anchor elements with data: URLs to prevent XSS" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="Data", URL="data:text/html,<script>alert(1)</script>"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).not_to include("data:")+ expect(cp.html).not_to include("alert")+ end++ it "strips anchor elements with vbscript: URLs to prevent XSS" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="VBScript", URL="vbscript:alert(1)"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).not_to include("vbscript:")+ end++ it "strips anchor elements with file: URLs" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="File", URL="file:///etc/passwd"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).not_to include("file:")+ end++ it "handles case-insensitive javascript: URLs" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="XSS", URL="JaVaScRiPt:alert(1)"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).not_to include("JaVaScRiPt:")+ expect(cp.html).not_to include("javascript:")+ end++ it "preserves relative URLs" do+ post = Fabricate(:post, raw: <<~MD)+ [graphviz]+ digraph G { a [label="Relative", URL="/some/path"] }+ [/graphviz]+ MD++ cp = CookedPostProcessor.new(post)+ cp.post_process++ expect(cp.html).to include('href="/some/path"')+ expect(cp.html).to include("<a")+ end+end