Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 11 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ on:
- main
jobs:
run-tests:
name: Run tests
name: Run tests (Node ${{ matrix.node }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: [20, 24]
steps:
- uses: actions/setup-node@v4
- uses: actions/setup-node@v6
with:
node-version: '20.x'
node-version: ${{ matrix.node }}
registry-url: 'https://registry.npmjs.org'
- uses: pnpm/action-setup@v4
- uses: pnpm/action-setup@v5
with:
version: 10
version: 10.6.0
run_install: false
- uses: actions/checkout@v4
- uses: actions/checkout@v6
with:
fetch-depth: 0
ref: ${{ github.ref }}
Expand All @@ -29,4 +33,4 @@ jobs:
pnpm install --frozen-lockfile
pnpm build
- name: Run tests
run: pnpm test
run: pnpm test:run
2 changes: 1 addition & 1 deletion packages/auth-provider-automerge-repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@localfirst/auth": "workspace:*",
"@localfirst/crypto": "workspace:*",
"@localfirst/shared": "workspace:*",
"msgpackr": "1.11.2"
"msgpackr": "1.11.8"
},
"devDependencies": {
"@automerge/automerge-repo-network-messagechannel": "^1.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@localfirst/shared": "workspace:*",
"@paralleldrive/cuid2": "^2.2.2",
"lodash-es": "^4.17.21",
"msgpackr": "1.11.2",
"msgpackr": "1.11.8",
"xstate": "^5.9.1"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/crdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@paralleldrive/cuid2": "^2.2.2",
"@types/lodash-es": "^4.17.12",
"lodash-es": "^4.17.21",
"msgpackr": "1.11.2"
"msgpackr": "1.11.8"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"bs58": "^5.0.0",
"libsodium-wrappers-sumo": "^0.7.13",
"lodash-es": "^4.17.21",
"msgpackr": "1.11.2"
"msgpackr": "1.11.8"
},
"publishConfig": {
"access": "public"
Expand Down
115 changes: 115 additions & 0 deletions packages/crypto/src/test/longPayload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Buffer } from 'node:buffer'
import { pack, unpack } from 'msgpackr'
import { describe, expect, test } from 'vitest'
import { asymmetric, hash, signatures, symmetric } from '../index.js'
import { keyToBytes } from '../util/index.js'

// Long UTF-8 strings exercise msgpackr's Buffer write path, which threw
// a RangeError with msgpackr 1.10.2 on Node 24. These are public test values.
const text = 'Quiet 🌱 — ключи — 鍵 '.repeat(12)
const makePayload = () => ({
type: 'long-unicode-payload',
text,
nested: { count: 42, enabled: true, empty: null, previous: undefined },
history: [{ text, previous: undefined }],
bytes: Buffer.from([0, 1, 127, 128, 255]),
})

type TestPayload = ReturnType<typeof makePayload>

function expectPayload(actual: TestPayload) {
// libsodium returns Uint8Array, while Node's MessagePack reader can return
// Buffer. Both must preserve every byte and all nested/undefined fields.
expect(actual.bytes).toBeInstanceOf(Uint8Array)
expect({ ...actual, bytes: Array.from(actual.bytes) }).toStrictEqual({
...makePayload(),
bytes: Array.from(makePayload().bytes),
})
}

describe('long Unicode payloads on current Node', () => {
test('serializes nested values, undefined fields and binary bytes without truncation', () => {
expect(Buffer.byteLength(text, 'utf8')).toBeGreaterThan(64)
for (let index = 0; index < 3; index++) {
// Repeated packing also exercises writes at different offsets in the
// serializer's reusable buffer, rather than only its first allocation.
expectPayload(unpack(pack(makePayload())))
}
})

test('hashes every field deterministically and distinguishes changed payloads', () => {
const purpose = 'public-node24-regression-hash'
const digest = hash(purpose, makePayload())
expect(keyToBytes(digest)).toHaveLength(32)
expect(hash(purpose, makePayload())).toBe(digest)
expect(hash(purpose, { ...makePayload(), text: text + '!' })).not.toBe(digest)
expect(hash('another-node24-hash-purpose', makePayload())).not.toBe(digest)
})

test('signs the full payload and rejects text, nested-field and binary tampering', () => {
const signer = signatures.keyPair('public-node24-regression-signer')
const payload = makePayload()
const context = 'test/node24-long-unicode'
const signature = signatures.sign(payload, signer.secretKey, context)
expect(signatures.sign(makePayload(), signer.secretKey, context)).toBe(signature)
expect(signatures.verify({ payload, signature, publicKey: signer.publicKey, context })).toBe(
true
)
expect(
signatures.verify({
payload,
signature,
publicKey: signer.publicKey,
context: context + '/other',
})
).toBe(false)
const changed = [
{ ...makePayload(), text: text + '!' },
{ ...makePayload(), nested: { ...payload.nested, count: 43 } },
{ ...makePayload(), bytes: Buffer.from([0, 1, 127, 128, 254]) },
]
for (const tampered of changed) {
expect(
signatures.verify({ payload: tampered, signature, publicKey: signer.publicKey, context })
).toBe(false)
}
})

test('symmetrically encrypts and decrypts the payload and rejects modified ciphertext', () => {
const password = 'public-node24-regression-password'
const cipher = symmetric.encrypt(makePayload(), password)
expectPayload(symmetric.decrypt(cipher, password))
expect(() => symmetric.decrypt(cipher, 'a-different-test-password')).toThrow()

const cipherBytes = symmetric.encryptBytes(makePayload(), password)
expectPayload(symmetric.decryptBytes(cipherBytes, password))
const modified = unpack(cipherBytes)
modified.message[0] ^= 1
expect(() => symmetric.decryptBytes(pack(modified), password)).toThrow()
})

test.each(['provided', 'ephemeral'] as const)(
'asymmetric round trip with a %s sender key',
mode => {
const recipient = asymmetric.keyPair('public-node24-regression-recipient')
const sender = asymmetric.keyPair('public-node24-regression-sender')
const wrongRecipient = asymmetric.keyPair('public-node24-regression-wrong-recipient')
const cipher = asymmetric.encrypt({
secret: makePayload(),
recipientPublicKey: recipient.publicKey,
senderSecretKey: mode === 'provided' ? sender.secretKey : undefined,
})
const senderPublicKey = mode === 'provided' ? sender.publicKey : undefined
expectPayload(
asymmetric.decrypt({ cipher, senderPublicKey, recipientSecretKey: recipient.secretKey })
)
expect(() =>
asymmetric.decrypt({
cipher,
senderPublicKey,
recipientSecretKey: wrongRecipient.secretKey,
})
).toThrow()
}
)
})
23 changes: 15 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading