Skip to content

Commit

Permalink
chore: remove phased out storage encryption policy (#2323)
Browse files Browse the repository at this point in the history
  • Loading branch information
moughxyz committed May 3, 2023
1 parent e3f8b81 commit 9f937f2
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 235 deletions.
1 change: 0 additions & 1 deletion packages/services/src/Domain/Storage/StorageKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export enum StorageKey {
ProtectionExpirey = 'SessionExpiresAtKey',
ProtectionSessionLength = 'SessionLengthKey',
KeyRecoveryUndecryptableItems = 'key_recovery_undecryptable',
StorageEncryptionPolicy = 'storage_policy',
WebSocketUrl = 'webSocket_url',
UserRoles = 'user_roles',
OfflineUserRoles = 'offline_user_roles',
Expand Down
7 changes: 1 addition & 6 deletions packages/services/src/Domain/Storage/StorageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ export enum StoragePersistencePolicies {
Ephemeral = 2,
}

export enum StorageEncryptionPolicy {
Default = 1,
Disabled = 2,
}

export enum StorageValueModes {
/** Stored inside wrapped encrpyed storage object */
/** Stored inside wrapped encrypted storage object */
Default = 1,
/** Stored outside storage object, unencrypted */
Nonwrapped = 2,
Expand Down
145 changes: 0 additions & 145 deletions packages/snjs/lib/Application/Application.spec.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/snjs/lib/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,6 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
return this.userService.changePasscode(newPasscode, origination)
}

public getStorageEncryptionPolicy(): ExternalServices.StorageEncryptionPolicy {
return this.diskStorageService.getStorageEncryptionPolicy()
}

public setStorageEncryptionPolicy(encryptionPolicy: ExternalServices.StorageEncryptionPolicy): Promise<void> {
this.diskStorageService.setEncryptionPolicy(encryptionPolicy)
return this.protocolService.repersistAllItems()
}

public enableEphemeralPersistencePolicy(): Promise<void> {
return this.diskStorageService.setPersistencePolicy(ExternalServices.StoragePersistencePolicies.Ephemeral)
}
Expand Down Expand Up @@ -1527,7 +1518,6 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli
this.diskStorageService = new InternalServices.DiskStorageService(
this.deviceInterface,
this.identifier,
this.environment,
this.internalEventBus,
)
this.services.push(this.diskStorageService)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { DiskStorageService } from './DiskStorageService'

import { InternalEventBus, DeviceInterface, InternalEventBusInterface } from '@standardnotes/services'
import { Environment } from '@standardnotes/models'

describe('diskStorageService', () => {
let storageService: DiskStorageService
Expand All @@ -12,7 +10,7 @@ describe('diskStorageService', () => {
internalEventBus = {} as jest.Mocked<InternalEventBus>
device = {} as jest.Mocked<DeviceInterface>

storageService = new DiskStorageService(device, 'test', Environment.Desktop, internalEventBus)
storageService = new DiskStorageService(device, 'test', internalEventBus)
})

it('setInitialValues should set unwrapped values as wrapped value if wrapped value is not encrypted', async () => {
Expand Down
58 changes: 11 additions & 47 deletions packages/snjs/lib/Services/Storage/DiskStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
DeletedPayloadInterface,
PayloadTimestampDefaults,
LocalStorageEncryptedContextualPayload,
Environment,
FullyFormedTransferPayload,
} from '@standardnotes/models'

Expand All @@ -37,7 +36,6 @@ export class DiskStorageService extends Services.AbstractService implements Serv
private encryptionProvider!: Encryption.EncryptionProviderInterface
private storagePersistable = false
private persistencePolicy!: Services.StoragePersistencePolicies
private encryptionPolicy!: Services.StorageEncryptionPolicy
private needsPersist = false
private currentPersistPromise?: Promise<Services.StorageValuesObject>

Expand All @@ -46,12 +44,10 @@ export class DiskStorageService extends Services.AbstractService implements Serv
constructor(
private deviceInterface: Services.DeviceInterface,
private identifier: string,
private environment: Environment,
protected override internalEventBus: Services.InternalEventBusInterface,
) {
super(internalEventBus)
void this.setPersistencePolicy(Services.StoragePersistencePolicies.Default)
void this.setEncryptionPolicy(Services.StorageEncryptionPolicy.Default, false)
}

public provideEncryptionProvider(provider: Encryption.EncryptionProviderInterface): void {
Expand All @@ -73,11 +69,6 @@ export class DiskStorageService extends Services.AbstractService implements Serv
if (this.needsPersist) {
void this.persistValuesToDisk()
}
} else if (stage === Services.ApplicationStage.StorageDecrypted_09) {
const persistedPolicy = await this.getValue(Services.StorageKey.StorageEncryptionPolicy)
if (persistedPolicy) {
void this.setEncryptionPolicy(persistedPolicy as Services.StorageEncryptionPolicy, false)
}
}
}

Expand All @@ -90,21 +81,6 @@ export class DiskStorageService extends Services.AbstractService implements Serv
}
}

public setEncryptionPolicy(encryptionPolicy: Services.StorageEncryptionPolicy, persist = true): void {
if (
encryptionPolicy === Services.StorageEncryptionPolicy.Disabled &&
![Environment.Mobile].includes(this.environment)
) {
throw Error('Disabling storage encryption is only available on mobile.')
}

this.encryptionPolicy = encryptionPolicy

if (persist) {
this.setValue(Services.StorageKey.StorageEncryptionPolicy, encryptionPolicy)
}
}

public isEphemeralSession() {
return this.persistencePolicy === Services.StoragePersistencePolicies.Ephemeral
}
Expand Down Expand Up @@ -329,10 +305,6 @@ export class DiskStorageService extends Services.AbstractService implements Serv
}
}

