CVE-2024-28199: Insufficient Information (NVD-CWE-noinfo)
value: phlex is an open source framework for building object-oriented views in Ruby. There is a potential cross-site scripting (XSS) vulnerability that can be exploited via maliciously crafted user data. This was due to improper case-sensitivity in the code that was meant to prevent these attacks. If you render an `<a>` tag with an `href` attribute set to a user-provided link, that link could potentially execute JavaScript when clicked by another user. If you splat user-provided attributes when rendering any HTML tag, malicious event attributes could be included in the output, executing JavaScript when the events are triggered by another user. Patches are available on RubyGems for all 1.x minor versions. Users are advised to upgrade. Users unable to upgrade should consider configuring a content security policy that does not allow `unsafe-inline`.
CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
Published: 2024-03-11T23:15Z
Repository: https://github.com/phlex-ruby/phlex
Language: Ruby
Files changed: lib/phlex/sgml.rb, test/phlex/view/naughty_business.rb
Commit date: 2024-03-11 22:32:45 +0000
Commit message: Fix improper case-sensitivity

See https://github.com/phlex-ruby/phlex/security/advisories/GHSA-242p-4v39-2v8g

Diff: diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb
index c76cf54..653bf1d 100644
--- a/lib/phlex/sgml.rb
+++ b/lib/phlex/sgml.rb
@@ -391,18 +391,10 @@ module Phlex
 		def __final_attributes__(**attributes)
 			if respond_to?(:process_attributes)
 				attributes = process_attributes(**attributes)
 			end
 
-			if attributes[:href]&.start_with?(/\s*javascript:/)
-				attributes.delete(:href)
-			end
-
-			if attributes["href"]&.start_with?(/\s*javascript:/)
-				attributes.delete("href")
-			end
-
 			buffer = +""
 			__build_attributes__(attributes, buffer: buffer)
 
 			buffer
 		end
@@ -416,12 +408,15 @@ module Phlex
 					when String then k
 					when Symbol then k.name.tr("_", "-")
 					else raise ArgumentError, "Attribute keys should be Strings or Symbols."
 				end
 
+				lower_name = name.downcase
+				next if lower_name == "href" && v.start_with?(/\s*javascript:/i)
+
 				# Detect unsafe attribute names. Attribute names are considered unsafe if they match an event attribute or include unsafe characters.
-				if HTML::EVENT_ATTRIBUTES[name] || name.match?(/[<>&"']/)
+				if HTML::EVENT_ATTRIBUTES[lower_name] || name.match?(/[<>&"']/)
 					raise ArgumentError, "Unsafe attribute name detected: #{k}."
 				end
 
 				case v
 				when true
diff --git a/test/phlex/view/naughty_business.rb b/test/phlex/view/naughty_business.rb
index 5e4cad4..c75dcff 100644
--- a/test/phlex/view/naughty_business.rb
+++ b/test/phlex/view/naughty_business.rb
@@ -1,10 +1,40 @@
 # frozen_string_literal: true
 
 describe Phlex::HTML do
 	extend ViewHelper
 
+	with "naughty javascript links" do
+		view do
+			def template
+				a(href: "javascript:alert(1)") { "a" }
+				a(href: "JAVASCRIPT:alert(1)") { "b" }
+				a(href: :"JAVASCRIPT:alert(1)") { "c" }
+				a(HREF: "javascript:alert(1)") { "d" }
+			end
+		end
+
+		it "removes the href attributes" do
+			expect(output).to be == "<a>a</a><a>b</a><a>c</a><a>d</a>"
+		end
+	end
+
+	with "naughty uppercase event tag" do
+		view do
+			def template
+				button ONCLICK: "ALERT(1)" do
+					"naughty button"
+				end
+			end
+		end
+
+		it "raises" do
+			expect { output }.to raise_exception ArgumentError,
+				message: be == "Unsafe attribute name detected: ONCLICK."
+		end
+	end
+
 	with "naughty text" do
 		view do
 			def view_template
 				plain %("><script type="text/javascript" src="bad_script.js"></script>)
 			end

Vulnerable code: 			if attributes[:href]&.start_with?(/\s*javascript:/)
				attributes.delete(:href)
			end

			if attributes["href"]&.start_with?(/\s*javascript:/)
				attributes.delete("href")
			end

				if HTML::EVENT_ATTRIBUTES[name] || name.match?(/[<>&"']/)
Fixed code: 				lower_name = name.downcase
				next if lower_name == "href" && v.start_with?(/\s*javascript:/i)

				if HTML::EVENT_ATTRIBUTES[lower_name] || name.match?(/[<>&"']/)
	with "naughty javascript links" do
		view do
			def template
				a(href: "javascript:alert(1)") { "a" }
				a(href: "JAVASCRIPT:alert(1)") { "b" }
				a(href: :"JAVASCRIPT:alert(1)") { "c" }
				a(HREF: "javascript:alert(1)") { "d" }
			end
		end

		it "removes the href attributes" do
			expect(output).to be == "<a>a</a><a>b</a><a>c</a><a>d</a>"
		end
	end

	with "naughty uppercase event tag" do
		view do
			def template
				button ONCLICK: "ALERT(1)" do
					"naughty button"
				end
			end
		end

		it "raises" do
			expect { output }.to raise_exception ArgumentError,
				message: be == "Unsafe attribute name detected: ONCLICK."
		end
	end