Skip to content

Commit

Permalink
Fix encryptActions changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Feb 21, 2024
1 parent cc53242 commit bfb2592
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
6 changes: 3 additions & 3 deletions encrypt-actions/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import type { Client } from '../client/index.js'
* to *not* encrypt.
* @returns Unbind listener.
*/
export async function encryptActions(
export function encryptActions(
client: Client,
secret: CryptoKey|string,
secret: CryptoKey | string,
opts?: {
ignore: string[]
}
):Promise<void>
): void

export function getRandomSpaces(): string
14 changes: 5 additions & 9 deletions encrypt-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,27 @@ async function decrypt(action, key) {
return bytesToObj(bytes)
}

export async function encryptActions(client, secret, opts = {}) {
export function encryptActions(client, secret, opts = {}) {
let key
if (secret instanceof CryptoKey) {

Check failure on line 83 in encrypt-actions/index.js

View workflow job for this annotation

GitHub Actions / Node.js 18 Quick

encrypt-actions/index.test.ts > encrypts and decrypts actions

ReferenceError: CryptoKey is not defined ❯ Module.encryptActions encrypt-actions/index.js:83:25 ❯ encrypt-actions/index.test.ts:68:3

Check failure on line 83 in encrypt-actions/index.js

View workflow job for this annotation

GitHub Actions / Node.js 18 Quick

encrypt-actions/index.test.ts > accepts key

ReferenceError: CryptoKey is not defined ❯ Module.encryptActions encrypt-actions/index.js:83:25 ❯ encrypt-actions/index.test.ts:116:3

Check failure on line 83 in encrypt-actions/index.js

View workflow job for this annotation

GitHub Actions / Node.js 18 Quick

encrypt-actions/index.test.ts > ignores specific actions

ReferenceError: CryptoKey is not defined ❯ Module.encryptActions encrypt-actions/index.js:83:25 ❯ encrypt-actions/index.test.ts:144:3

Check failure on line 83 in encrypt-actions/index.js

View workflow job for this annotation

GitHub Actions / Node.js 18 Quick

encrypt-actions/index.test.ts > cleans actions on server

ReferenceError: CryptoKey is not defined ❯ Module.encryptActions encrypt-actions/index.js:83:25 ❯ encrypt-actions/index.test.ts:177:3
key = secret
} else {
key = await getKey()
}

async function getKey() {
let _key = crypto.subtle.importKey(
async function buildKey() {
return crypto.subtle.importKey(
'raw',
await sha256(secret),
{ name: 'AES-GCM' },
false,
['encrypt', 'decrypt']
)

return _key
}

let ignore = new Set(opts.ignore || [])

async function onReceive(action, meta) {
if (action.type === '0') {
if (!key) key = await getKey()
if (!key) key = await buildKey()
let decrypted = await decrypt(action, key)
return [decrypted, meta]
} else {
Expand All @@ -118,7 +114,7 @@ export async function encryptActions(client, secret, opts = {}) {
} else if (result[0].type === '0/clean' || ignore.has(result[0].type)) {
return [result[0], result[1]]
} else {
if (!key) key = await getKey()
if (!key) key = await buildKey()
let encrypted = await encrypt(result[0], key)
return [encrypted, result[1]]
}
Expand Down
37 changes: 37 additions & 0 deletions encrypt-actions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,43 @@ it('encrypts and decrypts actions', async () => {
expect(size1 !== size2 || size1 !== size3 || size1 !== size4).toBeTruthy()
})

it('accepts key', async () => {
let client1 = createClient()
let client2 = createClient()

let key = await crypto.subtle.generateKey(
{
length: 256,
name: 'AES-GCM'
},
true,
['encrypt', 'decrypt']
)

encryptActions(client1, key)
encryptActions(client2, key)

await Promise.all([connect(client1), connect(client2)])
getPair(client1).clear()

client1.log.add({ type: 'sync', value: 'secret' }, { sync: true })
await delay(50)
expect(getPair(client1).leftSent).toMatchObject([
[
'sync',
1,
{ d: BASE64, iv: BASE64, type: '0' },
{ id: 1, time: expect.any(Number) }
]
])

getPair(client2).right.send(getPair(client1).leftSent[0])
await delay(10)
expect(privateMethods(client2.log).actions()).toEqual([
{ type: 'sync', value: 'secret' }
])
})

it('ignores specific actions', async () => {
let client1 = createClient()
let client2 = createClient()
Expand Down

0 comments on commit bfb2592

Please sign in to comment.