Skip to content

Commit c4e6658

Browse files
authored
Merge pull request #1 from mattk42/referrer-policy
Feat(router): Add ability to set Referrer-Policy header globally and per application.
2 parents 732555d + 4a95d56 commit c4e6658

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ _Note that Kubernetes annotation maps are all of Go type `map[string]string`. A
273273
| <a name="proxy-buffers-number"></a>deis-router | deployment | [router.deis.io/nginx.proxyBuffers.number](#proxy-buffers-number) | `"8"` | `number` argument to the nginx `proxy_buffers` directive for all applications (this can be overridden on an application basis). |
274274
| <a name="proxy-buffers-size"></a>deis-router | deployment | [router.deis.io/nginx.proxyBuffers.size](#proxy-buffers-size) | `"4k"` | `size` argument to the nginx `proxy_buffers` directive expressed in bytes (no suffix), kilobytes (suffixes `k` and `K`), or megabytes (suffixes `m` and `M`). This setting applies to all applications, but can be overridden on an application basis. |
275275
| <a name="proxy-buffers-busy-size"></a>deis-router | deployment | [router.deis.io/nginx.proxyBuffers.busySize](#proxy-buffers-busy-size) | `"8k"` | nginx `proxy_busy_buffers_size` expressed in bytes (no suffix), kilobytes (suffixes `k` and `K`), or megabytes (suffixes `m` and `M`). This setting applies to all applications, but can be overridden on an application basis. |
276+
| <a neme="referrer-policy"></a>deis-router | deployment | [router.deis.io/nginx.referrerPolicy](#referrer-policy) | `""` | The Referrer-Policy header to send for all apps. |
276277
| <a name="builder-connect-timeout"></a>deis-builder | service | [router.deis.io/nginx.connectTimeout](#builder-connect-timeout) | `"10s"` | nginx `proxy_connect_timeout` setting expressed in units `ms`, `s`, `m`, `h`, `d`, `w`, `M`, or `y`. |
277278
| <a name="builder-tcp-timeout"></a>deis-builder | service | [router.deis.io/nginx.tcpTimeout](#builder-tcp-timeout) | `"1200s"` | nginx `proxy_timeout` setting expressed in units `ms`, `s`, `m`, `h`, `d`, `w`, `M`, or `y`. |
278279
| <a name="app-domains"></a>routable application | service | [router.deis.io/domains](#app-domains) | N/A | Comma-delimited list of domains for which traffic should be routed to the application. These may be fully qualified (e.g. `foo.example.com`) or, if not containing any `.` character, will be considered subdomains of the router's domain, if that is defined. |
@@ -288,6 +289,7 @@ _Note that Kubernetes annotation maps are all of Go type `map[string]string`. A
288289
| <a name="app-nginx-proxy-buffers-number"></a>routable application | service | [router.deis.io/nginx.proxyBuffers.number](#app-nginx-proxy-buffers-number) | `"8"` | `number` argument to the nginx `proxy_buffers` directive. This can be used to override the same option set globally on the router. |
289290
| <a name="app-nginx-proxy-buffers-size"></a>routable application | service | [router.deis.io/nginx.proxyBuffers.size](#app-nginx-proxy-buffers-size) | `"4k"` | `size` argument to the nginx `proxy_buffers` directive expressed in bytes (no suffix), kilobytes (suffixes `k` and `K`), or megabytes (suffixes `m` and `M`). This can be used to override the same option set globally on the router. |
290291
| <a name="app-nginx-proxy-buffers-busy-size"></a>routable application | service | [router.deis.io/nginx.proxyBuffers.busySize](#app-nginx-proxy-buffers-busy-size) | `"8k"` | nginx `proxy_busy_buffers_size` expressed in bytes (no suffix), kilobytes (suffixes `k` and `K`), or megabytes (suffixes `m` and `M`). This can be used to override the same option set globally on the router. |
292+
| <a neme="app-referrer-policy"></a>routable application | service | [router.deis.io/referrerPolicy](#referrer-policy) | `""` | The Referrer-Policy header to send for this specific application. Overrides the global setting if necessary. |
291293
|<a name="app-proxy-locations"></a>routable application | service | [router.deis.io/proxyLocations](#app-proxy-locations) | N/A | A list of locations of this service to plug-in into another service determined by `router.deis.io/proxyDomain` (see example below) |
292294
|<a name="app-proxy-domain"></a>routable application | service | [router.deis.io/proxyDomain](#app-proxy-domain) | N/A | A reference to another service to plug-in `router.deis.io/proxyLocations` to (see example below) |
293295

model/model.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type RouterConfig struct {
7272
HTTP2Enabled bool `key:"http2Enabled" constraint:"(?i)^(true|false)$"`
7373
LogFormat string `key:"logFormat"`
7474
ProxyBuffersConfig *ProxyBuffersConfig `key:"proxyBuffers"`
75+
ReferrerPolicy string `key:"referrerPolicy" constraint:"^(no-referrer|no-referrer-when-downgrade|origin|origin-when-cross-origin|same-origin|strict-origin|strict-origin-when-cross-origin|unsafe-url|none)$"`
7576
}
7677

7778
func newRouterConfig() (*RouterConfig, error) {
@@ -107,6 +108,7 @@ func newRouterConfig() (*RouterConfig, error) {
107108
HTTP2Enabled: true,
108109
LogFormat: `[$time_iso8601] - $app_name - $remote_addr - $remote_user - $status - "$request" - $bytes_sent - "$http_referer" - "$http_user_agent" - "$server_name" - $upstream_addr - $http_host - $upstream_response_time - $request_time`,
109110
ProxyBuffersConfig: proxyBuffersConfig,
111+
ReferrerPolicy: "",
110112
}, nil
111113
}
112114

@@ -149,6 +151,7 @@ type AppConfig struct {
149151
Certificates map[string]*Certificate
150152
Available bool
151153
Maintenance bool `key:"maintenance" constraint:"(?i)^(true|false)$"`
154+
ReferrerPolicy string `key:"referrerPolicy" constraint:"^(no-referrer|no-referrer-when-downgrade|origin|origin-when-cross-origin|same-origin|strict-origin|strict-origin-when-cross-origin|unsafe-url|none)$"`
152155
SSLConfig *SSLConfig `key:"ssl"`
153156
Nginx *NginxAppConfig `key:"nginx"`
154157
ProxyLocations []string `key:"proxyLocations"`

model/model_validation_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ func TestInvalidHTTP2Enabled(t *testing.T) {
159159
testInvalidValues(t, newTestRouterConfig, "HTTP2Enabled", "http2Enabled", []string{"0", "-1", "foobar"})
160160
}
161161

162+
func TestValidReferrerPolicy(t *testing.T) {
163+
testValidValues(t, newTestRouterConfig, "ReferrerPolicy", "referrerPolicy", []string{"no-referrer", "no-referrer-when-downgrade", "origin", "origin-when-cross-origin", "same-origin", "strict-origin", "strict-origin-when-cross-origin", "unsafe-url", "none"})
164+
}
165+
166+
func TestInvalidReferrerPolicy(t *testing.T) {
167+
testInvalidValues(t, newTestRouterConfig, "ReferrerPolicy", "referrerPolicy", []string{"0", "-1", "foobar", ""})
168+
}
169+
162170
func TestInvalidGzipEnabled(t *testing.T) {
163171
testInvalidValues(t, newTestGzipConfig, "Enabled", "enabled", []string{"0", "-1", "foobar"})
164172
}
@@ -255,6 +263,14 @@ func TestValidCertMappings(t *testing.T) {
255263
testValidValues(t, newTestAppConfig, "CertMappings", "certificates", []string{"foobar.com:foobar,*.foobar.deis.ninja:foobar-deis-ninja"})
256264
}
257265

266+
func TestValidAppReferrerPolicy(t *testing.T) {
267+
testValidValues(t, newTestAppConfig, "ReferrerPolicy", "referrerPolicy", []string{"no-referrer", "no-referrer-when-downgrade", "origin", "origin-when-cross-origin", "same-origin", "strict-origin", "strict-origin-when-cross-origin", "unsafe-url", "none"})
268+
}
269+
270+
func TestInvalidAppReferrerPolicy(t *testing.T) {
271+
testInvalidValues(t, newTestAppConfig, "ReferrerPolicy", "referrerPolicy", []string{"0", "-1", "foobar", ""})
272+
}
273+
258274
func TestInvalidBuilderConnectTimeout(t *testing.T) {
259275
testInvalidValues(t, newTestBuilderConfig, "ConnectTimeout", "connectTimeout", []string{"0", "-1", "foobar"})
260276
}

nginx/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ http {
193193
ssl_certificate /opt/router/ssl/default/default.crt;
194194
ssl_certificate_key /opt/router/ssl/default/default.key;
195195
{{ end }}
196+
{{ if ne $routerConfig.ReferrerPolicy "" }}
197+
add_header Referrer-Policy {{ $routerConfig.ReferrerPolicy }};
198+
{{ end }}
196199
server_name _;
197200
location ~ ^/healthz/?$ {
198201
access_log off;
@@ -277,6 +280,9 @@ http {
277280
add_header X-Correlation-Id $correlation_id always;
278281
{{end}}
279282
283+
{{ if (and (ne $appConfig.ReferrerPolicy "") (ne $appConfig.ReferrerPolicy "none")) }}add_header Referrer-Policy {{ $appConfig.ReferrerPolicy }};
284+
{{ else if (and (ne $routerConfig.ReferrerPolicy "") (and (ne $appConfig.ReferrerPolicy "none") (ne $routerConfig.ReferrerPolicy "none"))) }}add_header Referrer-Policy {{ $routerConfig.ReferrerPolicy }};{{ end }}
285+
280286
{{ if $location.App.Maintenance }}return 503;{{ else if $location.App.Available }}
281287
proxy_buffering {{ if $location.App.Nginx.ProxyBuffersConfig.Enabled }}on{{ else }}off{{ end }};
282288
proxy_buffer_size {{ $location.App.Nginx.ProxyBuffersConfig.Size }};

0 commit comments

Comments
 (0)