Skip to content

Commit fd78a56

Browse files
authored
fix: Make QR scanner more strict (#28521)
## **Description** The QR scanner is now more strict about the contents it allows to be scanned. If the scanned QR code deviates at all from the supported formats, it will return "unknown" as the result (as it always has for completely unrecognized QR codes). Previously we would accept QR codes with a recognized prefix even if the complete contents did not match our expectations, which has resulted in unexpected behavior. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28521?quickstart=1) ## **Related issues** Fixes #28527 ## **Manual testing steps** - Open the MetaMask extension and select 'Send' - Click on the QR scanner icon in the "Send To" field and enable webcam - Scan a ERC-20 wallet receive QR from a mobile app, which follows the EIP-681 standard and contains a valid token contract and account address - ERC-20 Token Contract Address, which is the first address in the string, populates the "Send To" field instead of the intended recipient address ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** We didn't record this, but multiple people on the team reproduced the problem. ### **After** https://www.loom.com/share/be8822e872a14ec98a47547cf6198603 ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - We don't yet have any way to test QR scanning. We will follow up later with tests, and rely on manual testing for now. Later test automation work tracked in #28528 - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [x] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent ec8e5fb commit fd78a56

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

ui/components/app/modals/qr-scanner/qr-scanner.component.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const READY_STATE = {
2222
READY: 'READY',
2323
};
2424

25+
const ethereumPrefix = 'ethereum:';
26+
// A 0x-prefixed Ethereum address is 42 characters (2 prefix + 40 address)
27+
const addressLength = 42;
28+
2529
const parseContent = (content) => {
2630
let type = 'unknown';
2731
let values = {};
@@ -31,12 +35,18 @@ const parseContent = (content) => {
3135
// For ex. EIP-681 (https://eips.ethereum.org/EIPS/eip-681)
3236

3337
// Ethereum address links - fox ex. ethereum:0x.....1111
34-
if (content.split('ethereum:').length > 1) {
38+
if (
39+
content.split(ethereumPrefix).length > 1 &&
40+
content.length === ethereumPrefix.length + addressLength
41+
) {
3542
type = 'address';
36-
// uses regex capture groups to match and extract address while ignoring everything else
43+
// uses regex capture groups to match and extract address
3744
values = { address: parseScanContent(content) };
3845
// Regular ethereum addresses - fox ex. 0x.....1111
39-
} else if (content.substring(0, 2).toLowerCase() === '0x') {
46+
} else if (
47+
content.substring(0, 2).toLowerCase() === '0x' &&
48+
content.length === addressLength
49+
) {
4050
type = 'address';
4151
values = { address: content };
4252
}

0 commit comments

Comments
 (0)