Skip to content

Fix open redirect in user key login - #126

Open
Ewa55 wants to merge 3 commits into
catalyst:MOODLE_405_STABLEfrom
Ewa55:fix-124-wantsurl-open-redirect
Open

Ewa55 wants to merge 3 commits into
catalyst:MOODLE_405_STABLEfrom
Ewa55:fix-124-wantsurl-open-redirect

Conversation

@Ewa55

@Ewa55 Ewa55 commented Aug 13, 2026 •

Copy link
Copy Markdown

Fixes #124.

This change restricts external redirects after user key authentication.

External redirect URLs are now allowed only when their host is explicitly configured in the new 'allowedredirecthosts' setting. Local Moodle URLs continue to work without additional configuration.

The setting accepts a semicolon-separated list of allowed hosts, for example:

example.com;example.org

If an external host is not on the allowed list, the user is redirected to the Moodle site instead.

Manual testing instructions

  1. Generate a valid user key login URL.
  2. Verify that a local relative wantsurl, for example /course/index.php, redirects successfully.
  3. Verify that an external wantsurl whose host is not configured in allowedredirecthosts redirects to the Moodle site URL instead.
  4. Add a trusted external host, for example example.com, to the Allowed redirect hosts setting.
  5. Verify that an external wantsurl using the configured host redirects successfully to the requested external URL.

Automated tests

Updated and extended the PHPUnit tests to cover:

  • local relative wantsurl values;
  • external URLs blocked by default;
  • external URLs allowed when the host is configured in allowedredirecthosts;
  • protocol-relative URLs being blocked.

All auth_userkey PHPUnit tests pass successfully.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request addresses the open redirect vulnerability in the auth_userkey login flow by introducing redirect hardening after key-based authentication, allowing external redirects only when explicitly configured.

Changes:

  • Adds an allowedredirecthosts admin setting (semicolon-separated) to allowlist external redirect targets.
  • Routes post-authentication redirect selection through a new get_redirect_url() helper that enforces host-based restrictions.
  • Adds new language strings describing the allowlist setting.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
settings.php Adds the new auth_userkey/allowedredirecthosts configuration setting.
lang/en/auth_userkey.php Adds UI strings for the new allowlist setting and its help text.
auth.php Introduces get_redirect_url() and uses it to restrict redirects after userkey login.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread auth.php
Comment on lines +686 to +692

$redirecthost = parse_url($wantsurl, PHP_URL_HOST);
$localhost = parse_url($CFG->wwwroot, PHP_URL_HOST);

if (empty($redirecthost) || empty($localhost)) {
return $CFG->wwwroot;
}
Comment thread auth.php
Comment on lines +699 to +703
if (empty($this->config->allowedredirecthosts)) {
return $CFG->wwwroot;
}

$allowedhosts = explode(';', $this->config->allowedredirecthosts);
@dmitriim

Copy link
Copy Markdown
Member

@Ewa55 thanks for submitting the patch. It mostly looks good. However, a couple of things still needs to be addressed before merging:

  1. Missing automated tests supporting this change.
  2. Existing unit tests are failing after this change.
  3. Can you please add manual testing instructions so we could verify the fix.
  4. Would be nice to update README and describe a new feature.
  5. Review Copilot feedback and see if it's useful and worth attention.

Hope that's not much. Thanks!

Note: I haven't completed full review as the code may change after fixing all those items.

@dmitriim

Copy link
Copy Markdown
Member

Thanks @Ewa55
We will review and test the change in the next week or so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security issue: Open redirect after key-based login

3 participants