Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hostname handling fixes #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions spnego.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ func canonicalizeHostname(hostname string) (string, error) {

return strings.TrimRight(names[0], "."), nil
}

func getHostname(req *http.Request, canonicalize bool) (string, error) {
var err error

h := req.URL.Host // SPN should contain the port, if non-standard (https://social.technet.microsoft.com/wiki/contents/articles/717.service-principal-names-spn-setspn-syntax.aspx)
if req.Host != "" {
h = req.Host
}
// I can't think why you'd want to canonicalize an overridden Host
// header (you're likely to get the hostname in the URL, which is what
// you're overriding in the first place). But since it can be disabled,
// leave it here in the flow so that Host header hostnames can be
// canonicalized if desired.
if canonicalize {
if h, err = canonicalizeHostname(h); err != nil {
return "", err
}
}

return h, nil
}
9 changes: 3 additions & 6 deletions spnego_gokrb5.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ func (k *krb5) makeClient() error {
}

func (k *krb5) SetSPNEGOHeader(req *http.Request, canonicalize bool) error {
h := req.URL.Hostname()
if canonicalize {
var err error
if h, err = canonicalizeHostname(h); err != nil {
return err
}
h, err := getHostname(req, canonicalize)
if err != nil {
return err
}

if err := k.makeCfg(); err != nil {
Expand Down
129 changes: 129 additions & 0 deletions spnego_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package spnego

import (
"net/http"
"net/url"
"testing"
)

func TestGetHostnameEmptyHost(t *testing.T) {
desired := "example.com"

url, err := url.Parse("http://example.com/path")
if err != nil {
panic(err)
}
req := &http.Request{
URL: url,
}
t.Logf("url %s, host %s\n", req.URL.Host, req.Host)

h, err := getHostname(req, false)
if err != nil {
panic(err)
}

if h != desired {
t.Errorf("Hostname was incorrect: wanted %s, got %s.", desired, h)
}
}

func TestGetHostnameEmptyHostPort(t *testing.T) {
desired := "example.com:8080"

url, err := url.Parse("http://example.com:8080/path")
if err != nil {
panic(err)
}
req := &http.Request{
URL: url,
}
t.Logf("url %s, host %s\n", req.URL.Host, req.Host)

h, err := getHostname(req, false)
if err != nil {
panic(err)
}

if h != desired {
t.Errorf("Hostname was incorrect: wanted %s, got %s.", desired, h)
}
}

func TestGetHostnameHost(t *testing.T) {
desired := "example.com"

req, err := http.NewRequest("GET", "http://example.com/path", nil)
if err != nil {
panic(err)
}
t.Logf("url %s, host %s\n", req.URL.Host, req.Host)

h, err := getHostname(req, false)
if err != nil {
panic(err)
}

if h != desired {
t.Errorf("Hostname was incorrect: wanted %s, got %s.", desired, h)
}
}

func TestGetHostnameHostWithPort(t *testing.T) {
desired := "example.com:8080"

req, err := http.NewRequest("GET", "http://example.com:8080/path", nil)
if err != nil {
panic(err)
}
t.Logf("url %s, host %s\n", req.URL.Host, req.Host)

h, err := getHostname(req, false)
if err != nil {
panic(err)
}

if h != desired {
t.Errorf("Hostname was incorrect: wanted %s, got %s.", desired, h)
}
}

func TestGetHostnameOverride(t *testing.T) {
desired := "override.org"

req, err := http.NewRequest("GET", "http://example.com:8080/path", nil)
if err != nil {
panic(err)
}
req.Host = "override.org"
t.Logf("url %s, host %s\n", req.URL.Host, req.Host)

h, err := getHostname(req, false)
if err != nil {
panic(err)
}

if h != desired {
t.Errorf("Hostname was incorrect: wanted %s, got %s.", desired, h)
}
}

func TestGetHostnameOverrideWithPort(t *testing.T) {
desired := "override.org:1234"

req, err := http.NewRequest("GET", "http://example.com:8080/path", nil)
if err != nil {
panic(err)
}
req.Host = "override.org:1234"
t.Logf("url %s, host %s\n", req.URL.Host, req.Host)

h, err := getHostname(req, false)
if err != nil {
panic(err)
}

if h != desired {
t.Errorf("Hostname was incorrect: wanted %s, got %s.", desired, h)
}
}
9 changes: 3 additions & 6 deletions spnego_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ func New() Provider {

// SetSPNEGOHeader puts the SPNEGO authorization header on HTTP request object
func (s *sspi) SetSPNEGOHeader(req *http.Request, canonicalize bool) error {
h := req.URL.Hostname()
if canonicalize {
var err error
if h, err = canonicalizeHostname(h); err != nil {
return err
}
h, err := getHostname(req, canonicalize)
if err != nil {
return err
}
spn := "HTTP/" + h

Expand Down