Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This parser performs syntactic and semantic validation according to the
[errorformat](https://vim-jp.org/vimdoc-en/quickfix.html#error-file-format)
friendly way
- Verifies the existence of URLs by checking the response for URL fields
(can be disabled)
(can be disabled), including Mercurial repositories served over HTTP(S)

## As a library

Expand Down
28 changes: 27 additions & 1 deletion vcsurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -73,8 +74,33 @@ func (p *Parser) isRepo(ctx context.Context, u *url.URL) bool {
var ok bool

err := p.callVCS(ctx, func() { ok = vcsurl.IsRepo(u) })
if err != nil {
return false
}

return ok || p.isMercurialRepo(ctx, *u)
}

// Mercurial servers advertise the wire protocol on the same URL as the repository.
func (p *Parser) isMercurialRepo(ctx context.Context, u url.URL) bool {
query := u.Query()
query.Set("cmd", "capabilities")
u.RawQuery = query.Encode()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return false
}

resp, err := p.client.Do(req)
if err != nil {
return false
}
defer func() { _ = resp.Body.Close() }()

mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))

return err == nil && ok
return err == nil && resp.StatusCode == http.StatusOK && mediaType == "application/mercurial-0.1"
}

// repositoryURL drops a trailing slash and a ".git" suffix from a repository
Expand Down
47 changes: 47 additions & 0 deletions vcsurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,53 @@ import (
"testing"
)

func TestIsRepoRecognizesMercurialOverHTTP(t *testing.T) {
tests := []struct {
name string
contentType string
status int
want bool
}{
{"Mercurial", "application/mercurial-0.1", http.StatusOK, true},
{"Mercurial with a media type parameter", "application/mercurial-0.1; charset=utf-8", http.StatusOK, true},
{"ordinary web page", "text/html", http.StatusOK, false},
{"failed Mercurial request", "application/mercurial-0.1", http.StatusNotFound, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hg/project/" || r.URL.Query().Get("cmd") != "capabilities" {
http.NotFound(w, r)

return
}
if r.Method != http.MethodGet || r.URL.Query().Get("tenant") != "example" {
t.Errorf("unexpected capabilities request: %s %s", r.Method, r.URL)
}
w.Header().Set("Content-Type", tt.contentType)
w.WriteHeader(tt.status)
}))
defer server.Close()

p, err := NewParser(ParserConfig{AllowNetworkToPrivateHosts: true})
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(server.URL + "/hg/project/?tenant=example")
if err != nil {
t.Fatal(err)
}
if got := p.isRepo(t.Context(), u); got != tt.want {
t.Errorf("isRepo = %t, want %t", got, tt.want)
}
if got := u.RawQuery; got != "tenant=example" {
t.Errorf("isRepo changed the repository URL query to %q", got)
}
})
}
}

func TestParserNetworkPolicyIsolation(t *testing.T) {
var requests atomic.Int64
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down