@@ -40,7 +40,7 @@ import { lexer, walkTokens } from 'marked';
40
40
import type { Token , Tokens } from 'marked' ;
41
41
42
42
const MAX_TEXT_LENGTH = 50_000 ; // 50 kb
43
- const ALLOWED_PROTOCOLS = [ 'https:' , 'mailto:' ] ;
43
+ const DEFAULT_ALLOWED_PROTOCOLS = [ 'https:' , 'mailto:' ] ;
44
44
45
45
/**
46
46
* Get the button variant from a legacy button component variant.
@@ -320,16 +320,18 @@ function getMarkdownLinks(text: string) {
320
320
* @param link - The link to validate.
321
321
* @param isOnPhishingList - The function that checks the link against the
322
322
* phishing list.
323
+ * @param allowedProtocols - Allowed protocols (example: ['https:'])
323
324
*/
324
325
function validateLink (
325
326
link : string ,
326
327
isOnPhishingList : ( url : string ) => boolean ,
328
+ allowedProtocols : string [ ] ,
327
329
) {
328
330
try {
329
331
const url = new URL ( link ) ;
330
332
assert (
331
- ALLOWED_PROTOCOLS . includes ( url . protocol ) ,
332
- `Protocol must be one of: ${ ALLOWED_PROTOCOLS . join ( ', ' ) } .` ,
333
+ allowedProtocols . includes ( url . protocol ) ,
334
+ `Protocol must be one of: ${ allowedProtocols . join ( ', ' ) } .` ,
333
335
) ;
334
336
335
337
const hostname =
@@ -352,16 +354,18 @@ function validateLink(
352
354
* @param text - The text to verify.
353
355
* @param isOnPhishingList - The function that checks the link against the
354
356
* phishing list.
357
+ * @param allowedProtocols - Allowed protocols (example: ['https:'])
355
358
* @throws If the text contains a link that is not allowed.
356
359
*/
357
360
export function validateTextLinks (
358
361
text : string ,
359
362
isOnPhishingList : ( url : string ) => boolean ,
363
+ allowedProtocols : string [ ] = DEFAULT_ALLOWED_PROTOCOLS ,
360
364
) {
361
365
const links = getMarkdownLinks ( text ) ;
362
366
363
367
for ( const link of links ) {
364
- validateLink ( link . href , isOnPhishingList ) ;
368
+ validateLink ( link . href , isOnPhishingList , allowedProtocols ) ;
365
369
}
366
370
}
367
371
@@ -372,17 +376,19 @@ export function validateTextLinks(
372
376
* @param node - The JSX node to walk.
373
377
* @param isOnPhishingList - The function that checks the link against the
374
378
* phishing list.
379
+ * @param allowedProtocols - Allowed protocols (example: ['https:'])
375
380
*/
376
381
export function validateJsxLinks (
377
382
node : JSXElement ,
378
383
isOnPhishingList : ( url : string ) => boolean ,
384
+ allowedProtocols : string [ ] = DEFAULT_ALLOWED_PROTOCOLS ,
379
385
) {
380
386
walkJsx ( node , ( childNode ) => {
381
387
if ( childNode . type !== 'Link' ) {
382
388
return ;
383
389
}
384
390
385
- validateLink ( childNode . props . href , isOnPhishingList ) ;
391
+ validateLink ( childNode . props . href , isOnPhishingList , allowedProtocols ) ;
386
392
} ) ;
387
393
}
388
394
0 commit comments