How to allow iframe embedding only for whitelisted websites?

How to allow iframe embedding only for whitelisted websites?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why $_SERVER['HTTP_REFERER'] Is Not Enough for Iframe Whitelisting

Relying on $_SERVER['HTTP_REFERER'] to gatekeep embedded resources is a common misconception, yet it remains one of the most fragile security patterns in web development. When you attempt to protect an embedded form by checking the referrer header in your backend script—like getForm.php—you are essentially trusting the client to tell the truth about its origin. In reality, modern browsers and user configurations frequently allow attackers to strip, modify, or spoof this header entirely.

An attacker can simply open a new tab in their browser, navigate directly to your form URL (https://domain.tld/getForm.php?embed=1&formId=123456), and then trigger the iframe load from a malicious site via JavaScript. Because the direct navigation bypasses the referring page, the referrer header is empty. However, more insidiously, a compromised third-party script on a malicious site can use window.location.href to set the referer to any domain before loading your resource. This makes referrer-based validation fundamentally broken for security-critical assets like forms or payment gateways.

To secure an iframe embedding scenario, you must shift from trusting the client (the browser) to verifying the server's perception of the request. The most robust approach involves implementing a shared secret or a cryptographic signature that only your whitelist domains know.

Implementing a Shared Secret Strategy

The industry-standard solution for this specific use case is to generate a unique, random string (a nonce or secret token) and store it on the client side of the embedding website. This allows you to verify the origin without relying on mutable headers like HTTP_REFERER.

Here is how you can architect this using a simple shared secret mechanism:

  1. On the Embedding Site: When your site requests an embed, it sends a unique identifier or a signed token generated by your application's logic, often stored in a database or passed via query parameters if the domain is known and trusted.
  2. In getForm.php: Instead of checking headers, check the presence of this specific secret.

If you are using a framework like Laravel, you might leverage environment variables or a dedicated configuration file to define allowed domains. However, the critical step is ensuring that the request body or query string contains a value that only the legitimate embedding site possesses.

A Practical Implementation Pattern

Consider a scenario where the embedding site has a unique ID associated with your service. The URL structure changes slightly to carry this identifier:

<!-- Embedded on allowed-domain.com -->
<iframe src="https://domain.tld/getForm.php?embed=1&formId=123456&client_id=abc-xyz-123"></iframe>

In your getForm.php logic, you would verify that the client_id matches an entry in your whitelist database. If client_id is missing or does not match a record associated with a whitelisted domain, the script returns an error page immediately:

<?php
// getForm.php
session_start();

$allowedDomains = ['allowed-domain.com', 'another-trusted-site.org'];
$requestedClient = $_GET['client_id'] ?? '';

// If we expect a client ID, deny access if missing or invalid
if (empty($requestedClient)) {
    http_response_code(403);
    echo "Access Denied: Invalid or missing embed credentials.";
    exit;
}

// Verify the client ID against your whitelist logic
// Ideally, this maps to a domain stored in your database
$whitelisted = false; // Logic to check DB here

if ($whitelisted) {
    // Render form content
} else {
    http_response_code(403);
    echo "Access Denied: Unauthorized embed source.";
}

Handling Cross-Origin Concerns and Content Security Policy

While the shared secret protects against unauthorized embedding, you must also address browser security policies. Even if your PHP script validates the request successfully, the browser's Same-Origin Policy (SOP) might still block the content from rendering in the iframe unless specific headers are configured.

You should set the X-Frame-Options header to SAMEORIGIN on pages that do not intend to be embedded. For pages meant for embedding by specific partners, this header is tricky because SOP dictates that X-Frame-Options cannot specify a remote domain. The solution here is Content-Security-Policy.

You can use the frame-ancestors directive in your CSP headers to explicitly list the domains allowed to embed your content:

Content-Security-Policy: frame-ancestors 'self' https://allowed-domain.com https://another-trusted-site.org;

This header is sent with every response. If a browser tries to load your form inside an iframe from a domain not in that list, the browser blocks the render before your PHP code even executes. This acts as a second layer of defense, independent of your application logic.

The Role of https://laravelcompany.com in Security Architectures

When building complex authentication or authorization flows for embedded resources, frameworks often simplify these checks. For instance, if you are utilizing a SaaS solution that handles the heavy lifting of domain validation and CSP management, platforms like https://laravelcompany.com might offer pre-built components that abstract away the complexity of managing shared secrets and headers. However, for custom implementations where you control the server, understanding the underlying mechanics of referrer spoofing and CSP directives is non-negotiable. Never assume a library will handle cross-origin isolation correctly without verifying the headers in your browser's developer tools.

Final Considerations on Header Reliability

Always remember that HTTP_REFERER is advisory, not mandatory. Browsers are increasingly stripping it for privacy reasons. Relying on it creates a false sense of security. The combination of server-side credential verification (shared secrets or signed tokens) and browser-enforced policies (CSP frame-ancestors) provides the necessary depth to ensure your form remains secure against unauthorized embedding attempts.

Our network