Skip to content

golink: stop resolveLink from recursing forever on alias loops - #271

Open
Mohith26 wants to merge 2 commits into
tailscale:mainfrom
Mohith26:fix/resolve-link-loop
Open

Mohith26 wants to merge 2 commits into
tailscale:mainfrom
Mohith26:fix/resolve-link-loop

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 3, 2026 •

Copy link
Copy Markdown

resolveLink follows alias links (http://go/x or /x) by calling itself, with no limit, so two links that point at each other, or a link that points at itself, recurse until the runtime kills the process. With this snapshot:

{"Short":"a","Long":"http://go/b"}
{"Short":"b","Long":"http://go/a"}

golink -resolve-from-backup snapshot.json go/a on main ends with:

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

This makes resolveLink iterative and tracks the links it has already visited, so a loop fails as soon as a link repeats (one DB load per distinct link) and the error names the cycle:

resolving "a": alias loop a -> b -> a

Visited links are keyed by linkID, so spellings that db.Load treats as the same link (case, dashes) are also caught. DB errors are wrapped with %w, so errors.Is(err, fs.ErrNotExist) still works for a missing link.

Normal alias chains are unchanged (go/m/foo with m -> http://go/meet still resolves to https://meet.google.com/lookup/foo).

Testing: added TestResolveLinkLoop (a two-link loop, the same loop entered via go/b, a self-referencing /self link, and a loop through differently spelled shorts), checking the exact error, and TestResolveLinkMissing for the wrapped not-found error. On main the loop test crashes the test binary with the stack overflow above; with the change it passes. go vet . and go test ./... pass, and I checked both snapshots above against a locally built cmd/golink.

The only caller today is the CLI resolve path, so this doesn't affect the HTTP server.

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>

@mikeodr mikeodr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, the idea is sound but the implementation needs some improvement.

Comment thread golink.go Outdated
}

// maxResolveHops is how many alias links resolveLink will follow before
// giving up, so that links which point at each other return an error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue(blocking): the recursion, while functional does not fast fail and needs to run 32 times to determine the failure. It also requires 32 DB hits.

suggestion(untested, please validate and cleanup):

// resolveLink follows a go link (and any go-link aliases it points at)
// to its final destination. It returns an error naming the cycle if
// the aliases loop.
func (s *Server) resolveLink(link *url.URL) (*url.URL, error) {
        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)
                }

                short, remainder, _ := strings.Cut(strings.TrimPrefix(path, "/"), "/")
                if seen[short] {
                        chain = append(chain, short)
                        return nil, fmt.Errorf("resolving %q: alias loop %s", chain[0], strings.Join(chain, " -> "))
                }
                seen[short] = true
                chain = append(chain, short)

                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
        }
}

In addition to fast failing, this also differentiates the DB error vs the loop error so Errors.Is can work from any caller.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's much better. I took your version in b8804cc with two small changes: the seen-set is keyed by linkID(short) so loops through differently cased or dashed spellings of the same link are caught on the first repeat (that's what db.Load matches on), and I dropped the 32 hop cap entirely. DB errors are wrapped with %w as you suggested, and there's a new TestResolveLinkMissing checking errors.Is(err, fs.ErrNotExist). TestResolveLinkLoop now asserts the exact message, e.g. resolving "a": alias loop a -> b -> a.

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 <mikeo@tailscale.com>
Signed-off-by: Mohith Gajjela <109003762+Mohith26@users.noreply.github.com>
@Mohith26

Mohith26 commented Nov 7, 2026

Copy link
Copy Markdown
Author

Updated in b8804cc: resolveLink is now iterative with a seen-set, based on @mikeodr's suggestion. A loop fails on the first repeated link and the error names the cycle; DB errors are wrapped so errors.Is works. go vet ./... and go test ./... pass, and I rechecked -resolve-from-backup against a locally built cmd/golink (loop, normal alias chain, missing link).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants