Skip to content

Fix accidental duress stash creation in loginWithKey #654

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

Merged
merged 2 commits into from
May 12, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- fixed: Bug causing duress account creation from `loginWithKey` while in duress mode.
- fixed: Clean the Android build folder, so leftover files don't get published.

## 2.27.2 (2025-05-08)
Expand Down
13 changes: 9 additions & 4 deletions src/core/context/context-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ export function makeContextApi(ai: ApiInput): EdgeContext {
throw new Error(`Cannot find requested appId: "${appId}"`)
}

// Get the duress stash for existence check:
const duressAppId = appId + '.duress'
const duressStash = searchTree(
stashTree,
stash => stash.appId === duressAppId
)

let sessionKey: SessionKey
try {
sessionKey = {
Expand All @@ -161,9 +168,6 @@ export function makeContextApi(ai: ApiInput): EdgeContext {
makeAuthJson(stashTree, sessionKey)
} catch (error) {
if (error instanceof Error && error.message === 'Invalid checksum') {
const duressStash = searchTree(stashTree, stash =>
stash.appId.endsWith('.duress')
)
if (duressStash == null) {
throw error
}
Expand All @@ -188,7 +192,8 @@ export function makeContextApi(ai: ApiInput): EdgeContext {

return await makeAccount(ai, sessionKey, 'keyLogin', {
...opts,
duressMode: inDuressMode
// We must require that the duress account exists:
duressMode: inDuressMode && duressStash != null
})
},

Expand Down
66 changes: 66 additions & 0 deletions test/core/login/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,50 @@ describe('duress', function () {
expect(topicAccount.isDuressAccount).equals(true)
})

it('Avoid creating duress account when using loginWithPassword', async function () {
const world = await makeFakeEdgeWorld([fakeUser], quiet)
const context = await world.makeEdgeContext(contextOptions)
const otherAccount = await context.createAccount({
username: 'new-user',
pin: '1111'
})
// Create duress account:
await otherAccount.changePin({ pin: '0000', forDuressAccount: true })
await otherAccount.logout()
// Activate duress mode:
const duressAccount = await context.loginWithPIN('new-user', '0000')
await duressAccount.logout()

// Login to the main account using password:
const topicAccount = await context.loginWithPassword(
fakeUser.username,
fakeUser.password
)
expect(topicAccount.isDuressAccount).equals(false)
})

it('Avoid creating duress account when using loginWithPIN', async function () {
const world = await makeFakeEdgeWorld([fakeUser], quiet)
const context = await world.makeEdgeContext(contextOptions)
const otherAccount = await context.createAccount({
username: 'new-user',
pin: '1111'
})
// Create duress account:
await otherAccount.changePin({ pin: '0000', forDuressAccount: true })
await otherAccount.logout()
// Activate duress mode:
const duressAccount = await context.loginWithPIN('new-user', '0000')
await duressAccount.logout()

// Login to the main account using password:
const topicAccount = await context.loginWithPIN(
fakeUser.username,
fakeUser.pin
)
expect(topicAccount.isDuressAccount).equals(false)
})

it('persist duress mode when using loginWithKey', async function () {
const world = await makeFakeEdgeWorld([fakeUser], quiet)
const context = await world.makeEdgeContext(contextOptions)
Expand All @@ -547,6 +591,28 @@ describe('duress', function () {
expect(topicAccount2.isDuressAccount).equals(true)
})

it('Avoid creating duress account when using loginWithKey', async function () {
const world = await makeFakeEdgeWorld([fakeUser], quiet)
const context = await world.makeEdgeContext(contextOptions)
const otherAccount = await context.createAccount({
username: 'new-user',
pin: '1111'
})
const loginKey = await otherAccount.getLoginKey()
await otherAccount.logout()
const account = await context.loginWithPIN(fakeUser.username, fakeUser.pin)
// Create duress account:
await account.changePin({ pin: '0000', forDuressAccount: true })
await account.logout()
// Activate duress mode:
const duressAccount = await context.loginWithPIN(fakeUser.username, '0000')
await duressAccount.logout()

// Login to the main account using the loginKey:
const topicAccount = await context.loginWithKey('new-user', loginKey)
expect(topicAccount.isDuressAccount).equals(false)
})

it('check password', async function () {
const world = await makeFakeEdgeWorld([fakeUser], quiet)
const context = await world.makeEdgeContext(contextOptions)
Expand Down
Loading