Repository navigation
feat: handle the MFA inbound-SMS link in the host app #119
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
Open
CAMOBAP
wants to merge
2
commits into
master
Choose a base branch
from
feat/sms-link-handling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,134 @@ | ||
| import { parseSmsLink } from '../hcaptchaSmsLink'; | ||
|
|
||
| // The shape the MFA challenge emits: an hCaptcha number and a full sentence carrying a | ||
| // hyphenated one-time code. The numbers and codes here are dummies. | ||
| const POOL_NUMBER = '+15550001111'; | ||
| const POOL_BODY = | ||
| 'Return to the app and press Confirm after sending this message. ' + | ||
| 'Do not edit or share the code: aaaa-bbbb-cccc'; | ||
| const POOL_LINK = | ||
| 'sms:+15550001111?body=Return%20to%20the%20app%20and%20press%20Confirm%20after%20sending%20' + | ||
| 'this%20message.%20Do%20not%20edit%20or%20share%20the%20code%3A%20aaaa-bbbb-cccc'; | ||
|
|
||
| // The same shape with the empty first query parameter the challenge is reported to emit: | ||
| // `?&body=`, not `?body=`. | ||
| const EMPTY_LEADING_PAIR_LINK = | ||
| 'sms:+15550002222?&body=Return%20to%20the%20app%20and%20press%20Confirm%20after%20sending%20' + | ||
| 'this%20message.%20Do%20not%20edit%20or%20share%20the%20code%3A%20dddd-eeee-ffff'; | ||
|
|
||
| const NUMBER = '+15551234567'; | ||
| const CODE = 'code'; | ||
| const CODE_QUERY = '?body=code'; | ||
|
|
||
| /** @param suffix everything after the hCaptcha number in the link */ | ||
| const parseSuffix = (suffix) => parseSmsLink(`sms:${NUMBER}${suffix}`); | ||
|
|
||
| describe('parseSmsLink', () => { | ||
| it('parses a full challenge-shaped link', () => { | ||
| expect(parseSmsLink(POOL_LINK)).toEqual({ recipient: POOL_NUMBER, body: POOL_BODY }); | ||
| }); | ||
|
|
||
| it('parses a link with an empty leading query parameter', () => { | ||
| expect(parseSmsLink(EMPTY_LEADING_PAIR_LINK)).toEqual({ | ||
| recipient: '+15550002222', | ||
| body: | ||
| 'Return to the app and press Confirm after sending this message. ' + | ||
| 'Do not edit or share the code: dddd-eeee-ffff', | ||
| }); | ||
| }); | ||
|
|
||
| it('skips empty query parameters', () => { | ||
| expect(parseSuffix('?&body=code').body).toBe(CODE); | ||
| expect(parseSuffix('?&&body=code').body).toBe(CODE); | ||
| expect(parseSuffix('?x=1&body=code&').body).toBe(CODE); | ||
| }); | ||
|
|
||
| it('keeps the body byte-exact', () => { | ||
| // The code is hyphenated in the SMS body and unhyphenated in the challenge UI; the backend | ||
| // normalises, so nothing here may trim, reformat or re-case the body. | ||
| expect(parseSuffix('?body=%20%20spaced%20%20').body).toBe(' spaced '); | ||
| expect(parseSuffix('?body=aaaa-bbbb-cccc').body).toBe('aaaa-bbbb-cccc'); | ||
| }); | ||
|
|
||
| it('splits the query before the subscriber params', () => { | ||
| // RFC 5724 allows `;` subscriber params before the query. Folding the two grammars together | ||
| // loses the body. | ||
| expect(parseSuffix(`;phone-context=+1${CODE_QUERY}`)).toEqual({ | ||
| recipient: NUMBER, | ||
| body: CODE, | ||
| }); | ||
| }); | ||
|
|
||
| it('treats a semicolon inside the query as body text', () => { | ||
| expect(parseSuffix('?body=code;more').body).toBe('code;more'); | ||
| }); | ||
|
|
||
| it('reads the legacy body parameter from the head', () => { | ||
| expect(parseSuffix(';body=hello').body).toBe('hello'); | ||
| }); | ||
|
|
||
| it('keeps a plus in the body', () => { | ||
| // `+` is a literal plus in a URI query (RFC 3986); only form encoding reads it as a space. | ||
| expect(parseSuffix('?body=a+b').body).toBe('a+b'); | ||
| }); | ||
|
|
||
| it('decodes utf-8 escapes', () => { | ||
| expect(parseSuffix('?body=caf%C3%A9%20%E2%80%93%20ok').body).toBe('café – ok'); | ||
| }); | ||
|
|
||
| it('keeps malformed escapes as literal text', () => { | ||
| expect(parseSuffix('?body=100%25').body).toBe('100%'); | ||
| expect(parseSuffix('?body=50%').body).toBe('50%'); | ||
| expect(parseSuffix('?body=%zz').body).toBe('%zz'); | ||
| }); | ||
|
|
||
| it('normalises recipient formatting', () => { | ||
| expect(parseSmsLink('sms:+123-456-789?body=Hello%20World').recipient).toBe('+123456789'); | ||
| expect(parseSmsLink('sms:%2B1%20(555)%20123.4567').recipient).toBe(NUMBER); | ||
| }); | ||
|
|
||
| it('keeps only the first recipient', () => { | ||
| expect(parseSuffix(`,+15559999999${CODE_QUERY}`).recipient).toBe(NUMBER); | ||
| }); | ||
|
|
||
| it('accepts the smsto and authority forms', () => { | ||
| expect(parseSmsLink(`smsto:${NUMBER}${CODE_QUERY}`).recipient).toBe(NUMBER); | ||
| expect(parseSmsLink(`sms://${NUMBER}${CODE_QUERY}`).body).toBe(CODE); | ||
| expect(parseSmsLink(`SMS:${NUMBER}?BODY=code`).body).toBe(CODE); | ||
| }); | ||
|
|
||
| it('reports missing parts as null', () => { | ||
| expect(parseSuffix('').body).toBeNull(); | ||
| expect(parseSuffix('?body=').body).toBeNull(); | ||
| expect(parseSuffix('?subject=hi').body).toBeNull(); | ||
| expect(parseSmsLink(`sms:${CODE_QUERY}`).recipient).toBeNull(); | ||
| expect(parseSmsLink('sms:not-a-number?body=nope').recipient).toBeNull(); | ||
| }); | ||
|
|
||
| it('ignores literal fragments without removing encoded hashes', () => { | ||
| // A literal `#` ends the link, so a `?query` sitting after one is fragment text and the | ||
| // body goes with it. An encoded `%23` is payload and survives decoding. | ||
| const cases = [ | ||
| [`sms:${NUMBER}#123?body=code`, NUMBER, null], | ||
| [`sms:${NUMBER}?body=code#123`, NUMBER, CODE], | ||
| [`sms:${NUMBER};body=code#123?body=other`, NUMBER, CODE], | ||
| [`sms:${NUMBER}?body=code%23suffix`, NUMBER, 'code#suffix'], | ||
| [`sms:${NUMBER}?body=code%23suffix#ignored`, NUMBER, 'code#suffix'], | ||
| [`sms:${NUMBER};body=code%23suffix#ignored`, NUMBER, 'code#suffix'], | ||
| [`sms:${NUMBER}?body=%2523#ignored`, NUMBER, '%23'], | ||
| ['sms:#123?body=code', null, null], | ||
| ]; | ||
|
|
||
| cases.forEach(([url, recipient, body]) => { | ||
| expect(parseSmsLink(url)).toEqual({ recipient, body }); | ||
| }); | ||
| }); | ||
|
|
||
| it('rejects non-sms urls', () => { | ||
| expect(parseSmsLink(null)).toBeNull(); | ||
| expect(parseSmsLink(undefined)).toBeNull(); | ||
| expect(parseSmsLink(`https://example.com${CODE_QUERY}`)).toBeNull(); | ||
| expect(parseSmsLink(`tel:${NUMBER}`)).toBeNull(); | ||
| expect(parseSmsLink(`smsx:${NUMBER}`)).toBeNull(); | ||
| }); | ||
| }); |
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.
How can we verify the body prefill and the returned challenege on mobile for both the IOS and android? I see it checking the outgoing URL but I don't see the messaging app test.
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.
Good question, and it's the one place the JS SDK can't fully close. Neither native SDK tests the real messaging app either, so I've matched what they settled on:
intending(hasAction(ACTION_SENDTO)).respondWith(...)— so the messaging app never starts, then asserts the hand-off itself: action,smsto:data URI, and thesms_bodyextra byte-exact. The live path is confirmed by hand and recorded in a comment ontestSmsHandledFromNewWindow: "Verified against a live sitekey."MFMessageComposeViewControllerin CI at all (canSendText() == falseon the simulator), so it injected a presenter stand-in specifically to keep thedidFinishWithdismissal and return-to-challenge path testable.So the accepted bar is "assert the outgoing hand-off, verify the live flow manually." These tests assert the same category of thing — the exact URL handed to
Linking.openURL, including the body — which is the RN equivalent of Android's intent assertion.