Skip to content

Commit 96e8bfe

Browse files
committed
replace request.env['HTTP_HOST'] with request.host
request.env['HTTP_HOST'] doesn't respect X-Forwarded-Host, whereas request.host does
1 parent 58f06ca commit 96e8bfe

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#### Fixes
66

7+
* [#389](https://github.com/ruby-grape/grape-swagger/pull/389): respect X-Forwarded-Host - [@edvakf](https://github.com/edvakf).
8+
79
### 0.20.1 / 2016-04-17
810

911
#### Features

lib/grape-swagger/endpoint.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def swagger_object(target_class, request, options)
2424
swagger: '2.0',
2525
produces: content_types_for(target_class),
2626
authorizations: options[:authorizations],
27-
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request.env['HTTP_HOST']),
27+
host: GrapeSwagger::DocMethods::OptionalObject.build(:host, options, request.host),
2828
basePath: GrapeSwagger::DocMethods::OptionalObject.build(:base_path, options, request.env['SCRIPT_NAME']),
2929
tags: GrapeSwagger::DocMethods::TagNameDescription.build(options),
3030
schemes: options[:schemes].is_a?(String) ? [options[:schemes]] : options[:schemes]

spec/swagger_v2/x_forwarded_host.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
describe 'respect X-Forwarded-Host over Host header' do
4+
include_context "the api entities"
5+
6+
before :all do
7+
module TheApi
8+
class EmptyApi < Grape::API
9+
format :json
10+
11+
add_swagger_documentation
12+
end
13+
end
14+
end
15+
16+
def app
17+
TheApi::EmptyApi
18+
end
19+
20+
subject do
21+
header 'Host', 'dummy.example.com'
22+
header 'X-Forwarded-Host', 'real.example.com'
23+
get '/swagger_doc'
24+
JSON.parse(last_response.body)
25+
end
26+
27+
specify do
28+
expect(subject['host']).to eq 'real.example.com'
29+
end
30+
end

0 commit comments

Comments
 (0)