From 3ad5128cb9b4ac5209b8b6fdac7cbd02a8e6f69d Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Wed, 8 Aug 2018 10:47:20 +0530 Subject: [PATCH] config.go: trim trailing slash in ORIGIN and CDN_PREFIX Fixes https://gitlab.com/commento/commento-ce/issues/59 --- api/config.go | 4 ++++ api/config_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/api/config.go b/api/config.go index 2f8850e..7fa518e 100644 --- a/api/config.go +++ b/api/config.go @@ -62,10 +62,14 @@ func configParse() error { } } + os.Setenv("ORIGIN", strings.TrimSuffix(os.Getenv("ORIGIN"), "/")) + if os.Getenv("CDN_PREFIX") == "" { os.Setenv("CDN_PREFIX", os.Getenv("ORIGIN")) } + os.Setenv("CDN_PREFIX", strings.TrimSuffix(os.Getenv("ORIGIN"), "/")) + if os.Getenv("FORBID_NEW_OWNERS") != "true" && os.Getenv("FORBID_NEW_OWNERS") != "false" { logger.Errorf("COMMENTO_FORBID_NEW_OWNERS neither 'true' nor 'false'") return errorInvalidConfigValue diff --git a/api/config_test.go b/api/config_test.go index 76291bc..605f465 100644 --- a/api/config_test.go +++ b/api/config_test.go @@ -110,3 +110,18 @@ func TestConfigParseStaticNotADirectory(t *testing.T) { return } } + +func TestConfigOriginTrailingSlash(t *testing.T) { + os.Setenv("COMMENTO_ORIGIN", "https://commento.io/") + os.Setenv("COMMENTO_STATIC", "") + + if err := configParse(); err != nil { + t.Errorf("unexpected error when parsing config: %v", err) + return + } + + if os.Getenv("ORIGIN") != "https://commento.io" { + t.Errorf("expected ORIGIN=https://commento.io got ORIGIN=%s", os.Getenv("ORIGIN")) + return + } +}