@cryptotaxi247 / kubo / commits / 485a5c291

fix(cli): object add-link: do not allow blocks over BS limit (#8414)

* fix(cli): object add-link: do not allow blocks over BS limit * refactor: allow-big-block - renamed override flag to --allow-big-block - separate tests for default and override behavior Co-authored-by: Marcin Rataj <lidel@lidel.org>

Lucas Molas committed Sep 28, 2021 at 10:27 UTC 485a5c291f14a091eae4e85cc2f113d10960a72a
2 files changed +67
core/commands/object/patch.go
+49
@@ -4,13 +4,20 @@ import (
4 "fmt"
5 "io"
6
7 + "github.com/ipfs/go-cid"
8 cmds "github.com/ipfs/go-ipfs-cmds"
9 "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10 + coreiface "github.com/ipfs/interface-go-ipfs-core"
11
12 "github.com/ipfs/interface-go-ipfs-core/options"
13 "github.com/ipfs/interface-go-ipfs-core/path"
14 )
15
16 +const (
17 + softBlockLimit = 1024 * 1024 // https://github.com/ipfs/go-ipfs/issues/7421#issuecomment-910833499
18 + allowBigBlock = "allow-big-block"
19 +)
20 +
21 var ObjectPatchCmd = &cmds.Command{
22 Helptext: cmds.HelpText{
23 Tagline: "Deprecated way to create a new merkledag object based on an existing one. Use MFS with 'files cp|rm' instead.",
@@ -41,6 +48,9 @@ For modern use cases, use MFS with 'files' commands: 'ipfs files --help'.
48 "rm-link": patchRmLinkCmd,
49 "set-data": patchSetDataCmd,
50 },
51 + Options: []cmds.Option{
52 + cmds.BoolOption(allowBigBlock, "Disable block size check and allow creation of blocks bigger than 1MB. WARNING: such blocks won't be transferable over the standard bitswap.").WithDefault(false),
53 + },
54 }
55
56 var patchAppendDataCmd = &cmds.Command{
@@ -82,6 +92,10 @@ DEPRECATED and provided for legacy reasons. Use 'ipfs add' or 'ipfs files' inste
92 return err
93 }
94
95 + if err := checkBlockSize(req, p.Cid(), api.Dag()); err != nil {
96 + return err
97 + }
98 +
99 return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()})
100 },
101 Type: &Object{},
@@ -128,6 +142,10 @@ DEPRECATED and provided for legacy reasons. Use 'files cp' and 'dag put' instead
142 return err
143 }
144
145 + if err := checkBlockSize(req, p.Cid(), api.Dag()); err != nil {
146 + return err
147 + }
148 +
149 return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()})
150 },
151 Type: Object{},
@@ -166,6 +184,10 @@ DEPRECATED and provided for legacy reasons. Use 'files rm' instead.
184 return err
185 }
186
187 + if err := checkBlockSize(req, p.Cid(), api.Dag()); err != nil {
188 + return err
189 + }
190 +
191 return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()})
192 },
193 Type: Object{},
@@ -232,6 +254,10 @@ Use MFS and 'files' commands instead:
254 return err
255 }
256
257 + if err := checkBlockSize(req, p.Cid(), api.Dag()); err != nil {
258 + return err
259 + }
260 +
261 return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()})
262 },
263 Type: Object{},
@@ -242,3 +268,26 @@ Use MFS and 'files' commands instead:
268 }),
269 },
270 }
271 +
272 +func checkBlockSize(req *cmds.Request, c cid.Cid, dagAPI coreiface.APIDagService) error {
273 + allowAnyBlockSize, _ := req.Options[allowBigBlock].(bool)
274 + if allowAnyBlockSize {
275 + return nil
276 + }
277 +
278 + // We do not allow producing blocks bigger than 1 MiB to avoid errors
279 + // when transmitting them over BitSwap. The 1 MiB constant is an
280 + // unenforced and undeclared rule of thumb hard-coded here.
281 + modifiedNode, err := dagAPI.Get(req.Context, c)
282 + if err != nil {
283 + return err
284 + }
285 + modifiedNodeSize, err := modifiedNode.Size()
286 + if err != nil {
287 + return err
288 + }
289 + if modifiedNodeSize > softBlockLimit {
290 + return fmt.Errorf("produced block is over 1MB, object API is deprecated and does not support HAMT-sharding: to create big directories, please use the files API (MFS)")
291 + }
292 + return nil
293 +}
test/sharness/t0051-object.sh
+18
@@ -223,6 +223,24 @@ test_object_cmd() {
223 ipfs object stat $OUTPUT
224 '
225
226 + test_expect_success "'ipfs object patch' check output block size" '
227 + DIR=$(ipfs object new unixfs-dir)
228 + for i in {1..13}
229 + do
230 + DIR=$(ipfs object patch "$DIR" add-link "$DIR.jpg" "$DIR")
231 + done
232 + # Fail when new block goes over the BS limit of 1MB, but allow manual override
233 + test_expect_code 1 ipfs object patch "$DIR" add-link "$DIR.jpg" "$DIR" >patch_out 2>&1
234 + '
235 +
236 + test_expect_success "ipfs object patch add-link output has the correct error" '
237 + grep "produced block is over 1MB, object API is deprecated and does not support HAMT-sharding: to create big directories, please use the files API (MFS)" patch_out
238 + '
239 +
240 + test_expect_success "ipfs object patch --allow-big-block=true add-link works" '
241 + test_expect_code 0 ipfs object patch --allow-big-block=true "$DIR" add-link "$DIR.jpg" "$DIR"
242 + '
243 +
244 test_expect_success "'ipfs object new foo' shouldn't crash" '
245 test_expect_code 1 ipfs object new foo
246 '