Skip to content

@alpinejs/csp throws SecurityError when running in a sandboxed iframe without allow-same-origin #4892

Closed

Description

@xhaggi

Alpine.js version

3.17.1

Browser and operating system

Chrome

Describe the issue you're experiencing

@alpinejs/csp accesses localStorage unguarded during initialization. In a sandboxed iframe without the allow-same-origin flag, the document has an opaque origin and any access to localStorage throws a SecurityError, which prevents Alpine from initializing at all.

Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window':
The document is sandboxed and lacks the 'allow-same-origin' flag.
    at Array.forEach (<anonymous>)
    at /node_modules/@alpinejs/csp/dist/module.esm.js:1889:40

https://github.com/alpinejs/alpine/blob/master/packages/csp/src/parser.js#L4

Code snippets to reproduce the issue

<!DOCTYPE html>
<html>
<body>
<script>
  const html = `
    <!DOCTYPE html>
    <html><body>
      <div x-data="counter">
        <span x-text="count"></span>
        <button x-on:click="increment">+</button>
      </div>
      <script src="https://cdn.jsdelivr.net/npm/@alpinejs/[email protected]/dist/cdn.min.js"><\/script>
      <script>
        document.addEventListener('alpine:init', () => {
          Alpine.data('counter', () => ({ count: 0, increment() { this.count++ } }));
        });
      <\/script>
    </body></html>
  `;

  const iframe = document.createElement('iframe');
  iframe.setAttribute('sandbox', 'allow-scripts');   // kein allow-same-origin
  iframe.srcdoc = html;
  document.body.appendChild(iframe);
</script>
</body>
</html>

Screenshots/screen recordings

No response

How do you expect it to work?

Storage access is optional and should not fail if not accessible.

Please confirm (incomplete submissions will not be addressed)

  • I have provided easy and step-by-step instructions to reproduce the bug.
  • I have provided code samples as text and NOT images.
  • I understand my bug report will be removed if I haven't met the criteria above.

Activity

  1. xhaggi commented on Sep 4, 2026

    @xhaggi
    Author

    👍 thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions