Skip to content

Commit

Permalink
fix(ui5-link): add noopener to rel attribute (#4533)
Browse files Browse the repository at this point in the history
- The "noopener" text is added to the rel value attribute alongside "noreferrer" text for compatibility reasons with the openui5's sap.m.Link implementation.
- Explicit specification of rel "noopener" helps protect users from tabnabbing in legacy browsers including Edge Legacy and Internet Explorer.
  • Loading branch information
unazko authored Jan 17, 2022
1 parent 2294cb2 commit 3f2c3cd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/main/src/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ class Link extends UI5Element {
}

onBeforeRendering() {
const needsNoReferrer = this.target === "_blank"
const needsNoReferrer = this.target !== "_self"
&& this.href
&& this._isCrossOrigin();

this._rel = needsNoReferrer ? "noreferrer" : undefined;
this._rel = needsNoReferrer ? "noreferrer noopener" : undefined;
}

_isCrossOrigin() {
Expand Down
2 changes: 1 addition & 1 deletion packages/main/test/pages/Link.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<body class="link1auto">
<div class="group">
<ui5-link class="samples-big-margin-right" href="https://www.sap.com" target="_blank">Standard Link</ui5-link>
<ui5-link id="target-blank-link" class="samples-big-margin-right" href="https://www.sap.com" target="_blank">Standard Link</ui5-link>
<ui5-link class="samples-big-margin-right" href="https://www.sap.com" target="_blank" design="Subtle">Subtle link</ui5-link>
<ui5-link class="samples-big-margin-right" href="https://www.sap.com" target="_blank" disabled>Disabled</ui5-link>
<ui5-link class="samples-big-margin-right" href="https://www.sap.com" target="_blank" design="Emphasized">Emphasized</ui5-link>
Expand Down
6 changes: 6 additions & 0 deletions packages/main/test/specs/Link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe("General API", () => {
assert.strictEqual(await link.getAttribute("href"), HREF_ATTRIBUTE, "The href attribute is changed.");
});

it("tests rel attribute", async () => {
const anchor = await browser.$("#target-blank-link");

assert.strictEqual(await anchor.shadow$("a").getAttribute("rel"), "noreferrer noopener", "The rel attribute is properly set.");
});

it("tests target attributes", async () => {
const link = await browser.$("#empty-link-2");
const TARGET_ATTRIBUTE = "_blank";
Expand Down

0 comments on commit 3f2c3cd

Please sign in to comment.