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 new ErrInvalidPathSegment before any request is built. The check reads the value after url.PathUnescape, as NewRequest does, and compares whole segments, so notes..txt and the branch v1./x stay valid. Empty segments are left as they are.
  • Bytes that net/url would 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 to projects/group/proj/jobs/7/artifacts/dir/my%20file.txt. It now goes to projects/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 y is refused before any request, notes..txt and v1./x are sent as they are, and dir/my file.txt keeps group%2Fproj. Each refused case also checks that no request reached the server.
  • TestEscapeInvalidPathBytes_MatchesNetURL goes through all 256 byte values: net/url sends every escaped value as it is, and a byte is escaped only when net/url would 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 as 100%. Six one-line mutants of the change each fail the new tests.
  • make generate and make fmt leave no drift, golangci-lint 2.15.0 reports 0 issues, buf format and buf lint pass, and go test work -race passes on Go 1.26 and 1.27.

Closes https://gitlab.com/gitlab-org/api/client-go/-/issues/2336

Merge request reports

Loading
Loading