-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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-14419] At-risk passwords change password service #13279
Merged
shane-melton
merged 6 commits into
main
from
vault/pm-14419/change-password-url-service
Feb 13, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4fb5295
[PM-14419] Introduce the change-login-password service and its defaulโฆ
shane-melton a801b61
[PM-14419] Use the change login password service on the at-risk passwโฆ
shane-melton f2c2ca4
[PM-14419] Add unit tests
shane-melton a590c16
[PM-14419] Use existing fixed test environment
shane-melton e926de1
[PM-14419] Add mock implementation for ChangeLoginPasswordService in โฆ
shane-melton 099c404
[PM-14419] Linter
shane-melton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
libs/vault/src/abstractions/change-login-password.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; | ||
|
||
export abstract class ChangeLoginPasswordService { | ||
/** | ||
* Attempts to find a well-known change password URL for the given cipher. Only works for Login ciphers with at | ||
* least one http/https URL. If no well-known change password URL is found, the first URL is returned. | ||
* Non-Login ciphers and Logins with no valid http/https URLs return null. | ||
*/ | ||
abstract getChangePasswordUrl(cipher: CipherView): Promise<string | null>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
libs/vault/src/services/default-change-login-password.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/** | ||
* Jest needs to run in custom environment to mock Request/Response objects | ||
* @jest-environment ../../libs/shared/test.environment.ts | ||
*/ | ||
shane-melton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import { mock } from "jest-mock-extended"; | ||
|
||
import { ApiService } from "@bitwarden/common/abstractions/api.service"; | ||
import { CipherType } from "@bitwarden/common/vault/enums"; | ||
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; | ||
import { LoginUriView } from "@bitwarden/common/vault/models/view/login-uri.view"; | ||
import { LoginView } from "@bitwarden/common/vault/models/view/login.view"; | ||
|
||
import { DefaultChangeLoginPasswordService } from "./default-change-login-password.service"; | ||
|
||
describe("DefaultChangeLoginPasswordService", () => { | ||
let service: DefaultChangeLoginPasswordService; | ||
|
||
let mockShouldNotExistResponse: Response; | ||
let mockWellKnownResponse: Response; | ||
|
||
const mockApiService = mock<ApiService>(); | ||
|
||
beforeEach(() => { | ||
mockApiService.nativeFetch.mockClear(); | ||
|
||
// Default responses to success state | ||
mockShouldNotExistResponse = new Response("Not Found", { status: 404 }); | ||
mockWellKnownResponse = new Response("OK", { status: 200 }); | ||
|
||
mockApiService.nativeFetch.mockImplementation((request) => { | ||
if ( | ||
request.url.endsWith("resource-that-should-not-exist-whose-status-code-should-not-be-200") | ||
) { | ||
return Promise.resolve(mockShouldNotExistResponse); | ||
} | ||
|
||
if (request.url.endsWith(".well-known/change-password")) { | ||
return Promise.resolve(mockWellKnownResponse); | ||
} | ||
|
||
throw new Error("Unexpected request"); | ||
}); | ||
service = new DefaultChangeLoginPasswordService(mockApiService); | ||
}); | ||
|
||
it("should return null for non-login ciphers", async () => { | ||
const cipher = { | ||
type: CipherType.Card, | ||
} as CipherView; | ||
|
||
const url = await service.getChangePasswordUrl(cipher); | ||
|
||
expect(url).toBeNull(); | ||
}); | ||
|
||
it("should return null for logins with no URIs", async () => { | ||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { uris: [] as LoginUriView[] }), | ||
} as CipherView; | ||
|
||
const url = await service.getChangePasswordUrl(cipher); | ||
|
||
expect(url).toBeNull(); | ||
}); | ||
|
||
it("should return null for logins with no valid HTTP/HTTPS URIs", async () => { | ||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { | ||
uris: [{ uri: "ftp://example.com" }], | ||
}), | ||
} as CipherView; | ||
|
||
const url = await service.getChangePasswordUrl(cipher); | ||
|
||
expect(url).toBeNull(); | ||
}); | ||
|
||
it("should check the origin for a reliable status code", async () => { | ||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { | ||
uris: [{ uri: "https://example.com" }], | ||
}), | ||
} as CipherView; | ||
|
||
await service.getChangePasswordUrl(cipher); | ||
|
||
expect(mockApiService.nativeFetch).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
url: "https://example.com/.well-known/resource-that-should-not-exist-whose-status-code-should-not-be-200", | ||
}), | ||
); | ||
}); | ||
|
||
it("should attempt to fetch the well-known change password URL", async () => { | ||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { | ||
uris: [{ uri: "https://example.com" }], | ||
}), | ||
} as CipherView; | ||
|
||
await service.getChangePasswordUrl(cipher); | ||
|
||
expect(mockApiService.nativeFetch).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
url: "https://example.com/.well-known/change-password", | ||
}), | ||
); | ||
}); | ||
|
||
it("should return the well-known change password URL when successful at verifying the response", async () => { | ||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { | ||
uris: [{ uri: "https://example.com" }], | ||
}), | ||
} as CipherView; | ||
|
||
const url = await service.getChangePasswordUrl(cipher); | ||
|
||
expect(url).toBe("https://example.com/.well-known/change-password"); | ||
}); | ||
|
||
it("should return the original URI when unable to verify the response", async () => { | ||
mockShouldNotExistResponse = new Response("Ok", { status: 200 }); | ||
|
||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { | ||
uris: [{ uri: "https://example.com" }], | ||
}), | ||
} as CipherView; | ||
|
||
const url = await service.getChangePasswordUrl(cipher); | ||
|
||
expect(url).toBe("https://example.com"); | ||
}); | ||
|
||
it("should return the original URI when the well-known URL is not found", async () => { | ||
mockWellKnownResponse = new Response("Not Found", { status: 404 }); | ||
|
||
const cipher = { | ||
type: CipherType.Login, | ||
login: Object.assign(new LoginView(), { | ||
uris: [{ uri: "https://example.com" }], | ||
}), | ||
} as CipherView; | ||
|
||
const url = await service.getChangePasswordUrl(cipher); | ||
|
||
expect(url).toBe("https://example.com"); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
โ Would it be a better experience to hide the
change
button if the URL isn't available? At the moment if there is no URL the user doesn't have any feedback when clicking the button.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is a trade off, on page load there would be a two requests flying for each of the ciphers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this was the tradeoff to avoid spamming too many requests for large lists of ciphers. Though you do bring up a good point about there being a lack of feedback if there is no URL.
@sukhleenb What should the behavior be if we're unable to determine a change password URL and the item no longer as a URL associated? This should be pretty unlikely as items without URLs won't normally have tasks generated for them, but the cipher could technically have its URL removed after the task was created.