Skip to content

Commit

Permalink
fix: handle parsing invitation code errors in main.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Oct 24, 2023
1 parent 9f3680a commit ac0843d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
3 changes: 1 addition & 2 deletions packages/common/src/invitationCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const parseDeepUrl = ({ url, expectedProtocol = `quiet:` }: { url: string; expec
}
if (!validUrl || validUrl.protocol !== expectedProtocol) {
console.error(`Could not retrieve invitation code from deep url '${url}'`)
throw new Error()
throw new Error(`Invalid url`)
}
const params = validUrl.searchParams
const codes: InvitationPair[] = []
Expand Down Expand Up @@ -146,7 +146,6 @@ export const argvInvitationCode = (argv: string[]): InvitationData | null => {
}

const peerDataValid = ({ peerId, onionAddress }: { peerId: string; onionAddress: string }): boolean => {
// TODO: rename to peerDataValid?
if (!peerId.match(PEER_ID_REGEX)) {
// TODO: test it more properly e.g with PeerId.createFromB58String(peerId.trim())
console.log(`PeerId ${peerId} is not valid`)
Expand Down
33 changes: 24 additions & 9 deletions packages/desktop/src/main/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,19 @@ describe('other electron app events ', () => {
})

describe('Invitation code', () => {
const codes: InvitationData = {
pairs: [
{
peerId: 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
onionAddress: 'y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd',
},
],
psk: 'BNlxfE2WBF7LrlpIX0CvECN5o1oZtA16PkAb7GYiwYw=',
}
let codes: InvitationData

beforeEach(() => {
codes = {
pairs: [
{
peerId: 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
onionAddress: 'y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd',
},
],
psk: 'BNlxfE2WBF7LrlpIX0CvECN5o1oZtA16PkAb7GYiwYw=',
}
})

it('handles invitation code on open-url event (on macos)', async () => {
expect(mockAppOnCalls[2][0]).toBe('ready')
Expand All @@ -258,6 +262,17 @@ describe('Invitation code', () => {
expect(mockWindowWebContentsSend).toHaveBeenCalledWith('invitation', { data: codes })
})

it('do not process invitation code on open-url event (on macos) if url is invalid', async () => {
codes['psk'] = '12345'
expect(mockAppOnCalls[2][0]).toBe('ready')
await mockAppOnCalls[2][1]()

expect(mockAppOnCalls[1][0]).toBe('open-url')
const event = { preventDefault: () => {} }
mockAppOnCalls[1][1](event, composeInvitationDeepUrl(codes))
expect(mockWindowWebContentsSend).not.toHaveBeenCalledWith('invitation', { data: codes })
})

it('process invitation code on second-instance event', async () => {
await mockAppOnCalls[2][1]()
const commandLine = ['/tmp/.mount_Quiet-TVQc6s/quiet', composeInvitationDeepUrl(codes)]
Expand Down
27 changes: 20 additions & 7 deletions packages/desktop/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,12 @@ app.on('open-url', (event, url) => {
event.preventDefault()
if (mainWindow) {
invitationUrl = null
const invitationCode = parseInvitationCodeDeepUrl(url) // TODO: handle thrown error
processInvitationCode(mainWindow, invitationCode)
try {
const invitationCode = parseInvitationCodeDeepUrl(url)
processInvitationCode(mainWindow, invitationCode)
} catch (e) {
console.warn(e)
}
}
})

Expand Down Expand Up @@ -474,13 +478,22 @@ app.on('ready', async () => {
throw new Error(`mainWindow is on unexpected type ${mainWindow}`)
}
if (process.platform === 'darwin' && invitationUrl) {
const invitationCode = parseInvitationCodeDeepUrl(invitationUrl) // TODO: handle thrown error
processInvitationCode(mainWindow, invitationCode)
invitationUrl = null
try {
const invitationCode = parseInvitationCodeDeepUrl(invitationUrl)
processInvitationCode(mainWindow, invitationCode)
} catch (e) {
console.warn(e)
} finally {
invitationUrl = null
}
}
if (process.platform !== 'darwin' && process.argv) {
const invitationCode = argvInvitationCode(process.argv) // TODO: handle thrown error?
processInvitationCode(mainWindow, invitationCode)
try {
const invitationCode = argvInvitationCode(process.argv)
processInvitationCode(mainWindow, invitationCode)
} catch (e) {
console.warn(e)
}
}

await checkForUpdate(mainWindow)
Expand Down

0 comments on commit ac0843d

Please sign in to comment.