Skip to content

Commit

Permalink
Rewrite GitLab Link headers safely
Browse files Browse the repository at this point in the history
  • Loading branch information
Benaiah committed Jun 7, 2018
1 parent ecb1591 commit 5489946
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions api/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,32 @@ func (gl *GitLabGateway) authenticate(w http.ResponseWriter, r *http.Request) er
var gitlabLinkRegex = regexp.MustCompile("<(.*?)>")
var gitlabLinkRelRegex = regexp.MustCompile("rel=\"(.*?)\"")

func rewriteGitlabLinkEntry(linkEntry, endpointAPIURL, proxyAPIURL string) string {
linkAndRel := strings.Split(strings.TrimSpace(linkEntry), ";")
if len(linkAndRel) != 2 {
return linkEntry
}

linkMatch := gitlabLinkRegex.FindStringSubmatch(linkAndRel[0])
if len(linkMatch) < 2 {
return linkEntry
}

relMatch := gitlabLinkRelRegex.FindStringSubmatch(linkAndRel[1])
if len(relMatch) < 2 {
return linkEntry
}

proxiedLink := proxyAPIURL + strings.TrimPrefix(linkMatch[1], endpointAPIURL)
rel := relMatch[1]
return "<" + proxiedLink + ">; rel=\"" + rel + "\""
}

func rewriteGitlabLinks(linkHeader, endpointAPIURL, proxyAPIURL string) string {
linkEntries := strings.Split(linkHeader, ",")
finalLinkEntries := []string{}
finalLinkEntries := make([]string, len(linkEntries), len(linkEntries))
for _, linkEntry := range linkEntries {
linkAndRel := strings.Split(strings.TrimSpace(linkEntry), ";")
link := proxyAPIURL + strings.TrimPrefix(gitlabLinkRegex.FindStringSubmatch(linkAndRel[0])[1], endpointAPIURL)
rel := gitlabLinkRelRegex.FindStringSubmatch(linkAndRel[1])[1]
finalLinkEntries = append(finalLinkEntries, "<"+link+">; rel=\""+rel+"\"")
finalLinkEntries = append(finalLinkEntries, rewriteGitlabLinkEntry(linkEntry, endpointAPIURL, proxyAPIURL))
}
finalLinkHeader := strings.Join(finalLinkEntries, ",")
return finalLinkHeader
Expand Down

0 comments on commit 5489946

Please sign in to comment.