Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
5d7db89a
Commit
5d7db89a
authored
Apr 26, 2017
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
httpserver: Proper HTTP->HTTPS for wildcard sites (fixes #1625)
parent
1bae36ef
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
64 deletions
+105
-64
caddyhttp/httpserver/https.go
caddyhttp/httpserver/https.go
+9
-2
caddyhttp/httpserver/https_test.go
caddyhttp/httpserver/https_test.go
+96
-62
No files found.
caddyhttp/httpserver/https.go
View file @
5d7db89a
...
@@ -146,13 +146,20 @@ func redirPlaintextHost(cfg *SiteConfig) *SiteConfig {
...
@@ -146,13 +146,20 @@ func redirPlaintextHost(cfg *SiteConfig) *SiteConfig {
}
}
redirMiddleware
:=
func
(
next
Handler
)
Handler
{
redirMiddleware
:=
func
(
next
Handler
)
Handler
{
return
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
return
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
// Construct the URL to which to redirect. Note that the Host in a request might
// contain a port, but we just need the hostname; we'll set the port if needed.
toURL
:=
"https://"
toURL
:=
"https://"
requestHost
,
_
,
err
:=
net
.
SplitHostPort
(
r
.
Host
)
if
err
!=
nil
{
requestHost
=
r
.
Host
// Host did not contain a port; great
}
if
redirPort
==
""
{
if
redirPort
==
""
{
toURL
+=
cfg
.
Addr
.
Host
// don't use r.Host as it may have a port included
toURL
+=
requestHost
}
else
{
}
else
{
toURL
+=
net
.
JoinHostPort
(
cfg
.
Addr
.
Host
,
redirPort
)
toURL
+=
net
.
JoinHostPort
(
request
Host
,
redirPort
)
}
}
toURL
+=
r
.
URL
.
RequestURI
()
toURL
+=
r
.
URL
.
RequestURI
()
w
.
Header
()
.
Set
(
"Connection"
,
"close"
)
w
.
Header
()
.
Set
(
"Connection"
,
"close"
)
http
.
Redirect
(
w
,
r
,
toURL
,
http
.
StatusMovedPermanently
)
http
.
Redirect
(
w
,
r
,
toURL
,
http
.
StatusMovedPermanently
)
return
0
,
nil
return
0
,
nil
...
...
caddyhttp/httpserver/https_test.go
View file @
5d7db89a
package
httpserver
package
httpserver
import
(
import
(
"fmt"
"net"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"testing"
"testing"
...
@@ -9,75 +11,107 @@ import (
...
@@ -9,75 +11,107 @@ import (
)
)
func
TestRedirPlaintextHost
(
t
*
testing
.
T
)
{
func
TestRedirPlaintextHost
(
t
*
testing
.
T
)
{
cfg
:=
redirPlaintextHost
(
&
SiteConfig
{
for
i
,
testcase
:=
range
[]
struct
{
Addr
:
Address
{
Host
string
// used for the site config
Port
string
ListenHost
string
RequestHost
string
// if different from Host
}{
{
Host
:
"foohost"
,
},
{
Host
:
"foohost"
,
Port
:
"80"
,
},
{
Host
:
"foohost"
,
Host
:
"foohost"
,
Port
:
"1234"
,
Port
:
"1234"
,
},
},
ListenHost
:
"93.184.216.34"
,
{
TLS
:
new
(
caddytls
.
Config
),
Host
:
"foohost"
,
})
ListenHost
:
"93.184.216.34"
,
},
// Check host and port
{
if
actual
,
expected
:=
cfg
.
Addr
.
Host
,
"foohost"
;
actual
!=
expected
{
Host
:
"foohost"
,
t
.
Errorf
(
"Expected redir config to have host %s but got %s"
,
expected
,
actual
)
Port
:
"1234"
,
}
ListenHost
:
"93.184.216.34"
,
if
actual
,
expected
:=
cfg
.
ListenHost
,
"93.184.216.34"
;
actual
!=
expected
{
},
t
.
Errorf
(
"Expected redir config to have bindhost %s but got %s"
,
expected
,
actual
)
{
}
Host
:
"foohost"
,
if
actual
,
expected
:=
cfg
.
Addr
.
Port
,
"80"
;
actual
!=
expected
{
Port
:
"443"
,
// since this is the default HTTPS port, should not be included in Location value
t
.
Errorf
(
"Expected redir config to have port '%s' but got '%s'"
,
expected
,
actual
)
},
}
{
Host
:
"*.example.com"
,
// Make sure redirect handler is set up properly
RequestHost
:
"foo.example.com"
,
if
cfg
.
middleware
==
nil
||
len
(
cfg
.
middleware
)
!=
1
{
},
t
.
Fatalf
(
"Redir config middleware not set up properly; got: %#v"
,
cfg
.
middleware
)
{
}
Host
:
"*.example.com"
,
Port
:
"1234"
,
RequestHost
:
"foo.example.com:1234"
,
},
}
{
cfg
:=
redirPlaintextHost
(
&
SiteConfig
{
Addr
:
Address
{
Host
:
testcase
.
Host
,
Port
:
testcase
.
Port
,
},
ListenHost
:
testcase
.
ListenHost
,
TLS
:
new
(
caddytls
.
Config
),
})
// Check host and port
if
actual
,
expected
:=
cfg
.
Addr
.
Host
,
testcase
.
Host
;
actual
!=
expected
{
t
.
Errorf
(
"Test %d: Expected redir config to have host %s but got %s"
,
i
,
expected
,
actual
)
}
if
actual
,
expected
:=
cfg
.
ListenHost
,
testcase
.
ListenHost
;
actual
!=
expected
{
t
.
Errorf
(
"Test %d: Expected redir config to have bindhost %s but got %s"
,
i
,
expected
,
actual
)
}
if
actual
,
expected
:=
cfg
.
Addr
.
Port
,
HTTPPort
;
actual
!=
expected
{
t
.
Errorf
(
"Test %d: Expected redir config to have port '%s' but got '%s'"
,
i
,
expected
,
actual
)
}
handler
:=
cfg
.
middleware
[
0
](
nil
)
// Make sure redirect handler is set up properly
if
cfg
.
middleware
==
nil
||
len
(
cfg
.
middleware
)
!=
1
{
t
.
Fatalf
(
"Test %d: Redir config middleware not set up properly; got: %#v"
,
i
,
cfg
.
middleware
)
}
// Check redirect for correctness
handler
:=
cfg
.
middleware
[
0
](
nil
)
rec
:=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"http://foohost/bar?q=1"
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
status
,
err
:=
handler
.
ServeHTTP
(
rec
,
req
)
if
status
!=
0
{
t
.
Errorf
(
"Expected status return to be 0, but was %d"
,
status
)
}
if
err
!=
nil
{
t
.
Errorf
(
"Expected returned error to be nil, but was %v"
,
err
)
}
if
rec
.
Code
!=
http
.
StatusMovedPermanently
{
t
.
Errorf
(
"Expected status %d but got %d"
,
http
.
StatusMovedPermanently
,
rec
.
Code
)
}
if
got
,
want
:=
rec
.
Header
()
.
Get
(
"Location"
),
"https://foohost:1234/bar?q=1"
;
got
!=
want
{
t
.
Errorf
(
"Expected Location: '%s' but got '%s'"
,
want
,
got
)
}
// browsers can infer a default port from scheme, so make sure the port
// Check redirect for correctness, first by inspecting error and status code
// doesn't get added in explicitly for default ports like 443 for https.
requestHost
:=
testcase
.
Host
// hostname of request might be different than in config (e.g. wildcards)
cfg
=
redirPlaintextHost
(
&
SiteConfig
{
Addr
:
Address
{
Host
:
"foohost"
,
Port
:
"443"
},
TLS
:
new
(
caddytls
.
Config
)})
if
testcase
.
RequestHost
!=
""
{
handler
=
cfg
.
middleware
[
0
](
nil
)
requestHost
=
testcase
.
RequestHost
}
rec
:=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"http://"
+
requestHost
+
"/bar?q=1"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: %v"
,
i
,
err
)
}
status
,
err
:=
handler
.
ServeHTTP
(
rec
,
req
)
if
status
!=
0
{
t
.
Errorf
(
"Test %d: Expected status return to be 0, but was %d"
,
i
,
status
)
}
if
err
!=
nil
{
t
.
Errorf
(
"Test %d: Expected returned error to be nil, but was %v"
,
i
,
err
)
}
if
rec
.
Code
!=
http
.
StatusMovedPermanently
{
t
.
Errorf
(
"Test %d: Expected status %d but got %d"
,
http
.
StatusMovedPermanently
,
i
,
rec
.
Code
)
}
rec
=
httptest
.
NewRecorder
()
// Now check the Location value. It should mirror the hostname and port of the request
req
,
err
=
http
.
NewRequest
(
"GET"
,
"http://foohost/bar?q=1"
,
nil
)
// unless the port is redundant, in which case it should be dropped.
if
err
!=
nil
{
locationHost
,
_
,
err
:=
net
.
SplitHostPort
(
requestHost
)
t
.
Fatal
(
err
)
if
err
!=
nil
{
}
locationHost
=
requestHost
status
,
err
=
handler
.
ServeHTTP
(
rec
,
req
)
}
if
status
!=
0
{
expectedLoc
:=
fmt
.
Sprintf
(
"https://%s/bar?q=1"
,
locationHost
)
t
.
Errorf
(
"Expected status return to be 0, but was %d"
,
status
)
if
testcase
.
Port
!=
""
&&
testcase
.
Port
!=
DefaultHTTPSPort
{
}
expectedLoc
=
fmt
.
Sprintf
(
"https://%s:%s/bar?q=1"
,
locationHost
,
testcase
.
Port
)
if
err
!=
nil
{
}
t
.
Errorf
(
"Expected returned error to be nil, but was %v"
,
err
)
if
got
,
want
:=
rec
.
Header
()
.
Get
(
"Location"
),
expectedLoc
;
got
!=
want
{
}
t
.
Errorf
(
"Test %d: Expected Location: '%s' but got '%s'"
,
i
,
want
,
got
)
if
rec
.
Code
!=
http
.
StatusMovedPermanently
{
}
t
.
Errorf
(
"Expected status %d but got %d"
,
http
.
StatusMovedPermanently
,
rec
.
Code
)
}
if
got
,
want
:=
rec
.
Header
()
.
Get
(
"Location"
),
"https://foohost/bar?q=1"
;
got
!=
want
{
t
.
Errorf
(
"Expected Location: '%s' but got '%s'"
,
want
,
got
)
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment