-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add login response validation (#4541)
## Explanation Profile Syncing. Adds login response validation from the developer/users storage medium. The user could lie and pass in an invalid login response, so this makes sure that there is some validation around the response shape. NOTE - we can improve this in the future by adding a runtime parser/validator like Zod. But this is out of scope and we can add in a separate PR. ## References [NOTIFY-881](https://consensyssoftware.atlassian.net/browse/NOTIFY-881) ## Changelog ### `@metamask/profile-sync-controller` - **ADDED**: Validation around the LoginResponse shape. ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate
- Loading branch information
1 parent
ed7a9db
commit d9a386d
Showing
4 changed files
with
58 additions
and
2 deletions.
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
32 changes: 32 additions & 0 deletions
32
packages/profile-sync-controller/src/sdk/utils/validate-login-response.test.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,32 @@ | ||
import type { LoginResponse } from '../authentication'; | ||
import { validateLoginResponse } from './validate-login-response'; | ||
|
||
describe('validateLoginResponse() tests', () => { | ||
it('validates if a shape is of type LoginResponse', () => { | ||
const input: LoginResponse = { | ||
profile: { | ||
identifierId: '', | ||
metaMetricsId: '', | ||
profileId: '', | ||
}, | ||
token: { | ||
accessToken: '', | ||
expiresIn: 3600, | ||
obtainedAt: Date.now(), | ||
}, | ||
}; | ||
|
||
expect(validateLoginResponse(input)).toBe(true); | ||
}); | ||
|
||
it('returns false if a shape is invalid', () => { | ||
const assertInvalid = (input: unknown) => { | ||
expect(validateLoginResponse(input)).toBe(false); | ||
}; | ||
|
||
assertInvalid(null); | ||
assertInvalid({}); | ||
assertInvalid({ profile: {} }); | ||
assertInvalid({ token: {} }); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/profile-sync-controller/src/sdk/utils/validate-login-response.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,22 @@ | ||
import type { LoginResponse } from '../authentication'; | ||
|
||
/** | ||
* Validates Shape is LoginResponse | ||
* NOTE - validation is pretty loose, we can improve this by using external libs like Zod for improved/tighter validation | ||
* | ||
* @param input - unknown/untyped input | ||
* @returns boolean if input is LoginResponse | ||
*/ | ||
export function validateLoginResponse(input: unknown): input is LoginResponse { | ||
const assumedInput = input as LoginResponse; | ||
|
||
if (!assumedInput) { | ||
return false; | ||
} | ||
|
||
if (!assumedInput?.token || !assumedInput?.profile) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |