From fa16dc22e5d0a9f28be346c3d04d5c8afa45e808 Mon Sep 17 00:00:00 2001 From: Mohith Gajjela <109003762+Mohith26@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:46:38 -0500 Subject: [PATCH 1/2] golink: stop resolveLink from recursing forever on alias loops resolveLink follows alias links (http://go/x or /x) by calling itself, with no limit. Two links that point at each other, or a link that points at itself, made `golink -resolve-from-backup snapshot.json go/a` recurse until the Go runtime aborted with "goroutine stack exceeds 1000000000-byte limit". Cap the number of alias hops at 32 and return an error naming a possible loop instead. Signed-off-by: Mohith Gajjela <109003762+Mohith26@users.noreply.github.com> --- golink.go | 14 +++++++++++++- golink_test.go | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/golink.go b/golink.go index 229e245..0f60845 100644 --- a/golink.go +++ b/golink.go @@ -1445,7 +1445,19 @@ func (s *Server) restoreSnapshot(snapshot []byte) error { return bs.Err() } +// maxResolveHops is how many alias links resolveLink will follow before +// giving up, so that links which point at each other return an error +// instead of recursing until the stack overflows. +const maxResolveHops = 32 + func (s *Server) resolveLink(link *url.URL) (*url.URL, error) { + return s.resolveLinkHops(link, 0) +} + +func (s *Server) resolveLinkHops(link *url.URL, hops int) (*url.URL, error) { + if hops >= maxResolveHops { + return nil, fmt.Errorf("resolving %q: followed %d alias links, possible loop", link.String(), hops) + } path := link.Path // if link was specified as "go/name", it will parse with no scheme or host. @@ -1462,7 +1474,7 @@ func (s *Server) resolveLink(link *url.URL) (*url.URL, error) { 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) + dst, err = s.resolveLinkHops(dst, hops+1) } } return dst, err diff --git a/golink_test.go b/golink_test.go index 2380102..e43ff93 100644 --- a/golink_test.go +++ b/golink_test.go @@ -734,6 +734,25 @@ 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"}) + + for _, link := range []string{"a", "go/b", "self"} { + t.Run(link, func(t *testing.T) { + got, err := s.resolveLink(must.Get(url.Parse(link))) + if err == nil { + t.Fatalf("ResolveLink(%q) = %v; want an error for a link loop", link, got) + } + if !strings.Contains(err.Error(), "possible loop") { + t.Errorf("ResolveLink(%q) error = %q; want it to mention a possible loop", link, err) + } + }) + } +} + func TestNoHSTSShortDomain(t *testing.T) { s := newTestServer(t) s.db.Save(&Link{Short: "foobar", Long: "http://foobar/"}) From b8804ccf6f69daead8a47bf147403f1c28be1d94 Mon Sep 17 00:00:00 2001 From: Mohith Gajjela <109003762+Mohith26@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:48:18 -0500 Subject: [PATCH 2/2] golink: resolve aliases iteratively and fail fast on loops Replace the recursive resolveLink and its 32 hop cap with a loop that tracks which links it has already visited. A loop is now reported as soon as a link repeats, after one DB load per distinct link, and the error names the cycle (for example: alias loop a -> b -> a). Visited links are keyed by linkID so that spellings db.Load treats as the same link (case, dashes) are also caught. DB errors are wrapped with %w so callers can still use errors.Is(err, fs.ErrNotExist). Suggested-by: Mike O'Driscoll Signed-off-by: Mohith Gajjela <109003762+Mohith26@users.noreply.github.com> --- golink.go | 59 +++++++++++++++++++++++++++----------------------- golink_test.go | 39 ++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 33 deletions(-) diff --git a/golink.go b/golink.go index 0f60845..f9608ae 100644 --- a/golink.go +++ b/golink.go @@ -1445,39 +1445,44 @@ func (s *Server) restoreSnapshot(snapshot []byte) error { return bs.Err() } -// maxResolveHops is how many alias links resolveLink will follow before -// giving up, so that links which point at each other return an error -// instead of recursing until the stack overflows. -const maxResolveHops = 32 - +// 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) { - return s.resolveLinkHops(link, 0) -} + seen := make(map[string]bool) + var chain []string + for { + path := link.Path -func (s *Server) resolveLinkHops(link *url.URL, hops int) (*url.URL, error) { - if hops >= maxResolveHops { - return nil, fmt.Errorf("resolving %q: followed %d alias links, possible loop", link.String(), hops) - } - 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, "/"), "/") + 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 - 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.resolveLinkHops(dst, hops+1) + 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 { diff --git a/golink_test.go b/golink_test.go index e43ff93..f74faeb 100644 --- a/golink_test.go +++ b/golink_test.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" "net" "net/http" "net/http/httptest" @@ -739,15 +740,41 @@ func TestResolveLinkLoop(t *testing.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"}) - for _, link := range []string{"a", "go/b", "self"} { - t.Run(link, func(t *testing.T) { - got, err := s.resolveLink(must.Get(url.Parse(link))) + 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", link, got) + 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) } - if !strings.Contains(err.Error(), "possible loop") { - t.Errorf("ResolveLink(%q) error = %q; want it to mention a possible loop", link, err) + }) + } +} + +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) } }) }