Skip to content
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

[PM-3660] Address PR feedback #6157

4 changes: 4 additions & 0 deletions apps/browser/src/platform/browser/browser-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable } from "rxjs";

Check notice on line 1 in apps/browser/src/platform/browser/browser-api.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (EC-598-beeep-properly-store-passkeys-in-bitwarden)

ℹ Getting worse: Primitive Obsession

The ratio of primitive types in function arguments increases from 40.74% to 42.86%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.

import { DeviceType } from "@bitwarden/common/enums";

Expand Down Expand Up @@ -37,6 +37,10 @@
);
}

static async removeWindow(windowId: number) {
await chrome.tabs.remove(windowId);
}

Comment on lines +40 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR was merged with this bug, causing the window to not close. This was fixed with a separate commit straight to the feature branch

static async getTabFromCurrentWindowId(): Promise<chrome.tabs.Tab> | null {
return await BrowserApi.tabsQueryFirst({
active: true,
Expand Down
108 changes: 52 additions & 56 deletions apps/browser/src/popup/services/popup-utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,69 +55,65 @@ export class PopupUtilsService {
}
}

popOut(win: Window, href: string = null, options: { center?: boolean } = {}): Promise<Popout> {
return new Promise((resolve, reject) => {
if (href === null) {
href = win.location.href;
}
async popOut(
win: Window,
href: string = null,
options: { center?: boolean } = {}
): Promise<Popout> {
if (href === null) {
href = win.location.href;
}

if (typeof chrome !== "undefined" && chrome.windows && chrome.windows.create) {
if (href.indexOf("?uilocation=") > -1) {
href = href
.replace("uilocation=popup", "uilocation=popout")
.replace("uilocation=tab", "uilocation=popout")
.replace("uilocation=sidebar", "uilocation=popout");
} else {
const hrefParts = href.split("#");
href =
hrefParts[0] + "?uilocation=popout" + (hrefParts.length > 0 ? "#" + hrefParts[1] : "");
}

const bodyRect = document.querySelector("body").getBoundingClientRect();
const width = Math.round(bodyRect.width ? bodyRect.width + 60 : 375);
const height = Math.round(bodyRect.height || 600);
const top = options.center ? Math.round((screen.height - height) / 2) : undefined;
const left = options.center ? Math.round((screen.width - width) / 2) : undefined;
chrome.windows.create(
{
url: href,
type: "popup",
width,
height,
top,
left,
},
(window) => resolve({ type: "window", window })
);

if (win && this.inPopup(win)) {
BrowserApi.closePopup(win);
}
} else if (typeof chrome !== "undefined" && chrome.tabs && chrome.tabs.create) {
if (chrome?.windows?.create != undefined) {
coroiu marked this conversation as resolved.
Show resolved Hide resolved
if (href.indexOf("?uilocation=") > -1) {
href = href
.replace("uilocation=popup", "uilocation=tab")
.replace("uilocation=popout", "uilocation=tab")
.replace("uilocation=sidebar", "uilocation=tab");
chrome.tabs.create(
{
url: href,
},
(tab) => resolve({ type: "tab", tab })
);
.replace("uilocation=popup", "uilocation=popout")
.replace("uilocation=tab", "uilocation=popout")
.replace("uilocation=sidebar", "uilocation=popout");
} else {
reject(new Error("Cannot open tab or window"));
const hrefParts = href.split("#");
href =
hrefParts[0] + "?uilocation=popout" + (hrefParts.length > 0 ? "#" + hrefParts[1] : "");
}
});

const bodyRect = document.querySelector("body").getBoundingClientRect();
const width = Math.round(bodyRect.width ? bodyRect.width + 60 : 375);
const height = Math.round(bodyRect.height || 600);
const top = options.center ? Math.round((screen.height - height) / 2) : undefined;
const left = options.center ? Math.round((screen.width - width) / 2) : undefined;
const window = await BrowserApi.createWindow({
url: href,
type: "popup",
width,
height,
top,
left,
});

if (win && this.inPopup(win)) {
BrowserApi.closePopup(win);
}

return { type: "window", window };
} else if (chrome?.tabs?.create != undefined) {
coroiu marked this conversation as resolved.
Show resolved Hide resolved
href = href
.replace("uilocation=popup", "uilocation=tab")
.replace("uilocation=popout", "uilocation=tab")
.replace("uilocation=sidebar", "uilocation=tab");

const tab = await BrowserApi.createNewTab(href);
return { type: "tab", tab };
} else {
throw new Error("Cannot open tab or window");
}
}

closePopOut(popout: Popout): Promise<void> {
return new Promise((resolve) => {
if (popout.type === "window") {
chrome.windows.remove(popout.window.id, resolve);
} else {
chrome.tabs.remove(popout.tab.id, resolve);
}
});
if (popout.type === "window") {
coroiu marked this conversation as resolved.
Show resolved Hide resolved
return BrowserApi.removeWindow(popout.window.id);
} else {
return BrowserApi.removeTab(popout.tab.id);
}
}

/**
Expand Down
98 changes: 0 additions & 98 deletions libs/common/src/platform/misc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-useless-escape */

Check notice on line 1 in libs/common/src/platform/misc/utils.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (EC-598-beeep-properly-store-passkeys-in-bitwarden)

ℹ Getting worse: Primitive Obsession

The ratio of primitive types in function arguments increases from 70.00% to 71.05%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.

Check notice on line 1 in libs/common/src/platform/misc/utils.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (EC-598-beeep-properly-store-passkeys-in-bitwarden)

ℹ Getting worse: String Heavy Function Arguments

The ratio of strings in function arguments increases from 67.50% to 68.42%, threshold = 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.
import * as path from "path";

import { Observable, of, switchMap } from "rxjs";
Expand Down Expand Up @@ -37,9 +37,6 @@
["google.com", new Set(["script.google.com"])],
]);

/** Used by guidToStandardFormat */
private static byteToHex: string[] = [];

static init() {
if (Utils.inited) {
return;
Expand All @@ -62,11 +59,7 @@
} else {
// If it's not browser or node then it must be a service worker
Utils.global = self;
}

Check notice on line 62 in libs/common/src/platform/misc/utils.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (EC-598-beeep-properly-store-passkeys-in-bitwarden)

✅ No longer an issue: Complex Method

Utils.init is no longer above the threshold for cyclomatic complexity. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

for (let i = 0; i < 256; ++i) {
Utils.byteToHex.push((i + 0x100).toString(16).substring(1));
}
}

static fromB64ToArray(str: string): Uint8Array {
Expand Down Expand Up @@ -584,97 +577,6 @@

return null;
}

/*
License for: guidToRawFormat, guidToStandardFormat
Source: https://github.com/uuidjs/uuid/
The MIT License (MIT)
Copyright (c) 2010-2020 Robert Kieffer and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/

/** Convert standard format (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) UUID to raw 16 byte array. */
static guidToRawFormat(guid: string) {
if (!Utils.isGuid(guid)) {
throw TypeError("GUID parameter is invalid");
}

let v;
const arr = new Uint8Array(16);

// Parse ########-....-....-....-............
arr[0] = (v = parseInt(guid.slice(0, 8), 16)) >>> 24;
arr[1] = (v >>> 16) & 0xff;
arr[2] = (v >>> 8) & 0xff;
arr[3] = v & 0xff;

// Parse ........-####-....-....-............
arr[4] = (v = parseInt(guid.slice(9, 13), 16)) >>> 8;
arr[5] = v & 0xff;

// Parse ........-....-####-....-............
arr[6] = (v = parseInt(guid.slice(14, 18), 16)) >>> 8;
arr[7] = v & 0xff;

// Parse ........-....-....-####-............
arr[8] = (v = parseInt(guid.slice(19, 23), 16)) >>> 8;
arr[9] = v & 0xff;

// Parse ........-....-....-....-############
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
arr[10] = ((v = parseInt(guid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
arr[11] = (v / 0x100000000) & 0xff;
arr[12] = (v >>> 24) & 0xff;
arr[13] = (v >>> 16) & 0xff;
arr[14] = (v >>> 8) & 0xff;
arr[15] = v & 0xff;

return arr;
}

/** Convert raw 16 byte array to standard format (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) UUID. */
static guidToStandardFormat(bufferSource: BufferSource) {
const arr =
bufferSource instanceof ArrayBuffer
? new Uint8Array(bufferSource)
: new Uint8Array(bufferSource.buffer);
// Note: Be careful editing this code! It's been tuned for performance
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
const guid = (
Utils.byteToHex[arr[0]] +
Utils.byteToHex[arr[1]] +
Utils.byteToHex[arr[2]] +
Utils.byteToHex[arr[3]] +
"-" +
Utils.byteToHex[arr[4]] +
Utils.byteToHex[arr[5]] +
"-" +
Utils.byteToHex[arr[6]] +
Utils.byteToHex[arr[7]] +
"-" +
Utils.byteToHex[arr[8]] +
Utils.byteToHex[arr[9]] +
"-" +
Utils.byteToHex[arr[10]] +
Utils.byteToHex[arr[11]] +
Utils.byteToHex[arr[12]] +
Utils.byteToHex[arr[13]] +
Utils.byteToHex[arr[14]] +
Utils.byteToHex[arr[15]]
).toLowerCase();

// Consistency check for valid UUID. If this throws, it's likely due to one
// of the following:
// - One or more input array values don't map to a hex octet (leading to
// "undefined" in the uuid)
// - Invalid input values for the RFC `version` or `variant` fields
if (!Utils.isGuid(guid)) {
throw TypeError("Converted GUID is invalid");
}

return guid;
}
}

Utils.init();
2 changes: 0 additions & 2 deletions libs/common/src/vault/api/fido2-key.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export class Fido2KeyApi extends BaseResponse {
rpId: string;
userHandle: string;
counter: string;

// Extras
rpName: string;
userDisplayName: string;

Expand Down
2 changes: 0 additions & 2 deletions libs/common/src/vault/models/data/fido2-key.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export class Fido2KeyData {
rpId: string;
userHandle: string;
counter: string;

// Extras
rpName: string;
userDisplayName: string;

Expand Down
Loading