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
49 changes: 33 additions & 16 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,27 +1445,44 @@ func (s *Server) restoreSnapshot(snapshot []byte) error {
return bs.Err()
}

// resolveLink follows a go link, and any go link aliases it points at,
// to its final destination. If the aliases loop, it returns an error
// naming the cycle.
func (s *Server) resolveLink(link *url.URL) (*url.URL, error) {
path := link.Path
seen := make(map[string]bool)
var chain []string
for {
path := link.Path

// if link was specified as "go/name", it will parse with no scheme or host.
// Trim "go" prefix from beginning of path.
if link.Host == "" {
path = strings.TrimPrefix(path, s.hostname)
}
// if link was specified as "go/name", it will parse with no scheme or host.
// Trim "go" prefix from beginning of path.
if link.Host == "" {
path = strings.TrimPrefix(path, s.hostname)
}

short, remainder, _ := strings.Cut(strings.TrimPrefix(path, "/"), "/")
l, err := s.db.Load(short)
if err != nil {
return nil, err
}
dst, err := expandLink(l.Long, expandEnv{Now: time.Now().UTC(), Path: remainder})
if err == nil {
if dst.Host == "" || dst.Host == s.hostname {
dst, err = s.resolveLink(dst)
short, remainder, _ := strings.Cut(strings.TrimPrefix(path, "/"), "/")
chain = append(chain, short)
// Key on linkID so that differently cased or dashed spellings of
// the same link are treated as the same hop, matching db.Load.
id := linkID(short)
if seen[id] {
return nil, fmt.Errorf("resolving %q: alias loop %s", chain[0], strings.Join(chain, " -> "))
}
seen[id] = true

l, err := s.db.Load(short)
if err != nil {
return nil, fmt.Errorf("resolving %q: %w", short, err)
}
dst, err := expandLink(l.Long, expandEnv{Now: time.Now().UTC(), Path: remainder})
if err != nil {
return nil, err
}
if dst.Host != "" && dst.Host != s.hostname {
return dst, nil // not a go link alias; done
}
link = dst
}
return dst, err
}

func (s *Server) isRequestAuthorized(r *http.Request, u user, short string) bool {
Expand Down
46 changes: 46 additions & 0 deletions golink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -734,6 +735,51 @@ func TestResolveLink(t *testing.T) {
}
}

func TestResolveLinkLoop(t *testing.T) {
s := newTestServer(t)
s.db.Save(&Link{Short: "a", Long: "http://go/b"})
s.db.Save(&Link{Short: "b", Long: "http://go/a"})
s.db.Save(&Link{Short: "self", Long: "/self"})
// different spellings of the same link should still be caught as a loop
s.db.Save(&Link{Short: "up", Long: "/Down"})
s.db.Save(&Link{Short: "down", Long: "/u-p/x"})

tests := []struct {
link string
wantErr string
}{
{"a", `resolving "a": alias loop a -> b -> a`},
{"go/b", `resolving "b": alias loop b -> a -> b`},
{"self", `resolving "self": alias loop self -> self`},
{"up", `resolving "up": alias loop up -> Down -> u-p`},
}
for _, tt := range tests {
t.Run(tt.link, func(t *testing.T) {
got, err := s.resolveLink(must.Get(url.Parse(tt.link)))
if err == nil {
t.Fatalf("resolveLink(%q) = %v; want an error for a link loop", tt.link, got)
}
if err.Error() != tt.wantErr {
t.Errorf("resolveLink(%q) error = %q; want %q", tt.link, err, tt.wantErr)
}
})
}
}

func TestResolveLinkMissing(t *testing.T) {
s := newTestServer(t)
s.db.Save(&Link{Short: "dangling", Long: "/missing"})

for _, link := range []string{"missing", "dangling"} {
t.Run(link, func(t *testing.T) {
_, err := s.resolveLink(must.Get(url.Parse(link)))
if !errors.Is(err, fs.ErrNotExist) {
t.Errorf("resolveLink(%q) error = %v; want fs.ErrNotExist", link, err)
}
})
}
}

func TestNoHSTSShortDomain(t *testing.T) {
s := newTestServer(t)
s.db.Save(&Link{Short: "foobar", Long: "http://foobar/"})
Expand Down