1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe InternalRedirect do
let(:controller_class) do
Class.new do
include InternalRedirect
def request
@request ||= Struct.new(:host, :port).new('test.host', 80)
end
end
end
subject(:controller) { controller_class.new }
describe '#safe_redirect_path' do
where(:input) do
[
'Hello world',
'//example.com/hello/world',
'https://example.com/hello/world',
"not-starting-with-a-slash\n/starting/with/slash"
]
end
with_them 'being invalid' do
it 'returns nil' do
expect(controller.safe_redirect_path(input)).to be_nil
end
end
where(:input) do
[
'/hello/world',
'/-/ide/project/path'
]
end
with_them 'being valid' do
it 'returns the path' do
expect(controller.safe_redirect_path(input)).to eq(input)
end
it 'returns the path with querystring and fragment' do
expect(controller.safe_redirect_path("#{input}?hello=world#L123"))
.to eq("#{input}?hello=world#L123")
end
end
end
describe '#safe_redirect_path_for_url' do
where(:input) do
[
'Hello world',
'http://example.com/hello/world',
'http://test.host:3000/hello/world'
]
end
with_them 'being invalid' do
it 'returns nil' do
expect(controller.safe_redirect_path_for_url(input)).to be_nil
end
end
where(:input) do
[
'http://test.host/hello/world'
]
end
with_them 'being on the same host' do
let(:path) { URI(input).path }
it 'returns the path' do
expect(controller.safe_redirect_path_for_url(input)).to eq(path)
end
it 'returns the path with querystring and fragment' do
expect(controller.safe_redirect_path_for_url("#{input}?hello=world#L123"))
.to eq("#{path}?hello=world#L123")
end
end
end
describe '#sanitize_redirect' do
let(:valid_path) { '/hello/world?hello=world' }
let(:valid_url) { "http://test.host#{valid_path}" }
it 'returns `nil` for invalid paths' do
invalid_path = '//not/valid'
expect(controller.sanitize_redirect(invalid_path)).to eq nil
end
it 'returns `nil` for invalid urls' do
input = 'http://test.host:3000/invalid'
expect(controller.sanitize_redirect(input)).to eq nil
end
it 'returns input for valid paths' do
expect(controller.sanitize_redirect(valid_path)).to eq valid_path
end
it 'returns path for valid urls' do
expect(controller.sanitize_redirect(valid_url)).to eq valid_path
end
end
describe '#host_allowed?' do
it 'allows URI with the same host and port' do
expect(controller.host_allowed?(URI('http://test.host/test'))).to be(true)
end
it 'rejects URI with other host' do
expect(controller.host_allowed?(URI('http://example.com/test'))).to be(false)
end
it 'rejects URI with other port' do
expect(controller.host_allowed?(URI('http://test.host:3000/test'))).to be(false)
end
end
end
|