-
Notifications
You must be signed in to change notification settings - Fork 155
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
Cannot get basic version of in-browser Javascript application documentation working #158
Comments
The error always comes from
err is always "http: named cookie not present" So it seems that axios is not sending it? |
someone know it? |
I actually figured this out, at least for serving on localhost: https://github.com/francoposa/go-csrf-examples (sorry for no documentation yet but the code is simple). It's about the CORS settings, here's what I use for the API server (see ---
server:
host: localhost
port: 8080
timeout:
server: 30
read: 15
write: 10
idle: 5
cors:
allowCredentials: true
allowedHeaders:
- X-CSRF-Token
exposedHeaders:
- X-CSRF-Token
allowedOrigins:
- http://localhost*
debug: true
csrf:
secure: false # false in development only!
key: place-your-32-byte-long-key-here
cookieName: csrf
header: X-CSRF-Token For the UI side, I wrote a quick static file server so that the JavaScript is served from localhost. Just opening the index.html file in the browser will not register to the API server as the requests coming from localhost. Also see that Axios lowercases all the headers it receives from the response: https://github.com/francoposa/go-csrf-examples/blob/main/ui/axios-js/web/static/index.js#L6 |
@francoposa are there specific changes to the docs you can suggest given the above? |
I have forked with the intention of doing all of the below, but have been otherwise occupied since then. If anyone feels inspired to tackle it before I get to it, I do feel pretty confident that the CORS configuration in my example repo is the absolute minimum config to get this working, with no extra stuff. I played around with this for days trying to get it as simple as possible. Documentation updates in order of effort and helpfulness least to most:
|
@francoposa thanks for the direction. I'll throw updated docs together if you want to hand that off. |
This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days. |
Not stale; still an issue and the PR to fix has not been looked at to my knowledge |
Fixes #158, which is essentially that 1. none of the examples in the README for working with a JavaScript frontend will work without proper CORS config on the backend 2. there is no example at all for using the HTTP header instead of getting the CSRF token from the hidden form field **Summary of Changes** I have merged/copied over these simplified examples from my own repository of working examples. I was not sure how the maintainers may want to reference these examples in the main README. Copying them over to the README verbatim would be putting a lot of code into the README, but without changing the current README, the content there differs significantly from the examples. --------- Co-authored-by: Corey Daley <[email protected]>
I cannot get any version of the documentation working with in-browser Javascript.
Everything works fine with curl/Postman. Browser javascript involves introducing a CORS library, but I do not think that is the problem, since after the CORS is introduced Postman workflow still works and the CORS library debug output does not show any issues.
Here is my code, stripped down as much as possible. I use Chi router because it is what I am familiar with, but that difference from the docs should not matter here.
go.mod:
Go Code:
Javascript:
The text was updated successfully, but these errors were encountered: