2022-07-31 14:56:44 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-05 13:32:44 +01:00
|
|
|
RSpec.describe Rack::Protection::XSSHeader do
|
2022-07-31 14:56:44 +02:00
|
|
|
it_behaves_like 'any rack application'
|
2011-05-24 11:04:49 +02:00
|
|
|
|
|
|
|
it 'should set the X-XSS-Protection' do
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/', {}, 'wants' => 'text/html;charset=utf-8').headers['X-XSS-Protection']).to eq('1; mode=block')
|
2012-12-10 16:42:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the X-XSS-Protection for XHTML' do
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/', {}, 'wants' => 'application/xhtml+xml').headers['X-XSS-Protection']).to eq('1; mode=block')
|
2012-12-10 16:42:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not set the X-XSS-Protection for other content types' do
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/', {}, 'wants' => 'application/foo').headers['X-XSS-Protection']).to be_nil
|
2011-05-24 11:04:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow changing the protection mode' do
|
|
|
|
# I have no clue what other modes are available
|
|
|
|
mock_app do
|
2022-07-31 14:56:44 +02:00
|
|
|
use Rack::Protection::XSSHeader, xss_mode: :foo
|
2011-05-24 11:04:49 +02:00
|
|
|
run DummyApp
|
|
|
|
end
|
|
|
|
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/', {}, 'wants' => 'application/xhtml').headers['X-XSS-Protection']).to eq('1; mode=foo')
|
2011-05-24 11:04:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not override the header if already set' do
|
2022-07-31 14:56:44 +02:00
|
|
|
mock_app with_headers('X-XSS-Protection' => '0')
|
|
|
|
expect(get('/', {}, 'wants' => 'text/html').headers['X-XSS-Protection']).to eq('0')
|
2011-05-24 11:04:49 +02:00
|
|
|
end
|
2012-06-28 20:33:09 +09:00
|
|
|
|
|
|
|
it 'should set the X-Content-Type-Options' do
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/', {}, 'wants' => 'text/html').header['X-Content-Type-Options']).to eq('nosniff')
|
2012-06-28 20:33:09 +09:00
|
|
|
end
|
|
|
|
|
2013-03-01 15:36:05 +11:00
|
|
|
it 'should set the X-Content-Type-Options for other content types' do
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/', {}, 'wants' => 'application/foo').header['X-Content-Type-Options']).to eq('nosniff')
|
2013-03-01 15:36:05 +11:00
|
|
|
end
|
|
|
|
|
2012-06-28 20:33:09 +09:00
|
|
|
it 'should allow changing the nosniff-mode off' do
|
|
|
|
mock_app do
|
2022-07-31 14:56:44 +02:00
|
|
|
use Rack::Protection::XSSHeader, nosniff: false
|
2012-06-28 20:33:09 +09:00
|
|
|
run DummyApp
|
|
|
|
end
|
|
|
|
|
2022-07-31 14:56:44 +02:00
|
|
|
expect(get('/').headers['X-Content-Type-Options']).to be_nil
|
2012-06-28 20:33:09 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not override the header if already set X-Content-Type-Options' do
|
2022-07-31 14:56:44 +02:00
|
|
|
mock_app with_headers('X-Content-Type-Options' => 'sniff')
|
|
|
|
expect(get('/', {}, 'wants' => 'text/html').headers['X-Content-Type-Options']).to eq('sniff')
|
2012-06-28 20:33:09 +09:00
|
|
|
end
|
2011-05-23 17:36:16 +02:00
|
|
|
end
|