From 42523ae9d9dae6a1d231cad0e448aeff2c2fbbab Mon Sep 17 00:00:00 2001 From: Florian Michel <52607335+flo-mic@users.noreply.github.com> Date: Mon, 31 Jan 2022 06:40:58 +0100 Subject: [PATCH] Adds "Host" and "X-Forwarded-For" header options (#61) --- CHANGELOG.md | 1 + error-pages.yml | 4 ++++ internal/http/core/errorpage.go | 2 ++ internal/http/core/headers.go | 6 ++++++ internal/tpl/properties.go | 2 ++ internal/tpl/properties_test.go | 4 ++++ templates/cats.html | 8 ++++++++ templates/ghost.html | 8 ++++++++ templates/hacker-terminal.html | 2 ++ templates/l7-dark.html | 8 ++++++++ templates/l7-light.html | 8 ++++++++ templates/shuffle.html | 12 ++++++++++++ test/hurl/code_502_xml.hurl | 2 ++ 13 files changed, 67 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aaf4fa0..faa81de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher ### UNRELEASED - Go updated from `1.17.5` up to `1.17.6` +- Add "Host" and "X-Forwarded-For" Header to error pages ## v2.4.0 diff --git a/error-pages.yml b/error-pages.yml index 35d2047..08fdd80 100644 --- a/error-pages.yml +++ b/error-pages.yml @@ -21,7 +21,9 @@ formats: "message": {{ message | json }}, "description": {{ description | json }}{{ if show_details }}, "details": { + "host": {{ host | json }}, "original_uri": {{ original_uri | json }}, + "forwarded_for": {{ forwarded_for | json }}, "namespace": {{ namespace | json }}, "ingress_name": {{ ingress_name | json }}, "service_name": {{ service_name | json }}, @@ -39,7 +41,9 @@ formats: {{ message }} {{ description }}{{ if show_details }}
+ {{ host }} {{ original_uri }} + {{ forwarded_for }} {{ namespace }} {{ ingress_name }} {{ service_name }} diff --git a/internal/http/core/errorpage.go b/internal/http/core/errorpage.go index 851df0a..c118668 100644 --- a/internal/http/core/errorpage.go +++ b/internal/http/core/errorpage.go @@ -37,6 +37,8 @@ func RespondWithErrorPage( //nolint:funlen props.ServiceName = string(ctx.Request.Header.Peek(ServiceName)) props.ServicePort = string(ctx.Request.Header.Peek(ServicePort)) props.RequestID = string(ctx.Request.Header.Peek(RequestID)) + props.ForwardedFor = string(ctx.Request.Header.Peek(ForwardedFor)) + props.Host = string(ctx.Request.Header.Peek(Host)) } if page, exists := cfg.Pages[pageCode]; exists { diff --git a/internal/http/core/headers.go b/internal/http/core/headers.go index bbd9f64..18ee605 100644 --- a/internal/http/core/headers.go +++ b/internal/http/core/headers.go @@ -24,4 +24,10 @@ const ( // RequestID is a unique ID that identifies the request - same as for backend service RequestID = "X-Request-ID" + + // Forward-For identifies the user of this session + ForwardedFor = "X-Forwarded-For" + + // Host identifies the hosts origin + Host = "Host" ) diff --git a/internal/tpl/properties.go b/internal/tpl/properties.go index 69b098a..75b0f30 100644 --- a/internal/tpl/properties.go +++ b/internal/tpl/properties.go @@ -12,6 +12,8 @@ type Properties struct { // only string properties with a "token" tag, please ServiceName string `token:"service_name"` ServicePort string `token:"service_port"` RequestID string `token:"request_id"` + ForwardedFor string `token:"forwarded_for"` + Host string `token:"host"` ShowRequestDetails bool } diff --git a/internal/tpl/properties_test.go b/internal/tpl/properties_test.go index d2d1e2a..6be28be 100644 --- a/internal/tpl/properties_test.go +++ b/internal/tpl/properties_test.go @@ -18,6 +18,8 @@ func TestProperties_Replaces(t *testing.T) { ServiceName: "ddd", ServicePort: "eee", RequestID: "fff", + ForwardedFor:"ggg", + Host: "hhh", } r := props.Replaces() @@ -31,6 +33,8 @@ func TestProperties_Replaces(t *testing.T) { assert.Equal(t, "ddd", r["service_name"]) assert.Equal(t, "eee", r["service_port"]) assert.Equal(t, "fff", r["request_id"]) + assert.Equal(t, "ggg", r["forwarded_for"]) + assert.Equal(t, "hhh", r["host"]) props.Code, props.Message, props.Description = "", "", "" diff --git a/templates/cats.html b/templates/cats.html index b1dd80c..a08cf73 100644 --- a/templates/cats.html +++ b/templates/cats.html @@ -62,10 +62,18 @@ {{ if show_details }}
+ {{- if host }} + + + {{ end -}} {{- if original_uri }}{{ end -}} + {{- if forwarded_for }} + + + {{ end -}} {{- if namespace }} diff --git a/templates/ghost.html b/templates/ghost.html index d9482a5..920c298 100644 --- a/templates/ghost.html +++ b/templates/ghost.html @@ -55,10 +55,18 @@ {{ if show_details }}
Host{{ host }}
Original URI {{ original_uri }}
Forwarded for{{ forwarded_for }}
Namespace {{ namespace }}
+ {{- if host }} + + + {{ end -}} {{- if original_uri }}{{ end -}} + {{- if forwarded_for }} + + + {{ end -}} {{- if namespace }} diff --git a/templates/hacker-terminal.html b/templates/hacker-terminal.html index 11df5ce..828467d 100644 --- a/templates/hacker-terminal.html +++ b/templates/hacker-terminal.html @@ -150,7 +150,9 @@

Good luck.

{{ if show_details }}
+ {{- if host }}

Host: {{ host }}

{{ end -}} {{- if original_uri }}

Original URI: {{ original_uri }}

{{ end -}} + {{- if forwarded_for }}

Forwarded for: {{ forwarded_for }}

{{ end -}} {{- if namespace }}

Namespace: {{ namespace }}

{{ end -}} {{- if ingress_name }}

Ingress name: {{ ingress_name }}

{{ end -}} {{- if service_name }}

Service name: {{ service_name }}

{{ end -}} diff --git a/templates/l7-dark.html b/templates/l7-dark.html index 846cd2a..68b94ed 100644 --- a/templates/l7-dark.html +++ b/templates/l7-dark.html @@ -40,10 +40,18 @@ {{ if show_details }}
Host{{ host }}
Original URI {{ original_uri }}
Forwarded for{{ forwarded_for }}
Namespace {{ namespace }}
+ {{- if host }} + + + {{ end -}} {{- if original_uri }}{{ end -}} + {{- if forwarded_for }} + + + {{ end -}} {{- if namespace }} diff --git a/templates/l7-light.html b/templates/l7-light.html index 56a4678..60e1310 100644 --- a/templates/l7-light.html +++ b/templates/l7-light.html @@ -40,10 +40,18 @@ {{ if show_details }}
Host{{ host }}
Original URI {{ original_uri }}
Forwarded for{{ forwarded_for }}
Namespace {{ namespace }}
+ {{- if host }} + + + {{ end -}} {{- if original_uri }}{{ end -}} + {{- if forwarded_for }} + + + {{ end -}} {{- if namespace }} diff --git a/templates/shuffle.html b/templates/shuffle.html index b703a37..5c64a28 100644 --- a/templates/shuffle.html +++ b/templates/shuffle.html @@ -76,12 +76,24 @@ {{ if show_details }}
Host{{ host }}
Original URI {{ original_uri }}
Forwarded for{{ forwarded_for }}
Namespace {{ namespace }}
+ {{- if host }} + + + + + {{ end -}} {{- if original_uri }} {{ end -}} + {{- if forwarded_for }} + + + + + {{ end -}} {{- if namespace }} diff --git a/test/hurl/code_502_xml.hurl b/test/hurl/code_502_xml.hurl index d9cec69..943e92b 100644 --- a/test/hurl/code_502_xml.hurl +++ b/test/hurl/code_502_xml.hurl @@ -22,6 +22,8 @@ xpath "string(//error/details/ingressName)" == "baz" xpath "string(//error/details/serviceName)" == "aaa" xpath "string(//error/details/servicePort)" == "bbb" xpath "string(//error/details/requestID)" == "ccc" +xpath "string(//error/details/forwardedFor)" == "ddd" +xpath "string(//error/details/host)" == "fff" xpath "string(//error/details/timestamp)" exists # X-Format in the action
Host:{{ host }}
Original URI: {{ original_uri }}
Forwarded for:{{ forwarded_for }}
Namespace: