From 9b50de45696312b2b356dad00589af0bf0fcfacd Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 17 Jan 2024 11:30:27 +0000 Subject: [PATCH] SECURITY: Properly escape user content within `` + # Can't use nokogiri because it parses documents with the 'scripting flag' disabled, and therefore parses html inside noscript tags + noscript_content = response.body.scan(%r{(.*?)}m).join("\n") + end + + it "does not affect normal content" do + post = Fabricate(:post, raw: 'This is a post with an image <Look at this!>') + get post.url + + expect(noscript_content).to include('<Look at this!>') + end + + it "escapes noscript in attribute" do + post = + Fabricate( + :post, + raw: 'This is a post with an image </noscript> containing a noscript end tag', + ) + get post.url + + expect(noscript_content).to include('</noscript>') + end + + it "escapes noscript with trailing whitespace" do + post = + Fabricate( + :post, + raw: 'This is a post with an image </noscript  > containing a noscript end tag', + ) + get post.url + + expect(noscript_content).to include('</noscript  >') + end + + it "escapes noscript with leading whitespace" do + # The spec doesn't accept closing tags with leading whitespace. Browsers follow that, but some other parsers are more relaxed so we escape anyway + post = + Fabricate( + :post, + raw: 'This is a post with an image </  noscript> containing a noscript end tag', + ) + get post.url + + expect(noscript_content).to include('</  noscript>') + end +end