From 42c4e7bf8484c0811ee52069f611e003a80963ad Mon Sep 17 00:00:00 2001 From: Paramtamtam <7326800+tarampampam@users.noreply.github.com> Date: Thu, 27 Jun 2024 19:29:54 +0400 Subject: [PATCH] =?UTF-8?q?wip:=20=F0=9F=94=95=20temporary=20commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cli/serve/command.go | 6 +- internal/template/template.go | 5 + internal/template/template_test.go | 4 + templates/app-down.html | 12 +- templates/cats.html | 12 +- templates/connection.html | 12 +- templates/ghost.html | 12 +- templates/hacker-terminal.html | 12 +- templates/l7.html | 12 +- templates/lost-in-space.html | 455 +++++++++++++++++++++++++++++ 10 files changed, 505 insertions(+), 37 deletions(-) create mode 100644 templates/lost-in-space.html diff --git a/internal/cli/serve/command.go b/internal/cli/serve/command.go index 899f013..e4562e5 100644 --- a/internal/cli/serve/command.go +++ b/internal/cli/serve/command.go @@ -243,7 +243,11 @@ func NewCommand(log *logger.Logger) *cli.Command { //nolint:funlen,gocognit,gocy cfg.TemplateName = c.String(templateNameFlag.Name) if !cfg.Templates.Has(cfg.TemplateName) { - return fmt.Errorf("template %s not found and cannot be used", cfg.TemplateName) + return fmt.Errorf( + "template '%s' not found and cannot be used (available templates: %s)", + cfg.TemplateName, + cfg.Templates.Names(), + ) } } diff --git a/internal/template/template.go b/internal/template/template.go index 090a36a..b763dd4 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -3,6 +3,7 @@ package template import ( "encoding/json" "fmt" + "html" "maps" "os" "strconv" @@ -101,6 +102,10 @@ var builtInFunctions = template.FuncMap{ //nolint:gochecknoglobals // `{{ env "SHELL" }}` // `/bin/bash` "env": os.Getenv, + // escapes special characters like "<" to become "<": + // `{{ escape "" }}` // `<test>` + "escape": html.EscapeString, + // returns the content of the JS file with a script for automatic error page localization: // `{{ l10nScript }}` // `Object.defineProperty(window, ...` "l10nScript": l10n.L10n, diff --git a/internal/template/template_test.go b/internal/template/template_test.go index a8a37de..073552f 100644 --- a/internal/template/template_test.go +++ b/internal/template/template_test.go @@ -52,6 +52,10 @@ func TestRender_BuiltInFunction(t *testing.T) { "env (ok)": {giveTemplate: `{{ env "TEST_ENV_VAR" }}`, wantResult: "unit-test"}, "env (not found)": {giveTemplate: `{{ env "NOT_FOUND_ENV_VAR" }}`, wantResult: ""}, "l10nScript": {giveTemplate: `{{ l10nScript }}`, wantResult: l10n.L10n()}, + "escape": { + giveTemplate: `{{ escape "" }}`, + wantResult: "<script>alert('XSS' + "HERE")</script>", + }, } { t.Run(name, func(t *testing.T) { require.NoError(t, os.Setenv("TEST_ENV_VAR", "unit-test")) diff --git a/templates/app-down.html b/templates/app-down.html index a2f21f9..b2a7f87 100644 --- a/templates/app-down.html +++ b/templates/app-down.html @@ -8,12 +8,12 @@ - - - - - - + + + + + + + + + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+

{{code}}

+

UH OH! {{ message }}

+

{{ description }}

+ + +
    + +
  • Host: {{ host }}
  • + +
  • Original URI: {{ original_uri }}
  • + +
  • Forwarded for: {{ forwarded_for }}
  • + +
  • Namespace: {{ namespace }}
  • + +
  • Ingress name: {{ ingress_name }}
  • + +
  • Service name: {{ service_name }}
  • + +
  • Service port: {{ service_port }}
  • + +
  • Request ID: {{ request_id }}
  • + +
  • Timestamp: {{ now.Unix }}
  • +
+ +
+
+ + + + + +