fix(security): APIs that use NoEscape now reject URL segements with "." or ".." as a literal segement value
What does this MR do?
NoEscape puts a path value into the request path as it is. As agreed in the issue, it now checks and escapes the value:
- A value with a whole
.or..segment, plain or percent-encoded (%2e%2e,%2E%2E,a%2F..%2Fb), returns the newErrInvalidPathSegmentbefore any request is built. The check reads the value afterurl.PathUnescape, asNewRequestdoes, and compares whole segments, sonotes..txtand the branchv1./xstay valid. Empty segments are left as they are. - Bytes that
net/urlwould encode again (a space, a quote,?,#, a non-ASCII byte) are percent-encoded in the value, and%and/are kept. Before, such a byte made the encoded path invalid, so Go encoded the decoded path again and the project or group path lost its%2F:DownloadSingleArtifactsFile("group/proj", 7, "dir/my file.txt")went toprojects/group/proj/jobs/7/artifacts/dir/my%20file.txt. It now goes toprojects/group%2Fproj/jobs/7/artifacts/dir/my%20file.txt.
This covers every method that passes a NoEscape value: the three job artifact downloads, the system and group hook URL variables, the group hook custom headers and test trigger, the pipeline schedule variables, the license and project templates, the model registry package download, and the repository archive format, where StreamArchive built its path by hand and now goes through the same check.
I found this while working on gitlab-mcp-server, which passes names it receives to several of these methods.
Is this a breaking change?
Not for any value GitLab can serve. A value with a whole . or .. segment now returns an error instead of a request, and no file name, ref, key or template name can be such a segment. A value that worked before is sent byte for byte as before, including one the caller already escaped (Bug%20Report stays Bug%20Report). A value with a space or another byte Go encodes again no longer drops the %2F of the project or group path. ErrInvalidPathSegment is a new exported name.
How was this tested?
- A table-driven test per method, with the cases from the issue:
a/%2e%2e/%2e%2e/x yis refused before any request,notes..txtandv1./xare sent as they are, anddir/my file.txtkeepsgroup%2Fproj. Each refused case also checks that no request reached the server. TestEscapeInvalidPathBytes_MatchesNetURLgoes through all 256 byte values:net/urlsends every escaped value as it is, and a byte is escaped only whennet/urlwould have encoded it again.- On the code of
main, every new test fails except the one that keeps the existing error for an invalid escape such as100%. Six one-line mutants of the change each fail the new tests. make generateandmake fmtleave no drift, golangci-lint 2.15.0 reports 0 issues,buf formatandbuf lintpass, andgo test work -racepasses on Go 1.26 and 1.27.
Closes https://gitlab.com/gitlab-org/api/client-go/-/issues/2336