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
42 changes: 42 additions & 0 deletions eslint-local-rules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com */
const COPYRIGHT_HEADER = '/* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com */';

const DO_USE_LOOPS = `

There should be only one obvious way to do a thing.
Expand All @@ -22,6 +24,46 @@ const DO_NOT_USE_EACH = `Using .each or .forEach for looping is heavily discoura
const DO_NOT_USE_MAP_EXPR_STMT = 'Use .map() when you want to transform an array,' + ` not as a substitute for loops. ${DO_USE_LOOPS}`;

module.exports = {
'copyright-header': {
meta: {
type: 'layout',
fixable: 'whitespace',
schema: [],
messages: {
required: 'Expected the FlowCrypt copyright header followed by a newline.',
},
},
create: context => {
const { sourceCode } = context;
return {
Program: () => {
const { text } = sourceCode;
const shebang = text.match(/^#![^\r\n]*(?:\r?\n|$)/)?.[0] ?? '';
const headerStart = shebang.length;
const headerLine = shebang ? 2 : 1;
const firstComment = sourceCode.getAllComments().find(comment => comment.type !== 'Shebang');
const isLeadingComment = firstComment?.loc.start.line === headerLine && !text.slice(headerStart, firstComment.range[0]).trim();
// Preserve leading comments that are unrelated to the copyright header.
const isCopyrightHeader = isLeadingComment && /^[\s*]*(?:©|copyright\b).*\bFlowCrypt\b/is.test(firstComment.value);
const range = isCopyrightHeader ? firstComment.range : [headerStart, headerStart];
const [start, end] = range;
const hasNewline = /^\r?\n/.test(text.slice(end));
if (hasNewline && isCopyrightHeader && sourceCode.getText(firstComment) === COPYRIGHT_HEADER) {
return;
}

const newline = text.match(/\r?\n/)?.[0] ?? '\n';
const before = shebang && !shebang.endsWith('\n') ? newline : '';
const after = hasNewline ? '' : newline;
context.report({
loc: { start: sourceCode.getLocFromIndex(start), end: sourceCode.getLocFromIndex(end) },
messageId: 'required',
fix: fixer => fixer.replaceTextRange(range, before + COPYRIGHT_HEADER + after),
});
},
};
},
},
'standard-loops': {
meta: {
docs: {
Expand Down
9 changes: 1 addition & 8 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import tseslint from 'typescript-eslint';
import noOnlyTestsPlugin from 'eslint-plugin-no-only-tests';
import headerPlugin from '@tony.ganchev/eslint-plugin-header';
import jsdocPlugin from 'eslint-plugin-jsdoc';
import preferArrowPlugin from 'eslint-plugin-prefer-arrow';
import noNullPlugin from 'eslint-plugin-no-null';
import localRulesPlugin from 'eslint-plugin-local-rules';
Expand All @@ -24,12 +22,8 @@ const jsConfigRules = {
'dot-notation': 'error',
eqeqeq: ['error', 'smart'],
'guard-for-in': 'error',
'header/header': ['error', 'block', ' ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com '],
'id-denylist': 'error',
'id-match': 'error',
'jsdoc/check-alignment': 'off',
'jsdoc/check-indentation': 'off',
'jsdoc/newline-after-description': 'off',
'max-classes-per-file': 'off',
'no-bitwise': 'off',
'no-caller': 'error',
Expand Down Expand Up @@ -70,12 +64,11 @@ const jsConfigRules = {
radix: 'off',
'require-atomic-updates': 0,
'sort-imports': 'off',
'local-rules/copyright-header': 'error',
'local-rules/standard-loops': 'error',
};

const basePlugins = {
header: headerPlugin,
jsdoc: jsdocPlugin,
'prefer-arrow': preferArrowPlugin,
'no-null': noNullPlugin,
'local-rules': localRulesPlugin,
Expand Down
24 changes: 17 additions & 7 deletions extension/js/common/core/crypto/pgp/openpgp-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ export class OpenPGPKey {
userIDs: { email: string | undefined; name: string }[],
expireSeconds: number
) {
const opgpPrv = (await OpenPGPKey.extractExternalLibraryObjFromKey(privateKey)) as OpenPGP.PrivateKey; // todo: check isPrivate()?
const opgpPrv = await OpenPGPKey.extractExternalLibraryObjFromKey(privateKey);
if (!opgpPrv.isPrivate()) {
throw new UnexpectedKeyTypeError('Cannot reformat a public key');
}
const keyPair = await opgp.reformatKey({
privateKey: opgpPrv,
passphrase,
Expand Down Expand Up @@ -248,7 +251,10 @@ export class OpenPGPKey {
* Returns signature if detached=true, armored
*/
public static async sign(signingPrivate: Key, text: string, detached = false): Promise<string> {
const signingPrv = (await OpenPGPKey.extractExternalLibraryObjFromKey(signingPrivate)) as OpenPGP.PrivateKey; // todo: throw?
const signingPrv = await OpenPGPKey.extractExternalLibraryObjFromKey(signingPrivate);
if (!signingPrv.isPrivate()) {
throw new UnexpectedKeyTypeError('Cannot sign with a public key');
}
if (detached) {
const message = await opgp.createMessage({ text });
return await opgp.sign({ message, format: 'armored', signingKeys: [signingPrv], detached });
Expand Down Expand Up @@ -692,19 +698,23 @@ export class OpenPGPKey {
throw new Error('No valid signature found in key');
}

private static async extractExternalLibraryObjFromKey(key: Key): Promise<OpenPGP.Key> {
private static async extractExternalLibraryObjFromKey(key: Key): Promise<OpenPGP.PublicKey> {
if (key.family !== 'openpgp') {
throw new UnexpectedKeyTypeError(`Key type is ${key.family}, expecting OpenPGP`);
}
const keyWithPrivateFields = key as KeyWithPrivateFields;
const internal = keyWithPrivateFields.internal;
if (!internal) {
throw new Error('Object has type == "openpgp" but no internal key.');
} else if (typeof internal === 'string') {
keyWithPrivateFields.internal = await opgp.readKey({ binaryKey: Buf.fromRawBytesStr(internal) });
return keyWithPrivateFields.internal;
}
return internal;
const parsed = typeof internal === 'string' ? await opgp.readKey({ binaryKey: Buf.fromRawBytesStr(internal) }) : internal;
if (!(parsed instanceof opgp.PublicKey)) {
throw new UnexpectedKeyTypeError('Expected an OpenPGP key instance');
}
if (typeof internal === 'string') {
keyWithPrivateFields.internal = parsed;
}
return parsed;
}

private static getSigningAndEncryptionKeys = async (key: OpenPGP.Key) => {
Expand Down
2 changes: 1 addition & 1 deletion extension/js/common/platform/store/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class GlobalStore extends AbstractStore {

public static async get(keys: GlobalIndex[]): Promise<GlobalStoreDict> {
const storageObj = (await storageGet('local', GlobalStore.singleScopeRawIndexArr(GlobalStore.globalStorageScope, keys))) as RawStore;
return GlobalStore.buildSingleAccountStoreFromRawResults(GlobalStore.globalStorageScope, storageObj) as GlobalStore;
return GlobalStore.buildSingleAccountStoreFromRawResults(GlobalStore.globalStorageScope, storageObj) as GlobalStoreDict;
}

public static async remove(keys: string[]) {
Expand Down
Loading