Skip to content
Draft
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
50 changes: 50 additions & 0 deletions libs/common/src/vault/models/view/login-uri-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,56 @@ describe("LoginUriView", () => {
expect(actual).toBe(false);
});
});

describe("using a desktop app uri", () => {
const pairingUri = "desktopapp://autotype?version=1.0.0&data=e30";

it.each([
UriMatchStrategy.Domain,
UriMatchStrategy.Host,
UriMatchStrategy.Exact,
UriMatchStrategy.StartsWith,
UriMatchStrategy.RegularExpression,
])("does not match its own host with match strategy %s", (match) => {
const uri = uriFactory(match, pairingUri);

expect(uri.matchesUri("https://autotype/", new Set())).toBe(false);
expect(uri.matchesUri("http://autotype/login", new Set())).toBe(false);
});

it("does not match when it uses the default match strategy and the default is Host", () => {
const uri = uriFactory(null, pairingUri);

expect(uri.matchesUri("https://autotype/", new Set(), UriMatchStrategy.Host)).toBe(false);
});

it("does not match when the Never match strategy is overridden", () => {
const uri = uriFactory(UriMatchStrategy.Never, pairingUri);

expect(uri.matchesUri("https://autotype/", new Set(), undefined, true)).toBe(false);
});

it("does not match itself", () => {
const uri = uriFactory(UriMatchStrategy.Exact, pairingUri);

expect(uri.matchesUri(pairingUri, new Set())).toBe(false);
});

it.each(["DESKTOPAPP://autotype?version=1.0.0&data=e30", ` ${pairingUri}`])(
"does not match when the uri is %p",
(uriString) => {
const uri = uriFactory(UriMatchStrategy.Host, uriString);

expect(uri.matchesUri("https://autotype/", new Set())).toBe(false);
},
);

it("still matches website uris that only contain the desktop app scheme later on", () => {
const uri = uriFactory(UriMatchStrategy.Host, "https://example.com/?next=desktopapp://x");

expect(uri.matchesUri("https://example.com/", new Set())).toBe(true);
});
});
});

describe("fromSdkLoginUriView", () => {
Expand Down
7 changes: 7 additions & 0 deletions libs/common/src/vault/models/view/login-uri.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Jsonify } from "type-fest";
import { LoginUriView as SdkLoginUriView } from "@bitwarden/sdk-internal";

import { punycodeToUnicode } from "../../../autofill/utils/punycode";
import { DESKTOP_APP_URI_PREFIX } from "../../../desktop-native/models/desktop-autotype-pairing-uri";
import { UriMatchStrategy, UriMatchStrategySetting } from "../../../models/domain/domain-service";
import { View } from "../../../models/view/view";
import { SafeUrls, UrlType } from "../../../platform/misc/safe-urls";
Expand Down Expand Up @@ -151,6 +152,12 @@ export class LoginUriView implements View {
return false;
}

// A desktop app pairing is never matched against a URL, whatever its match strategy. Every
// pairing has the host `autotype`, so the Host strategy would otherwise match `http(s)://autotype/`.
if (this.uri.trim().toLowerCase().startsWith(DESKTOP_APP_URI_PREFIX)) {
return false;
}

let matchType = this.match ?? defaultUriMatch;
matchType ??= UriMatchStrategy.Domain;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,40 @@ describe("AutofillOptionsComponent", () => {
]);
});

it("loads the desktop app URIs on the cipher with the Never match strategy in the form", () => {
const localFixture = createComponent(
true,
createCipher(["https://example.com", UriMatchStrategy.Exact], [pairingUri, null]),
);

expect(
localFixture.componentInstance.autofillOptionsForm.controls.uris.at(1).value,
).toEqual({ uri: pairingUri, matchDetection: UriMatchStrategy.Never });
expect(httpAuthenticationService.updateUriMatchStrategies).not.toHaveBeenCalledWith([
UriMatchStrategy.Exact,
null,
]);
expect(httpAuthenticationService.updateUriMatchStrategies).toHaveBeenLastCalledWith([
UriMatchStrategy.Exact,
UriMatchStrategy.Never,
]);
});

