Repository navigation
Closes #23335: Adopt Django's LoginRequiredMiddleware - #23663
Merged
Merged
Conversation
Enforce LOGIN_REQUIRED for all UI views via a subclass of Django's LoginRequiredMiddleware, rather than relying on each view to inherit a login mixin. DRF views (which authenticate requests themselves) and the Prometheus metrics endpoint are exempt, and views which handle authentication themselves are marked with @login_not_required. ConditionalLoginRequiredMixin and TokenConditionalLoginRequiredMixin are deprecated for removal in v5.0.
jeremystretch
force-pushed
the
23335-adopt-login-required-middleware
branch
from
November 10, 2026 20:31
c7c805b to
4a28cbd
Compare
jeremystretch
marked this pull request as ready for review
November 10, 2026 20:33
jeremystretch
requested review from
a team and
jnovinger
and removed request for
a team
November 10, 2026 20:39
jnovinger
approved these changes
Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #23335
Adds
netbox.middleware.LoginRequiredMiddleware, a subclass of Django'sLoginRequiredMiddleware. WhenLOGIN_REQUIREDis true, it requires authentication for every UI view unless the view is marked@login_not_required. WhenLOGIN_REQUIREDis false, it does nothing. DRF views (the REST API, including plugin APIs) are exempt, since DRF authenticates requests itself. So is the Prometheus metrics endpoint, so that existing scrapers keep working.LoginView,LogoutView,SocialAuthBeginView,StaticMediaFailureView,MediaViewandNetBoxGraphQLView. The last two keep their own token auth andLOGIN_REQUIREDchecks.ConditionalLoginRequiredMixinfromHomeView,SearchView,MACAddressSetPrimaryViewand the generic feature views. Also removed the dead inline check inHomeView.get().ContentTypePermissionRequiredMixinandObjectPermissionRequiredMixinnow inherit directly fromAccessMixin.ConditionalLoginRequiredMixinandTokenConditionalLoginRequiredMixinare unchanged, but marked deprecated for removal in v5.0.login_not_required(andcsrf_exemptfor views that acceptPOSTrequests from external clients).Departures from the proposal in the issue:
social/djdtnamespace exemptions. The pinnedsocial-auth-app-django(6.1.0) anddjango-debug-toolbar(8.0.0) already decorate their public views with@login_not_required.social:disconnectkeeps its own@login_required./api/path. With the login check gone from the permission mixins, a path-based exemption would leave a non-DRF view mounted under/api/(e.g. a pluginObjectViewinapi_urls) open to anonymous users wheneverEXEMPT_VIEW_PERMISSIONScovers it. The middleware checks theclsattribute that DRF'sas_view()sets instead. All 665 current/api/routes are DRF views, including thecache_page-wrapped schema view.Behavior change: the only core view that becomes newly login-protected is
GraphiQLWorkerView, and only whenLOGIN_REQUIREDis true. The GraphiQL page that loads those workers already requires login, and a headless Chrome check confirmed the workers start for a logged-in session. Plugin views built on a plainViewnow require login whenLOGIN_REQUIREDis true. Plugins that serve intentionally public UI views will needlogin_not_required, so this needs a callout in the v4.8 release notes (not included here, per convention).Testing:
LoginRequiredMiddlewareTestCase(netbox/tests/test_middleware.py). It checks that anonymous UI requests, including a plain plugin view, redirect to login; that authenticated andLOGIN_REQUIRED=Falserequests are allowed; that each exempt endpoint answers for itself rather than redirecting; and that a non-DRF view under/api/still requires login while the metrics URL name is exempt.MediaViewTestCase.test_media_token_authentication: token-authenticated media requests still reach the view withLOGIN_REQUIRED=True.ruff checkpass.LOGIN_REQUIREDon and off, using curl for anonymous, session and token requests, including/metricswith metrics enabled.