Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overwrite symlinks when extracting tarballs #867

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

Wwwsylvia
Copy link
Member

@Wwwsylvia Wwwsylvia commented Jan 8, 2025

  1. Fix the bug that symbolic links are not automatically overwritten when extracted from a tar archive to the File store.
  2. Rename some functions and variables for better readability.
  3. Add corresponding unit tests.
  4. Improve docs and error messages.

Fixes #865

content/file/utils.go Fixed Show fixed Hide fixed
Copy link

codecov bot commented Jan 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.03%. Comparing base (eeb21fc) to head (beb75c1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #867      +/-   ##
==========================================
+ Coverage   75.67%   76.03%   +0.35%     
==========================================
  Files          63       63              
  Lines        6011     6025      +14     
==========================================
+ Hits         4549     4581      +32     
+ Misses       1079     1064      -15     
+ Partials      383      380       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -61,6 +61,7 @@ func tarDirectory(ctx context.Context, root, prefix string, w io.Writer, removeT
name = filepath.ToSlash(name)

// Generate header
// NOTE: We don't support hard links and treat it as regular files
Copy link
Member

Choose a reason for hiding this comment

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

If there wqs a duplicate blob in this with the same shasum, a hard link would be ideal. Is that a possibility with this method?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean users may push a directory containing two files, one of them is a hark link to another?

// NOTE: ORAS does not generate hard links when creating tarballs.
// If a hard link is found in the tarball, it will be extracted.
// If the target link already exists, os.Link will throw an error.
// This is a known limitation and will not be addressed.
Copy link
Member

Choose a reason for hiding this comment

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

If supporting links is possible and maybe desirable in the futre, maybe just make an issue out of this

Copy link
Member Author

Choose a reason for hiding this comment

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

Hard links in tarballs are tricky to support and can potentially introduce issues. For example, it requires exhaustive handling/tests on different OS. If there is a valid use case, we can evaluate the scenario and see if we need an issue for this.

Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
@Wwwsylvia Wwwsylvia force-pushed the overwrite_file_links branch from 62aba2f to 9fb9f9e Compare January 9, 2025 10:20
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
content/file/utils.go Fixed Show fixed Hide fixed
content/file/utils.go Fixed Show fixed Hide fixed
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
@@ -166,29 +167,41 @@
}

// Name check
name := header.Name
path, err := ensureBasePath(dir, prefix, name)
filename := header.Name

Check failure

Code scanning / CodeQL

Arbitrary file write extracting an archive containing symbolic links High

Unresolved path from an archive header, which may point outside the archive root, is used in
symlink creation
.
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Comment on lines 198 to 204
if _, err := os.Lstat(filePath); err == nil {
// link already exists, remove it first
if err := os.Remove(filePath); err != nil {
return err
}
}
err = os.Symlink(target, filePath)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should assume the link does not exist. If os.Symlink() fails, we should then remove and retry.

Copy link
Member Author

Choose a reason for hiding this comment

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

This makes sense, updated.

Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
}
case tar.TypeSymlink:
var target string
if target, err = ensureLinkPath(dir, prefix, path, header.Linkname); err == nil {
err = os.Symlink(target, path)
target, err = ensureLinkPath(dirPath, dirName, filePath, header.Linkname)

Check failure

Code scanning / CodeQL

Arbitrary file write extracting an archive containing symbolic links High

Unresolved path from an archive header, which may point outside the archive root, is used in
symlink creation
.
Unresolved path from an archive header, which may point outside the archive root, is used in
symlink creation
.
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.

Bug: symbolic links are not automatically overwritten when extracted from tar archive to File store
3 participants