public getStorageEncryptionPolicy() {
return this.encryptionPolicy
}

/**
* Default persistence key. Platforms can override as needed.
*/
Expand Down Expand Up @@ -393,36 +365,29 @@ export class DiskStorageService extends Services.AbstractService implements Serv

const { encrypted, decrypted, deleted, discardable } = CreatePayloadSplitWithDiscardables(payloads)

const encryptionEnabled = this.encryptionPolicy === Services.StorageEncryptionPolicy.Default
const rootKeyEncryptionAvailable = this.encryptionProvider.hasRootKeyEncryptionSource()

const encryptable: DecryptedPayloadInterface[] = []
const unencryptable: DecryptedPayloadInterface[] = []

if (encryptionEnabled) {
const split = Encryption.SplitPayloadsByEncryptionType(decrypted)

if (split.itemsKeyEncryption) {
extendArray(encryptable, split.itemsKeyEncryption)
}
const split = Encryption.SplitPayloadsByEncryptionType(decrypted)
if (split.itemsKeyEncryption) {
extendArray(encryptable, split.itemsKeyEncryption)
}

if (split.rootKeyEncryption) {
if (!rootKeyEncryptionAvailable) {
extendArray(unencryptable, split.rootKeyEncryption)
} else {
extendArray(encryptable, split.rootKeyEncryption)
}
if (split.rootKeyEncryption) {
if (!rootKeyEncryptionAvailable) {
extendArray(unencryptable, split.rootKeyEncryption)
} else {
extendArray(encryptable, split.rootKeyEncryption)
}
} else {
extendArray(unencryptable, encryptable)
extendArray(unencryptable, decrypted)
}

await this.deletePayloads(discardable)

const split = Encryption.SplitPayloadsByEncryptionType(encryptable)
const encryptableSplit = Encryption.SplitPayloadsByEncryptionType(encryptable)

const keyLookupSplit = Encryption.CreateEncryptionSplitWithKeyLookup(split)
const keyLookupSplit = Encryption.CreateEncryptionSplitWithKeyLookup(encryptableSplit)

const encryptedResults = await this.encryptionProvider.encryptSplit(keyLookupSplit)

Expand Down Expand Up @@ -478,7 +443,6 @@ export class DiskStorageService extends Services.AbstractService implements Serv
storage: {
storagePersistable: this.storagePersistable,
persistencePolicy: Services.StoragePersistencePolicies[this.persistencePolicy],
encryptionPolicy: Services.StorageEncryptionPolicy[this.encryptionPolicy],
needsPersist: this.needsPersist,
currentPersistPromise: this.currentPersistPromise != undefined,
isStorageWrapped: this.isStorageWrapped(),
Expand Down
22 changes: 0 additions & 22 deletions packages/snjs/mocha/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,28 +223,6 @@ describe('storage manager', function () {
expect(decrypted.content).to.be.an.instanceof(Object)
})

/** @TODO: Storage encryption disable is no longer available, remove tests and associated functionality */
it.skip('disabling storage encryption should store items without encryption', async function () {
await Factory.registerUserToApplication({
application: this.application,
email: this.email,
password: this.password,
ephemeral: false,
})

await this.application.setStorageEncryptionPolicy(StorageEncryptionPolicy.Disabled)

const payloads = await this.application.diskStorageService.getAllRawPayloads()
const payload = payloads[0]
expect(typeof payload.content).to.not.equal('string')
expect(payload.content.references).to.be.ok

const identifier = this.application.identifier

const app = await Factory.createAndInitializeApplication(identifier, Environment.Mobile)
expect(app.diskStorageService.encryptionPolicy).to.equal(StorageEncryptionPolicy.Disabled)
})

it('stored payloads should not contain metadata fields', async function () {
await this.application.addPasscode('123')
await Factory.createSyncedNote(this.application)
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"lint:eslint": "eslint --ext .ts lib/",
"lint:fix": "eslint --fix --ext .ts lib/",
"lint:tsc": "tsc --noEmit --emitDeclarationOnly false --project lib/tsconfig.json",
"test": "jest --coverage",
"test": "jest",
"test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
},
"devDependencies": {
Expand Down

0 comments on commit 9f937f2

Please sign in to comment.