This repository was archived by the owner on Jul 3, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 12 files changed +93
-49
lines changed Expand file tree Collapse file tree 12 files changed +93
-49
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
8
8
### Compatible changes
9
9
* Add compatibility with Rails 7.1
10
10
* Add compatibility with HAML 6
11
- * NOTE: Don't use HAML 6.0.0. AngularXSS relies on a patch [ introduced in 6.0.1] ( https://github.com/haml/haml/blob/main/CHANGELOG.md#601 ) . Anything newer should be fine - the gem is tested against 6.3
11
+ * NOTE: Don't use HAML 6.0.0. AngularXSS relies on a patch [ introduced in 6.0.1] ( https://github.com/haml/haml/blob/main/CHANGELOG.md#601 ) . Anything newer should be fine - the gem is currently tested against HAML 6.3
12
12
13
13
### Breaking changes
14
14
Original file line number Diff line number Diff line change 1
1
# Use module_eval so we crash when ERB::Util has not yet been loaded.
2
2
if defined? ( ActiveSupport ::CoreExt ::ERBUtil ) && ERB ::Util . is_a? ( ActiveSupport ::CoreExt ::ERBUtil )
3
-
4
3
# Rails 7.1+
5
4
# https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/erb/util.rb
6
5
module ERBUtilExt
Original file line number Diff line number Diff line change 7
7
8
8
if defined? ( ActionView ::VERSION ) && Gem ::Version . new ( ActionView ::VERSION ::STRING ) >= Gem ::Version . new ( '7.1' )
9
9
# ActionView < 7.1 used our patched ERB::Util.h to escape, 7.1 switched to CGI.escapeHTML
10
- module OutputBufferExt
10
+ module OutputBufferWithEscapedAngularXSS
11
11
def <<( value )
12
12
super ( AngularXss ::Escaper . escape_if_unsafe ( value ) )
13
13
end
14
14
15
- alias :concat :<<
16
- alias :append= :<<
15
+ def concat ( value )
16
+ super ( AngularXss ::Escaper . escape_if_unsafe ( value ) )
17
+ end
18
+
19
+ def append = ( value )
20
+ super ( AngularXss ::Escaper . escape_if_unsafe ( value ) )
21
+ end
17
22
end
18
23
19
- ActionView ::OutputBuffer . prepend OutputBufferExt
24
+ ActionView ::OutputBuffer . prepend OutputBufferWithEscapedAngularXSS
20
25
end
Original file line number Diff line number Diff line change 1
1
module AngularXss
2
- VERSION = '0.4.1' # todo
2
+ VERSION = '0.4.1'
3
3
end
Original file line number Diff line number Diff line change 1
- require 'spec_helper'
2
-
3
1
describe 'Angular XSS prevention in ERB' , :type => :view do
4
-
5
2
it_should_behave_like 'engine preventing Angular XSS' , :partial => 'test_erb'
6
-
7
3
end
8
4
9
5
describe ERB ::Util do
Original file line number Diff line number Diff line change
1
+ describe AngularXss ::Escaper do
2
+ describe '.escape' do
3
+ it 'replaces double brackets with a closed variant' do
4
+ expect ( described_class . escape ( '{{' ) ) . to eq ( '{{ $root.DOUBLE_LEFT_CURLY_BRACE }}' )
5
+ end
6
+
7
+ it 'does not handle HTML safe strings differently' do
8
+ expect ( described_class . escape ( '{{' . html_safe ) ) . to eq ( '{{ $root.DOUBLE_LEFT_CURLY_BRACE }}' )
9
+ end
10
+ end
11
+
12
+ describe '.escape_if_unsafe' do
13
+ it 'replaces double brackets with a closed variant' do
14
+ expect ( described_class . escape_if_unsafe ( '{{' ) ) . to eq ( '{{ $root.DOUBLE_LEFT_CURLY_BRACE }}' )
15
+ end
16
+
17
+ it 'does not modify HTML safe strings' do
18
+ expect ( described_class . escape_if_unsafe ( '{{' . html_safe ) ) . to eq ( '{{' )
19
+ end
20
+ end
21
+ end
Original file line number Diff line number Diff line change 1
- require 'spec_helper'
2
-
3
1
describe 'Angular XSS prevention in Haml' , :type => :view do
4
2
5
3
it_should_behave_like 'engine preventing Angular XSS' , :partial => 'test_haml'
Original file line number Diff line number Diff line change 1
- require 'spec_helper'
2
-
3
1
describe ActionView ::OutputBuffer do
4
2
describe '#<<' do
5
3
it 'escapes angular brackets' do
6
4
expect ( ( subject << "{{unsafe}}" ) . to_s ) . to eq ( "{{ $root.DOUBLE_LEFT_CURLY_BRACE }}unsafe}}" )
7
5
end
8
6
9
- it 'still allows concatting nil' do
7
+ it 'does not change behavior for already HTML safe strings' do
8
+ expect ( ( subject << "{{safe}}" . html_safe ) . to_s ) . to eq ( "{{safe}}" )
9
+ end
10
+
11
+ it 'allows concatting nil' do
10
12
expect { subject << nil } . to_not raise_error
11
13
end
14
+ end
15
+
16
+ describe '#concat' do
17
+ it 'escapes angular brackets' do
18
+ expect ( ( subject . concat "{{unsafe}}" ) . to_s ) . to eq ( "{{ $root.DOUBLE_LEFT_CURLY_BRACE }}unsafe}}" )
19
+ end
12
20
13
21
it 'does not change behavior for already HTML safe strings' do
14
- expect { subject << nil } . to_not raise_error
15
- expect ( ( subject << "{{safe}}" . html_safe ) . to_s ) . to eq ( "{{safe}}" )
22
+ expect ( ( subject . concat "{{safe}}" . html_safe ) . to_s ) . to eq ( "{{safe}}" )
16
23
end
17
24
25
+ it 'allows concatting nil' do
26
+ expect { subject . concat nil } . to_not raise_error
27
+ end
28
+ end
29
+
30
+ describe '#append=' do
31
+ it 'escapes angular brackets' do
32
+ subject . append = "{{unsafe}}"
33
+ expect ( subject . to_s ) . to eq ( "{{ $root.DOUBLE_LEFT_CURLY_BRACE }}unsafe}}" )
34
+ end
35
+
36
+ it 'does not change behavior for already HTML safe strings' do
37
+ subject . append = "{{safe}}" . html_safe
38
+ expect ( subject . to_s ) . to eq ( "{{safe}}" )
39
+ end
40
+
41
+ it 'allows concatting nil' do
42
+ expect { subject . append = nil } . to_not raise_error
43
+ end
18
44
end
19
45
end
Original file line number Diff line number Diff line change 1
- require 'spec_helper'
2
-
3
1
describe ActiveSupport ::SafeBuffer do
4
2
5
- it 'still allows concatting nil' do
6
- expect { subject << nil } . to_not raise_error
3
+ describe '#<<' do
4
+ it 'escapes angular brackets' do
5
+ subject << "{{unsafe}}"
6
+ expect ( subject . to_s ) . to eq ( "{{ $root.DOUBLE_LEFT_CURLY_BRACE }}unsafe}}" )
7
+ end
8
+
9
+ it 'allows concatting nil' do
10
+ expect { subject << nil } . to_not raise_error
11
+ end
12
+ end
13
+
14
+ describe '#+' do
15
+ it 'escapes angular brackets' do
16
+ combined_string = subject + "{{unsafe}}"
17
+ expect ( combined_string . to_s ) . to eq ( "{{ $root.DOUBLE_LEFT_CURLY_BRACE }}unsafe}}" )
18
+ end
7
19
end
8
20
9
21
end
Original file line number Diff line number Diff line change @@ -29,13 +29,3 @@ def self.env
29
29
Dir [ "#{ File . dirname ( __FILE__ ) } /support/**/*.rb" ] . each { |f | require f }
30
30
31
31
TEMPLATE_ROOT = Pathname . new ( __dir__ ) . join ( 'templates' )
32
-
33
-
34
- RSpec . configure do |config |
35
- config . mock_with :rspec do |c |
36
- c . syntax = [ :should , :expect ]
37
- end
38
- config . expect_with :rspec do |c |
39
- c . syntax = [ :should , :expect ]
40
- end
41
- end
You can’t perform that action at this time.
0 commit comments