From 225a0f7026e885cec6605ddcf27aeacfb1a90221 Mon Sep 17 00:00:00 2001 From: oliverpool Date: Fri, 27 Jun 2025 23:10:09 +0200 Subject: [PATCH] git/TreeEntry: LinkTarget simplification (#8323) See #8222 for context. This PR removes a call to `Blob.GetBlobContent` and `Blob.DataAsync` and unifies symlink resolution: - length was unlimited in one case - length was truncated to 1024 chars in the other case Now it is hard-limited to 4096 chars (ie error if larger), which is a length which seems appropriate according to https://stackoverflow.com/a/22575737. ### Tests - Tests are already present in `tests/integration/repo_test.go:972`: `TestRepoFollowSymlink` (it caught a cap/len stupid mistake). - [x] I did not document these changes and I do not expect someone else to do it. - [x] I do not want this change to show in the release notes. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8323 Reviewed-by: Earl Warren Co-authored-by: oliverpool Co-committed-by: oliverpool --- modules/git/tree_entry.go | 43 ++++++++++++++++------------ services/repository/files/content.go | 2 +- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/modules/git/tree_entry.go b/modules/git/tree_entry.go index d51b7992fe..ec5c632ca0 100644 --- a/modules/git/tree_entry.go +++ b/modules/git/tree_entry.go @@ -116,32 +116,37 @@ func (te *TreeEntry) Type() string { } } +// LinkTarget returns the target of the symlink as string. +func (te *TreeEntry) LinkTarget() (string, error) { + if !te.IsLink() { + return "", ErrBadLink{te.Name(), "not a symlink"} + } + + const symlinkLimit = 4096 // according to git config core.longpaths https://stackoverflow.com/a/22575737 + blob := te.Blob() + if blob.Size() > symlinkLimit { + return "", ErrBadLink{te.Name(), "symlink too large"} + } + + rc, size, err := blob.NewTruncatedReader(symlinkLimit) + if err != nil { + return "", err + } + defer rc.Close() + + buf := make([]byte, int(size)) + _, err = io.ReadFull(rc, buf) + return string(buf), err +} + // FollowLink returns the entry pointed to by a symlink func (te *TreeEntry) FollowLink() (*TreeEntry, string, error) { - if !te.IsLink() { - return nil, "", ErrBadLink{te.Name(), "not a symlink"} - } - // read the link - r, err := te.Blob().DataAsync() + lnk, err := te.LinkTarget() if err != nil { return nil, "", err } - closed := false - defer func() { - if !closed { - _ = r.Close() - } - }() - buf := make([]byte, te.Size()) - _, err = io.ReadFull(r, buf) - if err != nil { - return nil, "", err - } - _ = r.Close() - closed = true - lnk := string(buf) t := te.ptree // traverse up directories diff --git a/services/repository/files/content.go b/services/repository/files/content.go index dfdee1d1df..5a6006e9f2 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -206,7 +206,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref } else if entry.IsLink() { contentsResponse.Type = string(ContentTypeLink) // The target of a symlink file is the content of the file - targetFromContent, err := entry.Blob().GetBlobContent(1024) + targetFromContent, err := entry.LinkTarget() if err != nil { return nil, err }