Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
Thanks for the PR, the idea is sound but the implementation needs some improvement.
| } | ||
|
|
||
| // maxResolveHops is how many alias links resolveLink will follow before | ||
| // giving up, so that links which point at each other return an error |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
|
Updated in b8804cc: |
resolveLinkfollows alias links (http://go/xor/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:golink -resolve-from-backup snapshot.json go/aon main ends with:This makes
resolveLinkiterative 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:Visited links are keyed by
linkID, so spellings thatdb.Loadtreats as the same link (case, dashes) are also caught. DB errors are wrapped with%w, soerrors.Is(err, fs.ErrNotExist)still works for a missing link.Normal alias chains are unchanged (
go/m/foowithm -> http://go/meetstill resolves tohttps://meet.google.com/lookup/foo).Testing: added
TestResolveLinkLoop(a two-link loop, the same loop entered viago/b, a self-referencing/selflink, and a loop through differently spelled shorts), checking the exact error, andTestResolveLinkMissingfor 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 .andgo test ./...pass, and I checked both snapshots above against a locally builtcmd/golink.The only caller today is the CLI resolve path, so this doesn't affect the HTTP server.