Validate the next URL in set_language
Overview
POST /<lang>/i18n/setlang/ doesn't validate the next field. A malformed URL such as http://[::1 returns a 500, and any other value, including another site's URL, is echoed back in an HTML response that the language switcher then redirects to. It is reachable without logging in, though only with a CSRF token.
Complexity: Low Target branch: develop
Context
- On develop at fe298f5449,
POST /en/i18n/setlang/withnext=http://[::1raisesValueError: Invalid IPv6 URLatviews.py#L45. next=/en/user/?<script>alert(1)</script>comes back verbatim in atext/htmlresponse, andnext=https://evil.example/en/user/comes back with its scheme and host (views.py#L45-L83). The language switcher redirects to the returned URL (mixin.js). The switcher itself sendswindow.location.href.
The Change
set_languageshould validatenextbefore using it, and only return a path on this Kolibri server.- The response shouldn't be HTML.
Acceptance Criteria
- [ ]
next=http://[::1andnext=http://[return 400. - [ ]
next=https://evil.example/en/user/returns a same-origin path or a 400, never the other host. - [ ] The response is not
text/html, and anextcontaining markup comes back escaped or rejected. - [ ] Switching language from the language switcher works as before.
- [ ] Tests cover a malformed
next, an off-sitenextand anextwith markup.
AI usage
I had Claude Code sweep Kolibri's API code for unvalidated request input and reproduce each 500 with a test. I chose the fix and the split into sub-issues. Claude Code drafted the issue to the pattern of #15838.
Hi Learning Equality team,
I'd be happy to work on this issue if it's open for community contributions.
My proposed approach is to:
- Validate the
nextURL and safely reject malformed URLs, invalid hosts, and off-site or protocol-relative redirects. - Ensure redirects always return relative same-origin paths, including when the incoming URL is an absolute URL.
- Return the redirect target as
text/plainrather than HTML. - Handle URL path decoding consistently to avoid double-decoding issues.
I'd also add regression tests covering malformed IPv6 URLs, off-site and backslash-based redirects, markup, query strings and fragments, same-origin absolute URLs, and encoded paths.
Does this approach align with your expectations? Would you welcome a PR targeting develop?
Thanks!
Hi! 👋
Thanks so much for your interest! This issue is not open for contribution. Visit Contributing guidelines to learn about the contributing process and how to find suitable issues. If there are no unassigned 'help wanted' issues available, please wait until new ones are added.
We really appreciate your willingness to help. 😊
Are you preparing for Google Summer of Code? See our GSoC guidelines.