Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit fecdf8b

Browse files
committed
Self Review
1 parent 805c192 commit fecdf8b

File tree

12 files changed

+93
-49
lines changed

12 files changed

+93
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
88
### Compatible changes
99
* Add compatibility with Rails 7.1
1010
* 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
1212

1313
### Breaking changes
1414

lib/angular_xss/erb.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Use module_eval so we crash when ERB::Util has not yet been loaded.
22
if defined?(ActiveSupport::CoreExt::ERBUtil) && ERB::Util.is_a?(ActiveSupport::CoreExt::ERBUtil)
3-
43
# Rails 7.1+
54
# https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/erb/util.rb
65
module ERBUtilExt

lib/angular_xss/output_buffer.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
if defined?(ActionView::VERSION) && Gem::Version.new(ActionView::VERSION::STRING) >= Gem::Version.new('7.1')
99
# ActionView < 7.1 used our patched ERB::Util.h to escape, 7.1 switched to CGI.escapeHTML
10-
module OutputBufferExt
10+
module OutputBufferWithEscapedAngularXSS
1111
def <<(value)
1212
super(AngularXss::Escaper.escape_if_unsafe(value))
1313
end
1414

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
1722
end
1823

19-
ActionView::OutputBuffer.prepend OutputBufferExt
24+
ActionView::OutputBuffer.prepend OutputBufferWithEscapedAngularXSS
2025
end

lib/angular_xss/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module AngularXss
2-
VERSION = '0.4.1' # todo
2+
VERSION = '0.4.1'
33
end

spec/angular_xss/erb_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
require 'spec_helper'
2-
31
describe 'Angular XSS prevention in ERB', :type => :view do
4-
52
it_should_behave_like 'engine preventing Angular XSS', :partial => 'test_erb'
6-
73
end
84

95
describe ERB::Util do

spec/angular_xss/escaper_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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

spec/angular_xss/haml_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'spec_helper'
2-
31
describe 'Angular XSS prevention in Haml', :type => :view do
42

53
it_should_behave_like 'engine preventing Angular XSS', :partial => 'test_haml'
Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1-
require 'spec_helper'
2-
31
describe ActionView::OutputBuffer do
42
describe '#<<' do
53
it 'escapes angular brackets' do
64
expect((subject << "{{unsafe}}").to_s).to eq("{{ $root.DOUBLE_LEFT_CURLY_BRACE }}unsafe}}")
75
end
86

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
1012
expect { subject << nil }.to_not raise_error
1113
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
1220

1321
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}}")
1623
end
1724

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
1844
end
1945
end

spec/angular_xss/safe_buffer_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
require 'spec_helper'
2-
31
describe ActiveSupport::SafeBuffer do
42

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
719
end
820

921
end

spec/spec_helper.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,3 @@ def self.env
2929
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
3030

3131
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

0 commit comments

Comments
 (0)