File tree Expand file tree Collapse file tree 4 files changed +36
-11
lines changed Expand file tree Collapse file tree 4 files changed +36
-11
lines changed Original file line number Diff line number Diff line change 1- 3.2 .0
1+ 3.3 .0
Original file line number Diff line number Diff line change @@ -356,14 +356,6 @@ def capture
356356 end
357357
358358 private def __attributes__ ( **attributes )
359- if attributes [ :href ] &.start_with? ( /\s *javascript/ )
360- attributes . delete ( :href )
361- end
362-
363- if attributes [ "href" ] &.start_with? ( /\s *javascript/ )
364- attributes . delete ( "href" )
365- end
366-
367359 buffer = +""
368360 __build_attributes__ ( attributes , buffer : buffer )
369361
@@ -384,8 +376,11 @@ def capture
384376 else k . to_s
385377 end
386378
379+ lower_name = name . downcase
380+ next if lower_name == "href" && v . start_with? ( /\s *javascript:/i )
381+
387382 # Detect unsafe attribute names. Attribute names are considered unsafe if they match an event attribute or include unsafe characters.
388- if HTML ::EVENT_ATTRIBUTES [ name ] || name . match? ( /[<>&"']/ )
383+ if HTML ::EVENT_ATTRIBUTES [ lower_name ] || name . match? ( /[<>&"']/ )
389384 raise ArgumentError , "Unsafe attribute name detected: #{ k } ."
390385 end
391386
Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22
33module Phlex
4- VERSION = "1.2.1 "
4+ VERSION = "1.2.2 "
55end
Original file line number Diff line number Diff line change 33describe Phlex ::HTML do
44 extend ViewHelper
55
6+ with "naughty javascript links" do
7+ view do
8+ def template
9+ a ( href : "javascript:alert(1)" ) { "a" }
10+ a ( href : "JAVASCRIPT:alert(1)" ) { "b" }
11+ a ( href : :"JAVASCRIPT:alert(1)" ) { "c" }
12+ a ( HREF : "javascript:alert(1)" ) { "d" }
13+ end
14+ end
15+
16+ it "removes the href attributes" do
17+ expect ( output ) . to be == "<a>a</a><a>b</a><a>c</a><a>d</a>"
18+ end
19+ end
20+
21+ with "naughty uppercase event tag" do
22+ view do
23+ def template
24+ button ONCLICK : "ALERT(1)" do
25+ "naughty button"
26+ end
27+ end
28+ end
29+
30+ it "raises" do
31+ expect { output } . to raise_exception ArgumentError ,
32+ message : be == "Unsafe attribute name detected: ONCLICK."
33+ end
34+ end
35+
636 with "naughty text" do
737 view do
838 def template
You can’t perform that action at this time.
0 commit comments