Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbo frame request is removing the csrf meta tag #669

Open
noctivityinc opened this issue Aug 22, 2024 · 2 comments · May be fixed by #697
Open

Turbo frame request is removing the csrf meta tag #669

noctivityinc opened this issue Aug 22, 2024 · 2 comments · May be fixed by #697

Comments

@noctivityinc
Copy link

When doing a simply turbo frame request via a link click, we are noticing the CSRF meta tag is REMOVED from the page!? We are having to do this hack to get it back, which I don't love and seems like a bug:

document.addEventListener("turbo:before-fetch-request", function (event) {
  window.csrfToken = document.querySelector("[name=csrf-token]").content;
  event.detail.fetchOptions.headers["X-CSRF-Token"] = csrfToken;
});

document.addEventListener("turbo:frame-load", function (event) {
  const csrfMetaTag = document.querySelector('meta[name="csrf-token"]');
  if (!csrfMetaTag) {
    const newCsrfMetaTag = document.createElement("meta");
    newCsrfMetaTag.name = "csrf-token";
    newCsrfMetaTag.content = window.csrfToken;
    document.head.appendChild(newCsrfMetaTag);
  }
});
``
@sfnelson
Copy link

We also ran in to this issue. It seems like a consequence of the change in Turbo 8 to treat frame navigations as full requests (with head). I've spent a lot of time investigating the code paths before for different reasons. See: hotwired/turbo#1047 e.g.

@seanpdoyle I think this is probably something that should be on your radar?

seanpdoyle added a commit to seanpdoyle/turbo-rails that referenced this issue Oct 29, 2024
Closes [hotwired#669][]

If a response to a request with the `Turbo-Frame:` header does not
include the `<meta>` elements in the `<html>` document, it's likely that
the browser will remove any `<meta>` element present after handling
navigating the `<turbo-frame>` that originated the request.

In support of testing this behavior, this commit enables CSRF protection
in the test suite.
@seanpdoyle seanpdoyle linked a pull request Oct 29, 2024 that will close this issue
@seanpdoyle
Copy link
Contributor

I've opened #697 in an attempt to resolve this issue.

seanpdoyle added a commit to seanpdoyle/turbo-rails that referenced this issue Oct 29, 2024
Closes [hotwired#669][]

If a response to a request with the `Turbo-Frame:` header does not
include the `<meta>` elements in the `<html>` document, it's likely that
the browser will remove any `<meta>` element present after handling
navigating the `<turbo-frame>` that originated the request.

In support of testing this behavior, this commit enables CSRF protection
in the test suite.
sfnelson added a commit to katalyst/kpop that referenced this issue Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants