-
Notifications
You must be signed in to change notification settings - Fork 2.7k
API POST requests fail when REMOTE_AUTH_HEADER is enabled #18914
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
Comments
For a minimal, self-contained example to reproduce this situation, you can clone https://github.com/netbox-community/netbox-docker.git and add these files: docker-compose.override.yml
Add to netbox.env:
httpd.conf
|
Let me know if you'd like me to submit a patch for this. I think it's as simple as just adding those 2 lines I wrote above to the remote auth middleware. |
@llamafilm I can assign to you if you want to work on a PR for this? |
Yes please assign to me |
Hey @llamafilm , I was assigned to review the PR for this and have been looking at the reported issue. I've come to the conclusion that I don't think it is a valid issue. For the REST API, NetBox supports 2 kinds of authentication:
Note that Remote User Auth is not listed as a supported mechanism. I've gathered from your Arthur did point out that you might be trying to use NetBox's ability to read-only from the REST API via the I think the take away here is that your example Please let me know if I've misunderstood the situation. |
@jnovinger I just forgot to include the API token in my example curl command. I've updated the comment now, and the issue remains. I just updated the PR for compatibility with Django 5.2 (Netbox 4.3). |
Please, please make sure to include details like that in future reports as it makes a huge difference when trying to understand the issue. I assume the remote user authentication is used for other reasons, since you are in fact using token auth for the REST API. In that case, your proxy needs to make sure that the header ( If the remote user header is sent with these requests, then--due to middleware running well before DRF has a chance to look at the API token--DRF will treat this as a session-based authenticated request and enforce CSRF validation as designed. |
Sorry about that. I'll try to add some more context here, since obviously this is a contrived example; in production, we run a customized version of Envoy which is tied into our SSO / identity system. The proxy validates the caller's identity (whether it be a user or an app) and passes that identity to Netbox as a header. Envoy does have an option to disable authz for specific paths, but we would not want to do that because then anybody in possession of a Netbox token would be able to access the API. Those tokens are long-lived and possible to share accidentally. Enforcing authz in the proxy improves our security posture. So in this scenario, the API token is sort of redundant because Netbox already knows the caller's identity and can map it to a Netbox user (and we trust the proxy does its job properly). But that's fine, I'm happy to include the token anyway. |
Deployment Type
Self-hosted
NetBox Version
v4.2.4
Python Version
3.12
Steps to Reproduce
REMOTE_AUTH_ENABLED
andREMOTE_AUTH_HEADER
in Netbox configcurl http://localhost:8000/api/dcim/sites/ -d '{"name": "site1", "slug": "site1"}'
Expected Behavior
The request should succeed without "logging in". The same as how API requests work when you're not using
REMOTE_AUTH_HEADER
and you provide an API token in the Authorization header.Observed Behavior
The request fails with HTTP 403:
The problem lies in Netbox customized version of
RemoteUserMiddleware
. It runsauth.login()
which causes Django to enforce CSRF. This of course fails, because a webhook cannot contain a CSRF token.I can workaround this by writing my own custom
REMOTE_AUTH_BACKEND
class, and using this logic to skip API requests:This avoids the
login()
and the request succeeds, but this causes another minor problem: Every API request triggers auser_login_failed
signal which prints this message to the log:Furthermore, it should be possible to use
REMOTE_AUTH_HEADER
without writing a custom class.The text was updated successfully, but these errors were encountered: