Align InitializeAppView redirect validation with RootURLRedirectView pattern
❌ This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues.
Overview
Restructure the redirect-target logic in InitializeAppView (kolibri/core/device/api.py) to match the validate-then-use pattern used elsewhere in the codebase (kolibri/core/views.py:RootURLRedirectView). This drops the HTTP_REFERER fallback and tightens the data flow into HttpResponseRedirect without changing the underlying same-host validation policy.
Complexity: Low Target branch: develop
Context
InitializeAppView is hit during app-context login flows. It accepts an auth_token and a ?next= parameter, logs the user in as an OS-bound FacilityUser, then redirects to next so the app lands on the user's intended page.
Current validation (lines 511–529 of kolibri/core/device/api.py):
redirect_url = request.GET.get("next", "/")
if (redirect_url or not request.is_ajax()) and not url_has_allowed_host_and_scheme(
url=redirect_url,
allowed_hosts={request.get_host()},
require_https=request.is_secure(),
):
redirect_url = request.META.get("HTTP_REFERER")
if redirect_url:
redirect_url = urlunquote(redirect_url)
if not url_has_allowed_host_and_scheme(
url=redirect_url,
allowed_hosts={request.get_host()},
require_https=request.is_secure(),
):
redirect_url = "/"
response = HttpResponseRedirect(redirect_url)
The validation itself is not unsafe — it follows Django's set_language precedent — but the surface is wider than the feature needs, and the use-then-validate-with-fallback shape makes the data flow harder to audit.
The Change
Restructure the redirect-target logic in InitializeAppView to match the validate-then-use pattern in kolibri/core/views.py:RootURLRedirectView.get_redirect_url:
- Initialise
redirect_url = "/". - Read
?next=from the request. - If
nextis non-empty and passesurl_has_allowed_host_and_scheme(url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure()), setredirect_url = next. - Pass
redirect_urltoHttpResponseRedirect.
This drops the HTTP_REFERER fallback (and its urlunquote import) and brings InitializeAppView in line with the rest of the codebase's redirect policy. Same-host validation is unchanged — fully-qualified URLs pointing at the current host still work; anything else falls back to /. The validate-then-use shape also makes the data flow into HttpResponseRedirect unambiguous for static-analysis tooling.
Out of Scope
- Refactoring
core/views.py:RootURLRedirectView.get_redirect_url. We're aligning with its pattern, not changing it. - Extracting a shared
safe_next_urlhelper. Useful future work; not part of this task. - Frontend behaviour changes for callers that pass absolute same-host URLs — those still work.
- The
set_app_key_on_responsecall that follows the redirect construction.
Acceptance Criteria
General
- [ ]
InitializeAppViewredirect logic restructured to validate-then-use, matchingcore/views.py:RootURLRedirectView.get_redirect_url. - [ ] Same-host validation (
url_has_allowed_host_and_schemewithallowed_hosts={request.get_host()}) preserved. - [ ]
HTTP_REFERERfallback (and theurlunquoteimport that supports it) removed.
Testing
- [ ] A same-host relative path (
?next=/learn/) redirects to that path. - [ ] A fully-qualified URL pointing at the same host redirects to that URL.
- [ ] A fully-qualified URL pointing at a different host redirects to
/. - [ ] A protocol-relative URL (
?next=//evil.com/foo) redirects to/. - [ ] An absent
nextparameter redirects to/. - [ ] A request with
HTTP_REFERERset and an invalidnextredirects to/.
References
- The reference pattern:
kolibri/core/views.py:RootURLRedirectView.get_redirect_url - Django docs:
url_has_allowed_host_and_scheme
AI usage
Drafted with Claude during a triage session. I pushed back on an early path-stripping approach in favour of aligning with core/views.py's validate-then-use pattern. Reviewed section by section before creation.
Plan: Restructure InitializeAppView's redirect logic from the current use-then-validate-with-referer-fallback pattern to a clean validate-then-use pattern, removing the HTTP_REFERER fallback and the urlunquote import.
- [ ] Write failing tests for the new redirect logic
- [ ] Implement the validate-then-use redirect logic
- [ ] Simplify pass
@rtibblesbot's comments are generated by an LLM, and should be evaluated accordingly
How was this generated?
- Implemented code following the pre-approved plan using test-driven development (red-green-refactor)
- Ran the project's full test suite and linter before committing
- For UI changes: captured live screenshots to verify rendering
- For backend changes: verified with example API/CLI output