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

support Private Network Access #127

Merged
merged 1 commit into from Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions cors.go
Expand Up @@ -62,6 +62,9 @@ type Options struct {
// AllowCredentials indicates whether the request can include user credentials like
// cookies, HTTP authentication or client side SSL certificates.
AllowCredentials bool
// AllowPrivateNetwork indicates whether to accept cross-origin requests over a
// private network.
AllowPrivateNetwork bool
// OptionsPassthrough instructs preflight to let other potential next handlers to
// process the OPTIONS method. Turn this on if your application handles OPTIONS.
OptionsPassthrough bool
Expand Down Expand Up @@ -103,6 +106,7 @@ type Cors struct {
// Status code to use for successful OPTIONS requests
optionsSuccessStatus int
allowCredentials bool
allowPrivateNetwork bool
optionPassthrough bool
}

Expand All @@ -113,6 +117,7 @@ func New(options Options) *Cors {
allowOriginFunc: options.AllowOriginFunc,
allowOriginRequestFunc: options.AllowOriginRequestFunc,
allowCredentials: options.AllowCredentials,
allowPrivateNetwork: options.AllowPrivateNetwork,
maxAge: options.MaxAge,
optionPassthrough: options.OptionsPassthrough,
}
Expand Down Expand Up @@ -319,6 +324,9 @@ func (c *Cors) handlePreflight(w http.ResponseWriter, r *http.Request) {
if c.allowCredentials {
headers.Set("Access-Control-Allow-Credentials", "true")
}
if c.allowPrivateNetwork && r.Header.Get("Access-Control-Request-Private-Network") == "true" {
headers.Set("Access-Control-Allow-Private-Network", "true")
}
if c.maxAge > 0 {
headers.Set("Access-Control-Max-Age", strconv.Itoa(c.maxAge))
}
Expand Down
39 changes: 39 additions & 0 deletions cors_test.go
Expand Up @@ -18,6 +18,7 @@ var allHeaders = []string{
"Access-Control-Allow-Methods",
"Access-Control-Allow-Headers",
"Access-Control-Allow-Credentials",
"Access-Control-Allow-Private-Network",
"Access-Control-Max-Age",
"Access-Control-Expose-Headers",
}
Expand Down Expand Up @@ -387,6 +388,44 @@ func TestSpec(t *testing.T) {
},
true,
},
{
"AllowedPrivateNetwork",
Options{
AllowedOrigins: []string{"http://foobar.com"},
AllowPrivateNetwork: true,
},
"OPTIONS",
map[string]string{
"Origin": "http://foobar.com",
"Access-Control-Request-Method": "GET",
"Access-Control-Request-Private-Network": "true",
},
map[string]string{
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Private-Network": "true",
},
true,
},
{
"DisallowedPrivateNetwork",
Options{
AllowedOrigins: []string{"http://foobar.com"},
},
"OPTIONS",
map[string]string{
"Origin": "http://foobar.com",
"Access-Control-Request-Method": "GET",
"Access-Control-Request-PrivateNetwork": "true",
},
map[string]string{
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Allow-Methods": "GET",
},
true,
},
{
"OptionPassthrough",
Options{
Expand Down