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

chore: Basic UnixFS sanity checks #10701

Merged
merged 25 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f8a2fe5
fix::> Added checks for UnixFS + added force flag
PsychoPunkSage Feb 6, 2025
9db46b1
docs: update min requirements (#10687)
lidel Feb 5, 2025
3031cf3
docs(release): update RELEASE_CHECKLIST.md after v0.33.1 (#10697)
lidel Feb 5, 2025
b28bce9
Merge branch 'master' into issue-10331
PsychoPunkSage Feb 6, 2025
e922ef0
fix::> Removed confusing shorthand
PsychoPunkSage Feb 15, 2025
3297308
fix::> impl changes
PsychoPunkSage Feb 15, 2025
4a8bb62
fix::> Basic Setup of Tests
PsychoPunkSage Feb 15, 2025
a687b2b
fix::> test 1
PsychoPunkSage Feb 15, 2025
de94ec6
fix::> test passing
PsychoPunkSage Feb 15, 2025
58e4c90
fix::> formatting
PsychoPunkSage Feb 16, 2025
8e6796f
fix::> Proper test env ready
PsychoPunkSage Feb 16, 2025
fa7c769
fix::> Proper test ready
PsychoPunkSage Feb 16, 2025
1296054
fix::> Test Passing
PsychoPunkSage Feb 19, 2025
888e1e8
Merge branch 'master' into issue-10331
gammazero Feb 20, 2025
8ab8a83
Rename files cp "force" option to "no-validate"
gammazero Feb 20, 2025
d945049
Use require package for more compact test code
gammazero Feb 20, 2025
34f05a2
Rename file_test.go to files_test.go to match the files command
gammazero Feb 20, 2025
b77142d
Update core/commands/files.go
PsychoPunkSage Feb 27, 2025
9f7f8b6
Update core/commands/files.go
PsychoPunkSage Feb 27, 2025
9a59b8d
Update core/commands/files.go
PsychoPunkSage Feb 27, 2025
7ad6847
fix::> Added dedicated tests....
PsychoPunkSage Feb 27, 2025
548f630
fix::> more random tests
PsychoPunkSage Feb 27, 2025
5c93132
refactor: remove --force, simplify checks
lidel Mar 5, 2025
bf04ec1
Merge branch 'master' into issue-10331
lidel Mar 5, 2025
23c8b6e
docs: changelog
lidel Mar 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ cache, free memory and speed up read operations.`,
const (
filesCidVersionOptionName = "cid-version"
filesHashOptionName = "hash"
filesNoValidateOptionName = "no-validate"
)

var (
Expand Down Expand Up @@ -440,6 +441,7 @@ being GC'ed.
},
Options: []cmds.Option{
cmds.BoolOption(filesParentsOptionName, "p", "Make parent directories as needed."),
cmds.BoolOption(filesNoValidateOptionName, "Copy even if the node is not a valid UnixFS node."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
mkParents, _ := req.Options[filesParentsOptionName].(bool)
Expand Down Expand Up @@ -480,6 +482,19 @@ being GC'ed.
return fmt.Errorf("cp: cannot get node from path %s: %s", src, err)
}

force, _ := req.Options[filesNoValidateOptionName].(bool)
if !force {
// Check if it's a raw node
if _, ok := node.(*dag.RawNode); !ok {
if _, ok := node.(*dag.ProtoNode); !ok {
return fmt.Errorf("cp: source must be a UnixFS node or raw data")
}
if _, err = ft.FSNodeFromBytes(node.(*dag.ProtoNode).Data()); err != nil {
return fmt.Errorf("cp: source must be a UnixFS node or raw data: %s", err)
}
}
}

if mkParents {
err := ensureContainingDirectoryExists(nd.FilesRoot, dst, prefix)
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions core/commands/files_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package commands

import (
"context"
"io"
"testing"

dag "github.com/ipfs/boxo/ipld/merkledag"
cmds "github.com/ipfs/go-ipfs-cmds"
coremock "github.com/ipfs/kubo/core/mock"
"github.com/stretchr/testify/require"
)

func TestFilesCp_DagCborNodeFails(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cmdCtx, err := coremock.MockCmdsCtx()
require.NoError(t, err)

node, err := cmdCtx.ConstructNode()
require.NoError(t, err)

invalidData := []byte{0x00}
protoNode := dag.NodeWithData(invalidData)
err = node.DAG.Add(ctx, protoNode)
require.NoError(t, err)

req := &cmds.Request{
Context: ctx,
Arguments: []string{
"/ipfs/" + protoNode.Cid().String(),
"/test-destination",
},
Options: map[string]interface{}{
"force": false,
},
}

_, pw := io.Pipe()
res, err := cmds.NewWriterResponseEmitter(pw, req)
require.NoError(t, err)

err = filesCpCmd.Run(req, res, &cmdCtx)
require.Error(t, err)
require.ErrorContains(t, err, "cp: source must be a UnixFS node or raw data")
}
Loading