Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 120bf0d

Browse files
committed
Implement regex for validation
1 parent 7ae889a commit 120bf0d

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

src/helper.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,18 @@ export const reverse = src => {
186186
* @param {string} str The uri to validate
187187
* @return {boolean} If the uri is valid
188188
*/
189-
export const isValidUri = str => {
190-
return /^(lightning:|bitcoin:)[a-zA-Z0-9]*$/.test(str);
189+
export const isLnUri = str => {
190+
return /^lightning:ln[a-zA-Z0-9]*$/.test(str);
191+
};
192+
193+
/**
194+
* Basic bitcoin address validation. More thorough matching is
195+
* done by lnd. This is just to mitigate XSS.
196+
* @param {string} str The address to validate
197+
* @return {boolean} If the uri is valid
198+
*/
199+
export const isAddress = str => {
200+
return /^[a-km-zA-HJ-NP-Z0-9]{26,35}$/.test(str);
191201
};
192202

193203
/**

test/unit/helper.spec.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -508,38 +508,50 @@ describe('Helpers Unit Tests', () => {
508508
});
509509
});
510510

511-
describe('isValidUri()', () => {
512-
it('should accept bitcoin uri', () => {
513-
const uri = 'bitcoin:rfu4i1Mo2NF7TQsN9bMVLFSojSzcyQCEH5';
514-
expect(helpers.isValidUri(uri), 'to be', true);
515-
});
516-
511+
describe('isLnUri()', () => {
517512
it('should accept lightning uri', () => {
518513
const uri =
519514
'lightning:lntb1500n1pdn2e0app5wlyxzspccpfvqmrtfr8p487xcch4hxtu2u0qzcke6mzpv222w8usdpa2fjkzep6ypxx2ap8wvs8qmrp0ysxzgrvd9nksarwd9hxwgrwv468wmmjdvsxwcqzysmr9jxv06zx53cyqa0sqntehy5tyrqu064xvw00qjep5f9gw57qcqp6qnpqyuprh90aqzfyf9ypq8uth7qte5ecjq0fng3y47mywwkfqq3megny';
520-
expect(helpers.isValidUri(uri), 'to be', true);
515+
expect(helpers.isLnUri(uri), 'to be', true);
516+
});
517+
518+
it('should reject bitcoin uri', () => {
519+
const uri = 'bitcoin:rfu4i1Mo2NF7TQsN9bMVLFSojSzcyQCEH5';
520+
expect(helpers.isLnUri(uri), 'to be', false);
521521
});
522522

523523
it('should reject bitcoin address', () => {
524524
const uri = 'rfu4i1Mo2NF7TQsN9bMVLFSojSzcyQCEH5';
525-
expect(helpers.isValidUri(uri), 'to be', false);
525+
expect(helpers.isLnUri(uri), 'to be', false);
526526
});
527527

528528
it('should reject lightning invoice', () => {
529529
const uri =
530530
'lntb1500n1pdn2e0app5wlyxzspccpfvqmrtfr8p487xcch4hxtu2u0qzcke6mzpv222w8usdpa2fjkzep6ypxx2ap8wvs8qmrp0ysxzgrvd9nksarwd9hxwgrwv468wmmjdvsxwcqzysmr9jxv06zx53cyqa0sqntehy5tyrqu064xvw00qjep5f9gw57qcqp6qnpqyuprh90aqzfyf9ypq8uth7qte5ecjq0fng3y47mywwkfqq3megny';
531-
expect(helpers.isValidUri(uri), 'to be', false);
531+
expect(helpers.isLnUri(uri), 'to be', false);
532+
});
533+
534+
it('should mitigate xss', () => {
535+
const uri =
536+
'lightning:lntb1500n1<script>alert("XSS")</script>p487xcch4hxtu2u0qzcke6mzpv222w8usdpa2fjkzep6ypxx2ap8wvs8qmrp0ysxzgrvd9nksarwd9hxwgrwv468wmmjdvsxwcqzysmr9jxv06zx53cyqa0sqntehy5tyrqu064xvw00qjep5f9gw57qcqp6qnpqyuprh90aqzfyf9ypq8uth7qte5ecjq0fng3y47mywwkfqq3megny';
537+
expect(helpers.isLnUri(uri), 'to be', false);
538+
});
539+
});
540+
541+
describe('isAddress()', () => {
542+
it('should accept bitcoin uri', () => {
543+
const address = 'rfu4i1Mo2NF7TQsN9bMVLFSojSzcyQCEH5';
544+
expect(helpers.isAddress(address), 'to be', true);
532545
});
533546

534547
it('should reject invalid bitcoin uri', () => {
535-
const uri = 'bitcoin:/INVALID/rfu4i1Mo2NF7TQsN9bMVLFSojSzcyQCEH5';
536-
expect(helpers.isValidUri(uri), 'to be', false);
548+
const address = '/INVALID/rfu4i1Mo2NF7TQsN9bMVLFSoj';
549+
expect(helpers.isAddress(address), 'to be', false);
537550
});
538551

539552
it('should mitigate xss', () => {
540-
const uri =
541-
'bitcoin:rfu4i1Mo2NF7T<script>alert("XSS")</script>QsN9bMVLFSojSzcyQCEH5';
542-
expect(helpers.isValidUri(uri), 'to be', false);
553+
const address = 'rfu<script>alert("XSS")</script>';
554+
expect(helpers.isAddress(address), 'to be', false);
543555
});
544556
});
545557

0 commit comments

Comments
 (0)