BRUCE_FEBRUCE_FE

EN/CH Mode

BRUCE_FE CSS Interview Notes - How to Properly Open External Links

Master the complete guide to safely opening external links in new tabs, understand the correct use of target and rel attributes, and how to prevent security risks and improve user experience.

影片縮圖

Lazy to read articles? Then watch videos!

Security Concerns and Solutions

Using only target="_blank" to open external links poses security risks. The newly opened page can access the original page's window object through window.opener, which may lead to tabnabbing attacks.

Potential Risk Example

// Code that might be executed on a malicious website
if (window.opener) {
  // Can access the original page's window object
  window.opener.location = 'https://malicious-site.com/fake-login.html';
  
  // When user returns to the original tab, they may have been redirected to a phishing page
}

To prevent this, you should always add the rel="noopener noreferrer" attribute when opening external links:

<a 
  href="https://example.com" 
  target="_blank" 
  rel="noopener noreferrer"
>
  Safely Visit External Website
</a>

rel="noopener"

  • 1. Prevents newly opened windows from accessing window.opener
  • 2. Prevents tabnabbing attacks
  • 3. Modern browsers add this by default when using target="_blank"

rel="noreferrer"

  • 1. Prevents sending Referer information in request headers
  • 2. Enhances privacy protection, doesn't reveal source website
  • 3. Provides backward compatibility for older browsers

🔥 Common Interview Questions

(1) What security risks exist with target="_blank"? How to solve them?

Answer: When using target="_blank" to open links, the following security risks exist:

  1. 1. Window Reference Vulnerability: The new page can access the original page through window.opener and even modify the original page's URL.
  2. 2. Performance Issues: The new page shares processes with the original page, which may affect the original page's performance.
  3. 3. Referer Information Leakage: May leak user's source website information.

Solution: Add the rel="noopener noreferrer" attribute

<!-- Unsafe approach -->
<a href="https://example.com" target="_blank">Visit External Website</a>

<!-- Safe approach -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Visit External Website</a>

Explanation:

  • noopener: Makes window.opener return null, preventing tabnabbing.
  • noreferrer: Prevents sending Referer header information, protecting privacy.