it("loads the desktop app URIs on the cipher with their match strategy in the form when the feature flag is off", () => {
const localFixture = createComponent(
false,
createCipher(["https://example.com", UriMatchStrategy.Exact], [pairingUri, null]),
);

expect(
localFixture.componentInstance.autofillOptionsForm.controls.uris.at(1).value,
).toEqual({ uri: pairingUri, matchDetection: null });
expect(httpAuthenticationService.updateUriMatchStrategies).toHaveBeenLastCalledWith([
UriMatchStrategy.Exact,
null,
]);
});

describe("when the feature flag is read after the form opens", () => {
let gaFeatureFlag$: Subject<boolean>;

Expand All @@ -839,8 +873,10 @@ describe("AutofillOptionsComponent", () => {

function openForm(...uris: [string, UriMatchStrategySetting][]) {
getInitialCipherView.mockReturnValueOnce(createCipher(...uris));
TestBed.createComponent(AutofillOptionsComponent).detectChanges();
const localFixture = TestBed.createComponent(AutofillOptionsComponent);
localFixture.detectChanges();
cipherFormContainer.patchCipher.mockClear();
return localFixture.componentInstance;
}

it("saves the desktop app URIs on the cipher with the Never match strategy", () => {
Expand All @@ -855,6 +891,40 @@ describe("AutofillOptionsComponent", () => {
]);
});

it("gives the desktop app rows the Never match strategy in the form", () => {
const component = openForm(
["https://example.com", UriMatchStrategy.Exact],
[pairingUri, null],
);

gaFeatureFlag$.next(true);

expect(component.autofillOptionsForm.controls.uris.at(1).value).toEqual({
uri: pairingUri,
matchDetection: UriMatchStrategy.Never,
});
expect(httpAuthenticationService.updateUriMatchStrategies).toHaveBeenLastCalledWith([
UriMatchStrategy.Exact,
UriMatchStrategy.Never,
]);
});

it("keeps the form's match strategy of a desktop app URI typed into a website row, and saves it with Never", () => {
const component = openForm(["https://example.com", UriMatchStrategy.Exact]);
component.autofillOptionsForm.controls.uris
.at(0)
.setValue({ uri: pairingUri, matchDetection: UriMatchStrategy.Host });
cipherFormContainer.patchCipher.mockClear();

gaFeatureFlag$.next(true);

expect(component.autofillOptionsForm.controls.uris.at(0).value).toEqual({
uri: pairingUri,
matchDetection: UriMatchStrategy.Host,
});
expect(getSavedUris()).toEqual([{ uri: pairingUri, match: UriMatchStrategy.Never }]);
});

it("does not update the cipher when its desktop app URIs already use the Never match strategy", () => {
openForm(["https://example.com", null], [pairingUri, UriMatchStrategy.Never]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ export class AutofillOptionsComponent implements OnInit {
this.addUri(
{
uri: uri.uri,
matchDetection: uri.match,
// Desktop app URIs load with the match strategy they're saved with, so the form holds it
matchDetection: this.savedMatchDetection({ uri: uri.uri, matchDetection: uri.match }),
},
false,
false,
Expand Down Expand Up @@ -384,7 +385,9 @@ export class AutofillOptionsComponent implements OnInit {

/**
* Saves the desktop app URIs already on the cipher with the Never match strategy, even when the
* user doesn't change the URIs.
* user doesn't change the URIs. Desktop app rows also get it in the form, for when the feature flag
* is read after they load. Desktop app URIs typed into a website row keep the form's strategy until
* they're saved.
*/
private saveDesktopAppUrisWithNeverMatch() {
const matchChanged = this.uriControls.some(
Expand All @@ -393,9 +396,22 @@ export class AutofillOptionsComponent implements OnInit {
(control.value?.matchDetection ?? null),
);

if (matchChanged) {
this.updateUriFields();
if (!matchChanged) {
return;
}

for (const control of this.uriControls) {
if (this.desktopAppRows.has(control)) {
control.setValue(
{ ...control.value, matchDetection: this.savedMatchDetection(control.value) ?? null },
{ emitEvent: false },
);
}
}

this.updateUriFields();
// The match strategies changed without emitting value changes
this.publishUriMatchStrategies();
}

/** Reorder the controls to match the new order after a "drop" event */
Expand Down
Loading