1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-06 09:20:43 +01:00

Add send to cli (#253)

* Upgrade commander to 7.0.0

* Add url to Api call

This is needed to allow access to sends that are available from a
different Bitwarden server than configured for the CLI

* Allow upload of send files from CLI

* Allow send search by accessId

* Utils methods used in Send CLI implementation

* Revert adding string type to encrypted file data

* linter fixes

* Add Buffer to ArrayBuffer used in CLI send implementation
This commit is contained in:
Matt Gibson 2021-01-29 15:08:52 -06:00 committed by GitHub
parent 06239aea2d
commit 09c444ddd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 424 additions and 191 deletions

526
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -79,7 +79,7 @@
"big-integer": "1.6.36",
"browser-hrtime": "^1.1.8",
"chalk": "2.4.1",
"commander": "2.18.0",
"commander": "7.0.0",
"core-js": "2.6.2",
"duo_web_sdk": "git+https://github.com/duosecurity/duo_web_sdk.git",
"electron-log": "4.3.0",

View File

@ -174,7 +174,7 @@ export abstract class ApiService {
deleteFolder: (id: string) => Promise<any>;
getSend: (id: string) => Promise<SendResponse>;
postSendAccess: (id: string, request: SendAccessRequest) => Promise<SendAccessResponse>;
postSendAccess: (id: string, request: SendAccessRequest, apiUrl?: string) => Promise<SendAccessResponse>;
getSends: () => Promise<ListResponse<SendResponse>>;
postSend: (request: SendRequest) => Promise<SendResponse>;
postSendFile: (data: FormData) => Promise<SendResponse>;

View File

@ -9,7 +9,7 @@ export abstract class SendService {
decryptedSendCache: SendView[];
clearCache: () => void;
encrypt: (model: SendView, file: File, password: string, key?: SymmetricCryptoKey) => Promise<[Send, ArrayBuffer]>;
encrypt: (model: SendView, file: File | ArrayBuffer, password: string, key?: SymmetricCryptoKey) => Promise<[Send, ArrayBuffer]>;
get: (id: string) => Promise<Send>;
getAll: () => Promise<Send[]>;
getAllDecrypted: () => Promise<SendView[]>;

View File

@ -41,7 +41,7 @@ export class LoginCommand {
this.clientId = clientId;
}
async run(email: string, password: string, cmd: program.Command) {
async run(email: string, password: string, options: program.OptionValues) {
this.canInteract = process.env.BW_NOINTERACTION !== 'true';
let ssoCodeVerifier: string = null;
@ -50,7 +50,7 @@ export class LoginCommand {
let clientId: string = null;
let clientSecret: string = null;
if (cmd.apikey != null) {
if (options.apikey != null) {
const storedClientId: string = process.env.BW_CLIENTID;
const storedClientSecret: string = process.env.BW_CLIENTSECRET;
if (storedClientId == null) {
@ -77,7 +77,7 @@ export class LoginCommand {
} else {
clientSecret = storedClientSecret;
}
} else if (cmd.sso != null && this.canInteract) {
} else if (options.sso != null && this.canInteract) {
const passwordOptions: any = {
type: 'password',
length: 64,
@ -112,10 +112,10 @@ export class LoginCommand {
}
if (password == null || password === '') {
if (cmd.passwordfile) {
password = await NodeUtils.readFirstLine(cmd.passwordfile);
} else if (cmd.passwordenv && process.env[cmd.passwordenv]) {
password = process.env[cmd.passwordenv];
if (options.passwordfile) {
password = await NodeUtils.readFirstLine(options.passwordfile);
} else if (options.passwordenv && process.env[options.passwordenv]) {
password = process.env[options.passwordenv];
} else if (this.canInteract) {
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
type: 'password',
@ -131,11 +131,11 @@ export class LoginCommand {
}
}
let twoFactorToken: string = cmd.code;
let twoFactorToken: string = options.code;
let twoFactorMethod: TwoFactorProviderType = null;
try {
if (cmd.method != null) {
twoFactorMethod = parseInt(cmd.method, null);
if (options.method != null) {
twoFactorMethod = parseInt(options.method, null);
}
} catch (e) {
return Response.error('Invalid two-step login method.');
@ -185,18 +185,18 @@ export class LoginCommand {
if (twoFactorProviders.length === 1) {
selectedProvider = twoFactorProviders[0];
} else if (this.canInteract) {
const options = twoFactorProviders.map((p) => p.name);
options.push(new inquirer.Separator());
options.push('Cancel');
const twoFactorOptions = twoFactorProviders.map((p) => p.name);
twoFactorOptions.push(new inquirer.Separator());
twoFactorOptions.push('Cancel');
const answer: inquirer.Answers =
await inquirer.createPromptModule({ output: process.stderr })({
type: 'list',
name: 'method',
message: 'Two-step login method:',
choices: options,
choices: twoFactorOptions,
});
const i = options.indexOf(answer.method);
if (i === (options.length - 1)) {
const i = twoFactorOptions.indexOf(answer.method);
if (i === (twoFactorOptions.length - 1)) {
return Response.error('Login failed.');
}
selectedProvider = twoFactorProviders[i];

View File

@ -15,7 +15,7 @@ export class UpdateCommand {
this.inPkg = !!(process as any).pkg;
}
async run(cmd: program.Command): Promise<Response> {
async run(): Promise<Response> {
const currentVersion = this.platformUtilsService.getApplicationVersion();
const response = await fetch.default('https://api.github.com/repos/bitwarden/' +

View File

@ -26,4 +26,9 @@ export class NodeUtils {
.on('error', (err) => reject(err));
});
}
// https://stackoverflow.com/a/31394257
static bufferToArrayBuffer(buf: Buffer): ArrayBuffer {
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}
}

View File

@ -273,6 +273,14 @@ export class Utils {
return str == null || typeof str !== 'string' || str.trim() === '';
}
static nameOf<T>(name: string & keyof T) {
return name;
}
static assign<T>(target: T, source: Partial<T>): T {
return Object.assign(target, source);
}
private static validIpAddress(ipString: string): boolean {
// tslint:disable-next-line
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

View File

@ -73,6 +73,7 @@ import { VerifyBankRequest } from '../models/request/verifyBankRequest';
import { VerifyDeleteRecoverRequest } from '../models/request/verifyDeleteRecoverRequest';
import { VerifyEmailRequest } from '../models/request/verifyEmailRequest';
import { Utils } from '../misc/utils';
import { ApiKeyResponse } from '../models/response/apiKeyResponse';
import { BillingResponse } from '../models/response/billingResponse';
import { BreachAccountResponse } from '../models/response/breachAccountResponse';
@ -410,8 +411,8 @@ export class ApiService implements ApiServiceAbstraction {
return new SendResponse(r);
}
async postSendAccess(id: string, request: SendAccessRequest): Promise<SendAccessResponse> {
const r = await this.send('POST', '/sends/access/' + id, request, false, true);
async postSendAccess(id: string, request: SendAccessRequest, apiUrl?: string): Promise<SendAccessResponse> {
const r = await this.send('POST', '/sends/access/' + id, request, false, true, apiUrl);
return new SendAccessResponse(r);
}
@ -1210,7 +1211,8 @@ export class ApiService implements ApiServiceAbstraction {
}
private async send(method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, body: any,
authed: boolean, hasResponse: boolean): Promise<any> {
authed: boolean, hasResponse: boolean, apiUrl?: string): Promise<any> {
apiUrl = Utils.isNullOrWhitespace(apiUrl) ? this.apiBaseUrl : apiUrl;
const headers = new Headers({
'Device-Type': this.deviceType,
});
@ -1246,7 +1248,7 @@ export class ApiService implements ApiServiceAbstraction {
}
requestInit.headers = headers;
const response = await this.fetch(new Request(this.apiBaseUrl + path, requestInit));
const response = await this.fetch(new Request(apiUrl + path, requestInit));
if (hasResponse && response.status === 200) {
const responseJson = await response.json();

View File

@ -169,7 +169,7 @@ export class SearchService implements SearchServiceAbstraction {
if (s.name != null && s.name.toLowerCase().indexOf(query) > -1) {
return true;
}
if (query.length >= 8 && (s.id.startsWith(query) || (s.file?.id != null && s.file.id.startsWith(query)))) {
if (query.length >= 8 && (s.id.startsWith(query) || s.accessId.toLocaleLowerCase().startsWith(query) || (s.file?.id != null && s.file.id.startsWith(query)))) {
return true;
}
if (s.notes != null && s.notes.toLowerCase().indexOf(query) > -1) {

View File

@ -22,6 +22,7 @@ import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service';
import { Utils } from '../misc/utils';
import { CipherString } from '../models/domain';
const Keys = {
sendsPrefix: 'sends_',
@ -38,7 +39,7 @@ export class SendService implements SendServiceAbstraction {
this.decryptedSendCache = null;
}
async encrypt(model: SendView, file: File, password: string,
async encrypt(model: SendView, file: File | ArrayBuffer, password: string,
key?: SymmetricCryptoKey): Promise<[Send, ArrayBuffer]> {
let fileData: ArrayBuffer = null;
const send = new Send();
@ -64,7 +65,13 @@ export class SendService implements SendServiceAbstraction {
} else if (send.type === SendType.File) {
send.file = new SendFile();
if (file != null) {
fileData = await this.parseFile(send, file, model.cryptoKey);
if (file instanceof ArrayBuffer) {
const [name, data] = await this.encryptFileData(model.file.fileName, file, model.cryptoKey);
send.file.fileName = name;
fileData = data;
} else {
fileData = await this.parseFile(send, file, model.cryptoKey);
}
}
}
@ -227,9 +234,9 @@ export class SendService implements SendServiceAbstraction {
reader.readAsArrayBuffer(file);
reader.onload = async (evt) => {
try {
send.file.fileName = await this.cryptoService.encrypt(file.name, key);
const fileData = await this.cryptoService.encryptToBytes(evt.target.result as ArrayBuffer, key);
resolve(fileData);
const [name, data] = await this.encryptFileData(file.name, evt.target.result as ArrayBuffer, key);
send.file.fileName = name;
resolve(data);
} catch (e) {
reject(e);
}
@ -239,4 +246,11 @@ export class SendService implements SendServiceAbstraction {
};
});
}
private async encryptFileData(fileName: string, data: ArrayBuffer,
key: SymmetricCryptoKey): Promise<[CipherString, ArrayBuffer]> {
const encFileName = await this.cryptoService.encrypt(fileName, key);
const encFileData = await this.cryptoService.encryptToBytes(data, key);
return [encFileName, encFileData];
}
}