kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Validate the next URL in set_language

Open rtibbles opened this issue 2 weeks ago • 2 comments

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/ with next=http://[::1 raises ValueError: Invalid IPv6 URL at views.py#L45.
  • next=/en/user/?<script>alert(1)</script> comes back verbatim in a text/html response, and next=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 sends window.location.href.

The Change

  • set_language should validate next before using it, and only return a path on this Kolibri server.
  • The response shouldn't be HTML.

Acceptance Criteria

  • [ ] next=http://[::1 and next=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 a next containing markup comes back escaped or rejected.
  • [ ] Switching language from the language switcher works as before.
  • [ ] Tests cover a malformed next, an off-site next and a next with 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.

rtibbles avatar Nov 15 '26 20:11 rtibbles

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 next URL 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/plain rather 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!

ShayanMuhammad-CS avatar Nov 26 '26 20:11 ShayanMuhammad-CS

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.

learning-equality-bot avatar Nov 26 '26 20:11 learning-equality-bot