Merge pull request #1303 from betagouv/fix-1302

[Fix #1302] sanitize_url can deal with nil values
This commit is contained in:
gregoirenovel 2018-01-18 17:39:12 +01:00 committed by GitHub
commit bb090179b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View file

@ -2,7 +2,9 @@ module ApplicationHelper
include SanitizeUrl
def sanitize_url(url)
super(url, schemes: ['http', 'https'], replace_evil_with: root_url)
if !url.nil?
super(url, schemes: ['http', 'https'], replace_evil_with: root_url)
end
end
def flash_class(level)

View file

@ -0,0 +1,20 @@
describe ApplicationHelper do
describe "#sanitize_url" do
subject { sanitize_url(url) }
describe 'does nothing on clean url' do
let(:url) { "https://tps.fr/toto" }
it { is_expected.to eq(url) }
end
describe 'clean a dangerous url' do
let(:url) { "javascript:alert('coucou jtai hacké')" }
it { is_expected.to eq(root_url) }
end
describe 'can deal with a nil url' do
let(:url) { nil }
it { is_expected.to be_nil }
end
end
end