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

CSRF Error (403) When Adding Entry (v1.10.0) #393

Closed
five2seven opened this issue Feb 17, 2022 · 23 comments
Closed

CSRF Error (403) When Adding Entry (v1.10.0) #393

five2seven opened this issue Feb 17, 2022 · 23 comments
Labels
bug Reports of unexpected problems or errors
Milestone

Comments

@five2seven
Copy link

five2seven commented Feb 17, 2022

I can log feedings using a POST (iOS Shortcut) but I can’t create an entry on the actual BabyBuddy site.

9EDB6A32-1A15-41EE-A72E-6113835FD03F

EDIT: Since I know nothing about Django troubleshooting, I just tried pulling 1.9.3 instead and everything is working normally. So Watchtower must have updated my Docker image to the newest and I didn’t realize it. Let me know if there is anymore information I can provide to help get the latest image working.

@five2seven five2seven changed the title CSRF Error (403) When Editing DateTime CSRF Error (403) When Adding Entry Feb 17, 2022
@cdubz
Copy link
Member

cdubz commented Feb 17, 2022

@five2seven just to be clear — you’re saying that you see this issue in v1.10.0 but not v1.9.3?

@five2seven
Copy link
Author

Yep, sorry if I wasn’t clear. The error only shows up with the latest tag (1.10.0).

@cdubz
Copy link
Member

cdubz commented Feb 17, 2022

Ok no worries. I can’t reproduce in the demo so I imagine this has to be with being behind a proxy. I don’t think anything related to this was touch intentionally in the release but Django did have a major version upgrade. Maybe I missed some new change there. Will take a look.

@cdubz cdubz added the bug Reports of unexpected problems or errors label Feb 17, 2022
@cdubz
Copy link
Member

cdubz commented Feb 17, 2022

This looks like the culprit — https://docs.djangoproject.com/en/4.0/releases/4.0/#csrf-trusted-origins-changes-4-0

Does your configuration support making changes to a configuration file easily? Wondering if we’ll need to add an env var to support this for Docker-based deployments…

@five2seven
Copy link
Author

All I have in my Config folder (besides the child photos) is the db.sqlite3 file. The top two folders show empty to me.
197D8E23-300A-499A-B83E-CE2609615ED4

@cdubz
Copy link
Member

cdubz commented Feb 17, 2022

Can you try setting the SECURE_PROXY_SSL_HEADER environment variable to True (or anything, actually) for your deployment? Not sure if this will solve it but it might...

@five2seven
Copy link
Author

Looks like that did it! I’ll report back if I see any more errors but so far all functions seem good. Thank you for such a quick resolution and assistance.

@kazzaw
Copy link

kazzaw commented Feb 17, 2022

Just chiming in, had the same issue. Set environment variable as above to True and working fine now

@ultrara1n
Copy link

Same here, Baby Buddy running behind a nginx reverse proxy with TLS enabled. The enivornment variable seems to fix it.

@cdubz cdubz pinned this issue Feb 17, 2022
@cdubz cdubz changed the title CSRF Error (403) When Adding Entry CSRF Error (403) When Adding Entry (v1.10.0) Feb 17, 2022
@alzyee
Copy link
Contributor

alzyee commented Feb 18, 2022

It seems HA Proxy also needs Use "forwardfor" option. enabled
It is described as

The "forwardfor" option creates an HTTP "X-Forwarded-For" header which contains the client's IP address. This is useful to let the final web server know what the client address was. (eg for statistics on domains)

@ErbeckM
Copy link

ErbeckM commented Feb 18, 2022

SECURE_PROXY_SSL_HEADER environment variable to True

I am running this docker behind Unraid SWAG reverse proxy and this resolved my issue too. Set this in my babybuddy unraid container variables and it works now

@Alberdi
Copy link
Contributor

Alberdi commented Feb 20, 2022

The same error appears when trying to login from the default Gitpod installation.

