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

Duplicate chunks throw an error #51

Open
cooper667 opened this issue Jun 1, 2022 · 3 comments · Fixed by gliff-ai/etebase-js#2
Open

Duplicate chunks throw an error #51

cooper667 opened this issue Jun 1, 2022 · 3 comments · Fixed by gliff-ai/etebase-js#2

Comments

@cooper667
Copy link

cooper667 commented Jun 1, 2022

If you setContent with repetitive data on a collection, an error is thrown when trying to get that content again.

 TypeError: Cannot read properties of undefined (reading 'length')
    at reduce (/etebase-js/src/Crypto.ts:36:49)
    at Array.reduce (<anonymous>)
    at concatArrayBuffersArrays (/etebase-js/src/Crypto.ts:36:26)
    ...

It looks like the index list and/or chunks created here are the issue (the indices are pointers at the original chunks array, but the chunks array is mutated here, so the pointers are incorrect?)

The following tests illustrate the issue (large files are fine, small files are fine, but repetitive data (that would result in chunks that would be deduplicated) throws the error

describe.only("chunking files", () => {
  it("Duplicate Chunks", async () => {
    const collectionManager = etebase.getCollectionManager();
    const col = await collectionManager.create(colType, {}, "");

    const buf = randomBytesDeterministic(10 * 1024, new Uint8Array(32)); // 10kb of psuedorandom data
    const content = JSON.stringify([buf, buf, buf, buf]);

    await col.setContent(content);

    await collectionManager.transaction(col);
    const decryptedContent = await col.getContent(); // <---
    /*
  TypeError: Cannot read properties of undefined (reading 'length')
    at reduce (/etebase-js/src/Crypto.ts:36:49)
    at Array.reduce (<anonymous>)
    at concatArrayBuffersArrays (/etebase-js/src/Crypto.ts:36:26)
    ...
  */

    const out = to_string(decryptedContent);
    expect(out).toEqual(content);
  });

  it("Regular Chunks", async () => {
    const collectionManager = etebase.getCollectionManager();
    const col = await collectionManager.create(colType, {}, "");

    const buf = randomBytesDeterministic(100 * 1024, new Uint8Array(32));
    const content = JSON.stringify(buf);

    await col.setContent(content);

    await collectionManager.transaction(col);
    const decryptedContent = await col.getContent();

    const out = to_string(decryptedContent);
    expect(out).toEqual(content);
  });
  
  it("Small file, no chunks", async () => {
    const collectionManager = etebase.getCollectionManager();
    const col = await collectionManager.create(colType, {}, "");

    const buf = randomBytesDeterministic(10, new Uint8Array(32));
    const content = JSON.stringify(buf);

    await col.setContent(content);

    await collectionManager.transaction(col);
    const decryptedContent = await col.getContent();

    const out = to_string(decryptedContent);
    expect(out).toEqual(content);
  });
});

I'll do a PR with a fix shortly

@tasn
Copy link
Member

tasn commented Jun 1, 2022

Let me know once the PR is ready and I'll take a look. I wonder if this also affects the Rust code (and thus every other library).

@tasn
Copy link
Member

tasn commented Jun 1, 2022

Did you mean to close this?

@cooper667
Copy link
Author

cooper667 commented Jun 2, 2022

No! I didn't think merging the PR into a fork would close it.

PR is #54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment