From aceae75ce7cb0ee951107dde02527516b575a943 Mon Sep 17 00:00:00 2001 From: Jonatan Witoszek Date: Tue, 4 Oct 2022 14:28:26 +0200 Subject: [PATCH] Add rel=noopener to links that take users to external sites (#1980) * Add rel=noopener to links that take users to external sites * Update Storyshots for Link changes * Fix invalid isExternalURL function usage * Update snapshots --- package-lock.json | 2 +- src/components/ExternalLink/ExternalLink.tsx | 20 ++++- src/components/Link.tsx | 7 ++ .../OrderDetails/OrderNormalDetails/index.tsx | 1 + .../OrderUnconfirmedDetails/index.tsx | 1 + .../__snapshots__/Stories.test.ts.snap | 81 +++++++++++++++++++ 6 files changed, 109 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index fd385fb38..b73d9b591 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27156,7 +27156,7 @@ "request-progress": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/request-progress/-/request-progress-3.0.0.tgz", - "integrity": "sha512-MnWzEHHaxHO2iWiQuHrUPBi/1WeBf5PkxQqNyNvLl9VAYSdXkP8tQ3pBSeCPD+yw0v0Aq1zosWLz0BdeXpWwZg==", + "integrity": "sha1-TKdUCBx/7GP1BeT6qCWqBs1mnb4=", "dev": true, "requires": { "throttleit": "^1.0.0" diff --git a/src/components/ExternalLink/ExternalLink.tsx b/src/components/ExternalLink/ExternalLink.tsx index 58570c3de..de01628d0 100644 --- a/src/components/ExternalLink/ExternalLink.tsx +++ b/src/components/ExternalLink/ExternalLink.tsx @@ -19,12 +19,28 @@ interface ExternalLinkProps extends React.HTMLProps { } const ExternalLink: React.FC = props => { - const { className, children, href, typographyProps, ...rest } = props; + const { + className, + children, + href, + typographyProps, + target, + rel, + ...rest + } = props; const classes = useStyles(props); + const opensNewTab = target === "_blank"; + return ( - + {children} diff --git a/src/components/Link.tsx b/src/components/Link.tsx index 98705f13b..5eaf4eaef 100644 --- a/src/components/Link.tsx +++ b/src/components/Link.tsx @@ -50,11 +50,15 @@ const Link: React.FC = props => { onClick, disabled, href, + target, + rel, ...linkProps } = props; const classes = useStyles(props); + const opensNewTab = target === "_blank"; + const commonLinkProps = { className: classNames(className, { [classes.root]: true, @@ -71,6 +75,9 @@ const Link: React.FC = props => { event.preventDefault(); onClick(event); }, + target, + rel: + rel ?? (opensNewTab && isExternalURL(href)) ? "noopener noreferer" : "", ...linkProps, }; diff --git a/src/orders/views/OrderDetails/OrderNormalDetails/index.tsx b/src/orders/views/OrderDetails/OrderNormalDetails/index.tsx index c6c2f19d9..d8c7e6e81 100644 --- a/src/orders/views/OrderDetails/OrderNormalDetails/index.tsx +++ b/src/orders/views/OrderDetails/OrderNormalDetails/index.tsx @@ -232,6 +232,7 @@ export const OrderNormalDetails: React.FC = ({ window.open( order.invoices.find(invoice => invoice.id === id)?.url, "_blank", + "rel=noopener", ) } onInvoiceGenerate={() => diff --git a/src/orders/views/OrderDetails/OrderUnconfirmedDetails/index.tsx b/src/orders/views/OrderDetails/OrderUnconfirmedDetails/index.tsx index 90f663d1e..26a46bbbe 100644 --- a/src/orders/views/OrderDetails/OrderUnconfirmedDetails/index.tsx +++ b/src/orders/views/OrderDetails/OrderUnconfirmedDetails/index.tsx @@ -241,6 +241,7 @@ export const OrderUnconfirmedDetails: React.FC = ( window.open( order.invoices.find(invoice => invoice.id === id)?.url, "_blank", + "rel=noopener", ) } onInvoiceGenerate={() => diff --git a/src/storybook/__snapshots__/Stories.test.ts.snap b/src/storybook/__snapshots__/Stories.test.ts.snap index 124511c19..42b13c447 100644 --- a/src/storybook/__snapshots__/Stories.test.ts.snap +++ b/src/storybook/__snapshots__/Stories.test.ts.snap @@ -6416,6 +6416,7 @@ exports[`Storyshots Generics / External Link default 1`] = `
Ă…land Islands International store @@ -18985,6 +18989,7 @@ exports[`Storyshots Orders / OrderHistory default 1`] = `
Jane Doe @@ -24586,6 +24591,7 @@ exports[`Storyshots Shipping zones with no options selected 1`] = ` Add Shipping Zones @@ -24802,6 +24808,7 @@ exports[`Storyshots Shipping zones with options selected 1`] = ` Add Shipping Zones @@ -25001,6 +25008,7 @@ exports[`Storyshots Views / Apps / App details default 1`] = ` Jane Doe @@ -121397,6 +121414,7 @@ exports[`Storyshots Views / Orders / Order details no customer note 1`] = ` Default Channel @@ -123057,6 +123075,7 @@ exports[`Storyshots Views / Orders / Order details no payment 1`] = `
Jane Doe @@ -123393,6 +123412,7 @@ exports[`Storyshots Views / Orders / Order details no payment 1`] = ` Default Channel @@ -125111,6 +125131,7 @@ exports[`Storyshots Views / Orders / Order details no shipping address 1`] = ` Jane Doe @@ -125439,6 +125460,7 @@ exports[`Storyshots Views / Orders / Order details no shipping address 1`] = ` Default Channel @@ -127157,6 +127179,7 @@ exports[`Storyshots Views / Orders / Order details partially fulfilled 1`] = ` Jane Doe @@ -127493,6 +127516,7 @@ exports[`Storyshots Views / Orders / Order details partially fulfilled 1`] = ` Default Channel @@ -129211,6 +129235,7 @@ exports[`Storyshots Views / Orders / Order details payment confirmed 1`] = ` Jane Doe @@ -129547,6 +129572,7 @@ exports[`Storyshots Views / Orders / Order details payment confirmed 1`] = ` Default Channel @@ -131265,6 +131291,7 @@ exports[`Storyshots Views / Orders / Order details payment error 1`] = ` Jane Doe @@ -131601,6 +131628,7 @@ exports[`Storyshots Views / Orders / Order details payment error 1`] = ` Default Channel @@ -133319,6 +133347,7 @@ exports[`Storyshots Views / Orders / Order details pending payment 1`] = ` Jane Doe @@ -133655,6 +133684,7 @@ exports[`Storyshots Views / Orders / Order details pending payment 1`] = ` Default Channel @@ -135386,6 +135416,7 @@ exports[`Storyshots Views / Orders / Order details refunded payment 1`] = ` Jane Doe @@ -135722,6 +135753,7 @@ exports[`Storyshots Views / Orders / Order details refunded payment 1`] = ` Default Channel @@ -137440,6 +137472,7 @@ exports[`Storyshots Views / Orders / Order details rejected payment 1`] = ` Jane Doe @@ -137776,6 +137809,7 @@ exports[`Storyshots Views / Orders / Order details rejected payment 1`] = ` Default Channel @@ -139494,6 +139528,7 @@ exports[`Storyshots Views / Orders / Order details unfulfilled 1`] = ` Jane Doe @@ -139830,6 +139865,7 @@ exports[`Storyshots Views / Orders / Order details unfulfilled 1`] = ` Default Channel @@ -140193,6 +140229,7 @@ exports[`Storyshots Views / Orders / Order draft default 1`] = ` > Add Discount @@ -140569,6 +140608,7 @@ exports[`Storyshots Views / Orders / Order draft default 1`] = ` Default Channel @@ -141161,6 +141201,7 @@ exports[`Storyshots Views / Orders / Order draft no user permissions 1`] = ` > Add Discount @@ -141537,6 +141580,7 @@ exports[`Storyshots Views / Orders / Order draft no user permissions 1`] = ` Default Channel @@ -141864,6 +141908,7 @@ exports[`Storyshots Views / Orders / Order draft with errors 1`] = ` > Add Discount @@ -142245,6 +142292,7 @@ exports[`Storyshots Views / Orders / Order draft with errors 1`] = ` Default Channel @@ -142611,6 +142659,7 @@ exports[`Storyshots Views / Orders / Order draft without lines 1`] = ` Default Channel @@ -181116,6 +181165,7 @@ exports[`Storyshots Views / Products / Create product variant no warehouses 1`] There are no warehouses set up for your store. To add stock quantity to the variant please configure a warehouse @@ -184355,6 +184405,7 @@ exports[`Storyshots Views / Products / Product edit form errors 1`] = ` > All Warehouses Versatile @@ -186393,6 +186445,7 @@ exports[`Storyshots Views / Products / Product edit limits reached 1`] = ` > All Warehouses Versatile @@ -188343,6 +188397,7 @@ exports[`Storyshots Views / Products / Product edit no limits 1`] = ` > All Warehouses Versatile @@ -190030,6 +190086,7 @@ exports[`Storyshots Views / Products / Product edit no product attributes 1`] = > All Warehouses Versatile @@ -192578,6 +192636,7 @@ exports[`Storyshots Views / Products / Product edit no stock and no variants 1`] Versatile @@ -194401,6 +194460,7 @@ exports[`Storyshots Views / Products / Product edit no stock, no variants and no Versatile @@ -196224,6 +196284,7 @@ exports[`Storyshots Views / Products / Product edit no variants 1`] = ` Versatile @@ -197449,6 +197510,7 @@ exports[`Storyshots Views / Products / Product edit when data is fully loaded 1` > All Warehouses Versatile @@ -199331,6 +199394,7 @@ exports[`Storyshots Views / Products / Product edit when loading data 1`] = ` ... @@ -200225,6 +200289,7 @@ exports[`Storyshots Views / Products / Product edit when product has no images 1 > All Warehouses Versatile @@ -202773,6 +202839,7 @@ exports[`Storyshots Views / Products / Product edit when product has no variants Versatile @@ -203998,6 +204065,7 @@ exports[`Storyshots Views / Products / Product edit with channels 1`] = ` > All Warehouses Versatile @@ -246982,6 +247051,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details default 1`] = ` Europe @@ -246992,6 +247062,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details default 1`] = ` Oceania @@ -247002,6 +247073,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details default 1`] = ` Asia @@ -247012,6 +247084,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details default 1`] = ` Americas @@ -247022,6 +247095,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details default 1`] = ` Africa @@ -247798,6 +247872,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details form errors 1`] = ` Europe @@ -247808,6 +247883,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details form errors 1`] = ` Oceania @@ -247818,6 +247894,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details form errors 1`] = ` Asia @@ -247828,6 +247905,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details form errors 1`] = ` Americas @@ -247838,6 +247916,7 @@ exports[`Storyshots Views / Warehouses / Warehouse details form errors 1`] = ` Africa @@ -251469,6 +251548,7 @@ exports[`Storyshots Warehouses with no options selected 1`] = ` Add Warehouses @@ -251723,6 +251803,7 @@ exports[`Storyshots Warehouses with options selected 1`] = ` Add Warehouses