@cdubz
Copy link
Member

cdubz commented Feb 20, 2022

Thanks for pointing that out, @Alberdi. That makes this at least a bit easier to try to fiddle around with.

For Gitpod specifically after the deploy you can edit babybuddy/settings/base.py to add --

CSRF_TRUSTED_ORIGINS = [
    os.environ.get("GITPOD_WORKSPACE_URL").replace("https://", "https://8000-")
]

cdubz added a commit that referenced this issue Feb 20, 2022
Supports CSRF with Django 4+ (Baby Buddy v1.10.0+).

See #393.
@cdubz
Copy link
Member

cdubz commented Feb 20, 2022

I don't think there is going to be any quick/easy fix for all environments with this change... seems we'll need to add some manner of support for setting CSRF_TRUSTED_ORIGINS via environment variables and good documentation on when and why it is necessary.

26fa988 takes care of Gitpod, at least.

@thijsjek
Copy link

Can you try setting the SECURE_PROXY_SSL_HEADER environment variable to True (or anything, actually) for your deployment? Not sure if this will solve it but it might...

I got the same issue, behind a nginx reverse proxy.
But my docker-cli is not resolving the issue.

docker-cli
docker run -d \
--name=babybuddy \
--net=baby \
--ip=172.22.0.2 \
-e TZ=Europe/Amsterdam \
-e SECURE_PROXY_SSL_HEADER=True \
-e DEBUG=True \
-p 8001:8000 \
-v babybuddy:/config \
--restart unless-stopped \
ghcr.io/linuxserver/babybuddy:latest

Any hints what i am doing wrong?

@cdubz
Copy link
Member

cdubz commented Feb 21, 2022

SECURE_PROXY_SSL_HEADER doesn't seem to resolve the issue in every case. If you set DEBUG=1 you may be able to see the domain issue and you likely need CSRF_TRUSTED_ORIGINS configured (which is not currently possible via environment variables... but I'm working on it).

@johnnypea
Copy link

johnnypea commented Feb 21, 2022

I can't login. Using Heroku + Cloudflare https://support.cloudflare.com/hc/en-us/articles/205893698-Configure-Cloudflare-and-Heroku-over-HTTPS

Debug error page
Screenshot 2022-02-21 at 21 47 07

@cdubz
Copy link
Member

cdubz commented Feb 21, 2022

@johnnypea thanks for adding that use case -- this actually gives me a good way to test this issue and ensure (sort of) it stays fixed in the future. The demo is now serving HTTPS and has the same breakage 😄

@cdubz
Copy link
Member

cdubz commented Feb 22, 2022

Ok CSRF_TRUSTED_ORIGINS support is on HEAD now and I'm going to cut a new release with that, at least. There is some more stuff I want to do though like configure a custom view to give more useful information but I'll break that out.

@cdubz cdubz added this to the v1.10.1 milestone Feb 22, 2022
@cdubz
Copy link
Member

cdubz commented Feb 22, 2022

Closing this out -- but please feel free to continue discussion here as needed!

@cdubz cdubz unpinned this issue Feb 22, 2022
@thijsjek
Copy link

This did the trick. Just make sure the whole URL is in the env.

-e CSRF_TRUSTED_ORIGINS="https://baby.{redacted}.com"

@cdubz
Copy link
Member

cdubz commented Feb 22, 2022

Yeah I'll highlight that (whole URL including scheme) in the documentation as well. Will add to #403.

@johnnypea
Copy link

johnnypea commented Feb 23, 2022

I can also confirm this works. It is not very clear from the documentation if you still need SECURE_PROXY_SSL_HEADER to make this work.

Yeah I'll highlight that (whole URL including scheme) in the documentation as well. Will add to #403.

Maybe you should mention this can be an array as well. You can add all multiple domains.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Reports of unexpected problems or errors
Projects
None yet
Development

No branches or pull requests

9 participants