unicorn 4.9.0 → 5.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,42 +0,0 @@
1
- # -*- encoding: binary -*-
2
- # :stopdoc:
3
- # this module is meant to be included in Unicorn::HttpServer
4
- # It is an implementation detail and NOT meant for users.
5
- module Unicorn::SSLServer
6
- attr_accessor :ssl_engine
7
-
8
- def ssl_enable!
9
- sni_hostnames = rack_sni_hostnames(@app)
10
- seen = {} # we map a single SSLContext to multiple listeners
11
- listener_ctx = {}
12
- @listener_opts.each do |address, address_opts|
13
- ssl_opts = address_opts[:ssl_opts] or next
14
- listener_ctx[address] = seen[ssl_opts.object_id] ||= begin
15
- unless sni_hostnames.empty?
16
- ssl_opts = ssl_opts.dup
17
- ssl_opts[:sni_hostnames] = sni_hostnames
18
- end
19
- ctx = Flipper.ssl_context(ssl_opts)
20
- # FIXME: make configurable
21
- ctx.session_cache_mode = OpenSSL::SSL::SSLContext::SESSION_CACHE_OFF
22
- ctx
23
- end
24
- end
25
- Unicorn::HttpServer::LISTENERS.each do |listener|
26
- ctx = listener_ctx[sock_name(listener)] or next
27
- listener.extend(Kgio::SSLServer)
28
- listener.ssl_ctx = ctx
29
- listener.kgio_ssl_class = Unicorn::SSLClient
30
- end
31
- end
32
-
33
- # ugh, this depends on Rack internals...
34
- def rack_sni_hostnames(rack_app) # :nodoc:
35
- hostnames = {}
36
- if Rack::URLMap === rack_app
37
- mapping = rack_app.instance_variable_get(:@mapping)
38
- mapping.each { |hostname,_,_,_| hostnames[hostname] = true }
39
- end
40
- hostnames.keys
41
- end
42
- end
@@ -1,30 +0,0 @@
1
- #!/bin/sh
2
- . ./test-lib.sh
3
- t_plan 5 "trust_x_forwarded=false configuration test"
4
-
5
- t_begin "setup and start" && {
6
- unicorn_setup
7
- echo "trust_x_forwarded false" >> $unicorn_config
8
- unicorn -D -c $unicorn_config env.ru
9
- unicorn_wait_start
10
- }
11
-
12
- t_begin "spoofed request with X-Forwarded-Proto does not trigger" && {
13
- curl -H 'X-Forwarded-Proto: https' http://$listen/ | \
14
- grep -F '"rack.url_scheme"=>"http"'
15
- }
16
-
17
- t_begin "spoofed request with X-Forwarded-SSL does not trigger" && {
18
- curl -H 'X-Forwarded-SSL: on' http://$listen/ | \
19
- grep -F '"rack.url_scheme"=>"http"'
20
- }
21
-
22
- t_begin "killing succeeds" && {
23
- kill $unicorn_pid
24
- }
25
-
26
- t_begin "check stderr has no errors" && {
27
- check_stderr
28
- }
29
-
30
- t_done
@@ -1,30 +0,0 @@
1
- #!/bin/sh
2
- . ./test-lib.sh
3
- t_plan 5 "trust_x_forwarded=true configuration test"
4
-
5
- t_begin "setup and start" && {
6
- unicorn_setup
7
- echo "trust_x_forwarded true " >> $unicorn_config
8
- unicorn -D -c $unicorn_config env.ru
9
- unicorn_wait_start
10
- }
11
-
12
- t_begin "spoofed request with X-Forwarded-Proto sets 'https'" && {
13
- curl -H 'X-Forwarded-Proto: https' http://$listen/ | \
14
- grep -F '"rack.url_scheme"=>"https"'
15
- }
16
-
17
- t_begin "spoofed request with X-Forwarded-SSL sets 'https'" && {
18
- curl -H 'X-Forwarded-SSL: on' http://$listen/ | \
19
- grep -F '"rack.url_scheme"=>"https"'
20
- }
21
-
22
- t_begin "killing succeeds" && {
23
- kill $unicorn_pid
24
- }
25
-
26
- t_begin "check stderr has no errors" && {
27
- check_stderr
28
- }
29
-
30
- t_done
@@ -1,38 +0,0 @@
1
- # -*- encoding: binary -*-
2
- require './test/test_helper'
3
-
4
- include Unicorn
5
-
6
- class HttpParserXFTrustTest < Test::Unit::TestCase
7
- def setup
8
- assert HttpParser.trust_x_forwarded?
9
- end
10
-
11
- def test_xf_trust_false_xfp
12
- HttpParser.trust_x_forwarded = false
13
- parser = HttpParser.new
14
- parser.buf << "GET / HTTP/1.1\r\nHost: foo:\r\n" \
15
- "X-Forwarded-Proto: https\r\n\r\n"
16
- env = parser.parse
17
- assert_kind_of Hash, env
18
- assert_equal 'foo', env['SERVER_NAME']
19
- assert_equal '80', env['SERVER_PORT']
20
- assert_equal 'http', env['rack.url_scheme']
21
- end
22
-
23
- def test_xf_trust_false_xfs
24
- HttpParser.trust_x_forwarded = false
25
- parser = HttpParser.new
26
- parser.buf << "GET / HTTP/1.1\r\nHost: foo:\r\n" \
27
- "X-Forwarded-SSL: on\r\n\r\n"
28
- env = parser.parse
29
- assert_kind_of Hash, env
30
- assert_equal 'foo', env['SERVER_NAME']
31
- assert_equal '80', env['SERVER_PORT']
32
- assert_equal 'http', env['rack.url_scheme']
33
- end
34
-
35
- def teardown
36
- HttpParser.trust_x_forwarded = true
37
- end
38
- end
@@ -1,47 +0,0 @@
1
- # -*- encoding: binary -*-
2
- require "test/unit"
3
- require "unicorn"
4
-
5
- # this tests an implementation detail, it may change so this test
6
- # can be removed later.
7
- class TestSniHostnames < Test::Unit::TestCase
8
- include Unicorn::SSLServer
9
-
10
- def setup
11
- GC.start
12
- end
13
-
14
- def teardown
15
- GC.start
16
- end
17
-
18
- def test_host_name_detect_one
19
- app = Rack::Builder.new do
20
- map "http://sni1.example.com/" do
21
- use Rack::ContentLength
22
- use Rack::ContentType, "text/plain"
23
- run lambda { |env| [ 200, {}, [] ] }
24
- end
25
- end.to_app
26
- hostnames = rack_sni_hostnames(app)
27
- assert hostnames.include?("sni1.example.com")
28
- end
29
-
30
- def test_host_name_detect_multiple
31
- app = Rack::Builder.new do
32
- map "http://sni2.example.com/" do
33
- use Rack::ContentLength
34
- use Rack::ContentType, "text/plain"
35
- run lambda { |env| [ 200, {}, [] ] }
36
- end
37
- map "http://sni3.example.com/" do
38
- use Rack::ContentLength
39
- use Rack::ContentType, "text/plain"
40
- run lambda { |env| [ 200, {}, [] ] }
41
- end
42
- end.to_app
43
- hostnames = rack_sni_hostnames(app)
44
- assert hostnames.include?("sni2.example.com")
45
- assert hostnames.include?("sni3.example.com")
46
- end
47
- end