From 355a2950d0e40e2ccb106fea99fb00c03280c1ae Mon Sep 17 00:00:00 2001 From: Kassio Borges Date: Sat, 20 Jan 2024 17:15:46 -0300 Subject: Fix project path lookup to work in case insensitive GitLab projects are unique with case insensitive, which means that Pages must lookup projects in a case insensitive manner as well. Related to: https://gitlab.com/gitlab-org/gitlab/-/issues/438699 Changelog: fixed --- internal/source/gitlab/gitlab.go | 9 +++++---- internal/source/gitlab/gitlab_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index d3fbc8cc..2de113f8 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -72,17 +72,18 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) { return nil, response.Error } - urlPath := path.Clean(r.URL.Path) + urlPath := strings.ToLower(path.Clean(r.URL.Path)) size := len(response.Domain.LookupPaths) for _, lookup := range response.Domain.LookupPaths { - isSubPath := strings.HasPrefix(urlPath, lookup.Prefix) - isRootPath := urlPath == path.Clean(lookup.Prefix) + lookupPrefix := strings.ToLower(lookup.Prefix) + isSubPath := strings.HasPrefix(urlPath, lookupPrefix) + isRootPath := urlPath == path.Clean(lookupPrefix) if isSubPath || isRootPath { subPath := "" if isSubPath { - subPath = strings.TrimPrefix(urlPath, lookup.Prefix) + subPath = strings.TrimPrefix(urlPath, lookupPrefix) } srv, err := g.fabricateServing(lookup) diff --git a/internal/source/gitlab/gitlab_test.go b/internal/source/gitlab/gitlab_test.go index 095fc2eb..d99e88e9 100644 --- a/internal/source/gitlab/gitlab_test.go +++ b/internal/source/gitlab/gitlab_test.go @@ -116,6 +116,14 @@ func TestResolve(t *testing.T) { expectedPath: "some/path/to/project/", expectedSubPath: "index.html", }, + "when request path is capitalized": { + file: "client/testdata/test.gitlab.io.json", + target: "https://test.gitlab.io:443/My/Pages/Project/path/index.html", + expectedPrefix: "/my/pages/project/", + expectedPath: "some/path/to/project/", + expectedSubPath: "path/index.html", + expectedIsNamespace: false, + }, } for name, tc := range tests { -- cgit v1.2.3 From 92ac92eb9b87fed87760fc4862dbbed112e8ee60 Mon Sep 17 00:00:00 2001 From: Kassio Borges Date: Mon, 22 Jan 2024 11:53:08 -0300 Subject: Do not change the request capitalization --- .../gitlab/client/testdata/test.gitlab.io.json | 11 +++++++++++ internal/source/gitlab/gitlab.go | 9 +++++---- internal/source/gitlab/gitlab_test.go | 20 +++++++++++++------- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/internal/source/gitlab/client/testdata/test.gitlab.io.json b/internal/source/gitlab/client/testdata/test.gitlab.io.json index c094e52a..086123a9 100644 --- a/internal/source/gitlab/client/testdata/test.gitlab.io.json +++ b/internal/source/gitlab/client/testdata/test.gitlab.io.json @@ -34,6 +34,17 @@ "type": "file", "sha256": "0f8ef3377b30fc47f96b48247f463a726a802f62f3faa03d56403751d2f66c67" } + }, + { + "access_control": false, + "https_only": true, + "prefix": "/my/capitalizedprefix/", + "project_id": 126, + "source": { + "path": "some/path/to/project/", + "type": "file", + "sha256": "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3" + } } ] } diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 2de113f8..fe175aaf 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -72,18 +72,19 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) { return nil, response.Error } - urlPath := strings.ToLower(path.Clean(r.URL.Path)) + urlPath := path.Clean(r.URL.Path) + lowerUrlPath := strings.ToLower(urlPath) size := len(response.Domain.LookupPaths) for _, lookup := range response.Domain.LookupPaths { lookupPrefix := strings.ToLower(lookup.Prefix) - isSubPath := strings.HasPrefix(urlPath, lookupPrefix) - isRootPath := urlPath == path.Clean(lookupPrefix) + isSubPath := strings.HasPrefix(lowerUrlPath, lookupPrefix) + isRootPath := lowerUrlPath == path.Clean(lookupPrefix) if isSubPath || isRootPath { subPath := "" if isSubPath { - subPath = strings.TrimPrefix(urlPath, lookupPrefix) + subPath = urlPath[len(lookupPrefix):] } srv, err := g.fabricateServing(lookup) diff --git a/internal/source/gitlab/gitlab_test.go b/internal/source/gitlab/gitlab_test.go index d99e88e9..6d5d978a 100644 --- a/internal/source/gitlab/gitlab_test.go +++ b/internal/source/gitlab/gitlab_test.go @@ -116,13 +116,19 @@ func TestResolve(t *testing.T) { expectedPath: "some/path/to/project/", expectedSubPath: "index.html", }, - "when request path is capitalized": { - file: "client/testdata/test.gitlab.io.json", - target: "https://test.gitlab.io:443/My/Pages/Project/path/index.html", - expectedPrefix: "/my/pages/project/", - expectedPath: "some/path/to/project/", - expectedSubPath: "path/index.html", - expectedIsNamespace: false, + "when project name requested is capitalized": { + file: "client/testdata/test.gitlab.io.json", + target: "https://test.gitlab.io:443/My/CapitalizedPrefix", + expectedPrefix: "/my/capitalizedprefix/", + expectedPath: "some/path/to/project/", + expectedSubPath: "", + }, + "when project name and requested path are capitalized": { + file: "client/testdata/test.gitlab.io.json", + target: "https://test.gitlab.io:443/My/CapitalizedPrefix/Path/Index.html", + expectedPrefix: "/my/capitalizedprefix/", + expectedPath: "some/path/to/project/", + expectedSubPath: "Path/Index.html", }, } -- cgit v1.2.3 From 0f5850946704c461f4415c5d1cfc5ce9c89b1644 Mon Sep 17 00:00:00 2001 From: Kassio Borges Date: Mon, 22 Jan 2024 13:05:06 -0300 Subject: Try to fix CI --- internal/source/gitlab/gitlab.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index fe175aaf..40b127d2 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -73,13 +73,13 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) { } urlPath := path.Clean(r.URL.Path) - lowerUrlPath := strings.ToLower(urlPath) + lowerURLPath := strings.ToLower(urlPath) size := len(response.Domain.LookupPaths) for _, lookup := range response.Domain.LookupPaths { lookupPrefix := strings.ToLower(lookup.Prefix) - isSubPath := strings.HasPrefix(lowerUrlPath, lookupPrefix) - isRootPath := lowerUrlPath == path.Clean(lookupPrefix) + isSubPath := strings.HasPrefix(lowerURLPath, lookupPrefix) + isRootPath := lowerURLPath == path.Clean(lookupPrefix) if isSubPath || isRootPath { subPath := "" -- cgit v1.2.3