Problem/Motivation

When an anonymous user wants to open a URL which is only accessible to authenticated users, e.g. https://www.example.com/intranet, then they get redirected to the login form and after that probably to the legal_confirm page. That redirect to legal_confirm contains the url argument destination=intranet/ which then leads to this error:

InvalidArgumentException: The user-entered string 'intranet/' must begin with a '/', '?', or '#'. in Drupal\Core\Url::fromUserInput() (Zeile 214 in /var/www/drupal/wohnen/develop/web/core/lib/Drupal/Core/Url.php).

When a user had accepted the T&C before already, the above scenario also redirects to the user login form with the same url argument destination=intranet/, which then redirects correctly.

Not sure yet, why the same destination argument works in one but not the other scenario.

Issue fork legal-3251628

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jurgenhaas created an issue. See original summary.

jurgenhaas’s picture

Status: Active » Needs review

Found the issue in \Drupal\legal\Form\LegalLogin::submitForm and fixed it in the MR3

mkolar’s picture

Status: Needs review » Reviewed & tested by the community

Had same issue, this works for me. I would say RTBC

juliekramer’s picture

I had same issue where the redirect to legal_confirm contains the url argument "destination=something", this fixes the issue

alecsmrekar’s picture

StatusFileSize
new1.29 KB

If the current patch is applied we end up with 2 issues.

#1 The default redirect to /user/ID is broken. You can't use "//user/ID" as a path. That's why the test is failing.

$redirect = '/user/' . $values['uid'];
...
$form_state->setRedirectUrl(Url::fromUserInput('/' . $redirect));

#2 Url destination parameters can come with or without a leading forward slash. Both need to be taken into account.

I'm attaching a patch that mitigates both issues. It also generates the user URL in better way, URL hardcoding should be avoided.

alecsmrekar’s picture

Status: Reviewed & tested by the community » Needs review
plach’s picture

+++ b/src/Form/LegalLogin.php
@@ -216,14 +217,23 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
+      $destination_path = $_GET['destination'];

Let's use the redirect.destination service, since we're touching this line.

alecsmrekar’s picture

StatusFileSize
new3.06 KB

Fixed in this patch. I also removed some unused class properties: $database, $moduleHandler, and $languageManager.

alecsmrekar’s picture

StatusFileSize
new3.05 KB

Fixing the last patch

alecsmrekar’s picture

StatusFileSize
new2.84 KB

Added a fix to prevent the from from redirecting to itself.

anybody’s picture

Status: Needs review » Needs work

@alecsmrekar please don't use patches on an issue where a MR was used. Please incorporate the (only issue related!!) changes into the MR so we can see the interdiff. All other changes should go into separate issues please.

jaydarnell’s picture

This no longer appears to be an issue. As mentioned above, the initial patch supplied here: https://git.drupalcode.org/project/legal/-/merge_requests/3.patch actually breaks the redirect altogether (in the 3.x branch at least). After I removed this patch I attempted to recreate the initial bug reported in this issue but cannot. I suspect the problem was fixed elsewhere and this issue merely needs to be closed.

frob’s picture

I have not tried any of the patches, however, I can confirm that this issue is still present on 3.0.1

anybody’s picture

Version: 2.0.x-dev » 3.0.x-dev

So do we need a new stable release or is this also still happening in 3.0.x @frob?

frob’s picture

I am unable to test the new version at the moment. However, the test is pretty simple. If you visit `/user/login?destination=internet` then it should break with the OP's error.

To fix this ourselves we patched the `public/modules/contrib/legal/src/Form/LegalLogin.php` file to ensure the destination is valid. It looks like you where leaning toward a solution which involved the redirection service so I didn't submit my patch.

Replace

      $redirect = $_GET['destination'];

with

      $destination_path = $_GET['destination'];
      if (strpos($destination_path, '/') !== 0) {
        $destination_path = '/' . $destination_path;
      }
      $redirect = $destination_path;

robert castelo’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.