Problem/Motivation

Block page visibility paths are not validated, using path a without leading forward slash does not work.

Steps to Reproduce

  • Install using standard install profile
  • Navigate to /admin/structure/block/manage/bartik_branding
  • Configure pages using 'user/login' path without leading forward slash and 'Show for the listed pages' option selected
  • Save form
  • Observer there is no validation error message.
  • Navigate to home page and observe no branding block rendered as expected
  • Navigate to /user/login path, observe no branding block rendered, unexpected (or expected?)

Proposed resolution

Use RequestPath::validateConfigurationForm to validate for required leading forward slashes.

Remaining tasks

Commit

User interface changes

Provides error message on block config form.

API changes

None

Data model changes

None

Comments

jasonawant created an issue. See original summary.

jasonawant’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.13 KB

I added validateConfigurationForm method to validate the paths. I'd appreciate any feedback regarding the logic, and if we need test coverage. I haven't written any tests, but I'd like to get the experience. Jason

krishkasc’s picture

I have tested the patch and its working fine in drupal 8.2.

krishkasc’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +ChennaiSprint2017

I have tested the patch with drupal 8.2 and properly validating the forward slash on the url key.

tim.plunkett’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Nice work!
This needs automated tests before it can go in

jasonawant’s picture

Hi,

I'd like to work on this test coverage, but would like if someone could confirm I'm heading in the right direct. This will be my first test to write.

I think I need to add a new test in BlockUiTest, which would use drupalPostForm to try to save either of the setUp Tools or Powered by Drupal blocks with the additional page visibility value to trigger the validation error, and then use assertText to see if the error message text is on the page. Does that sound about right?

Well, maybe not, because the new validation occurs within the RequestPath condition, which could be used as a plugin elsewhere.

Instead of expanding the BlockUiTest tests, I should create another test like ConditionFormTest, which appears to only test the node_type condition. I think that's it. Thanks for the feedback, Jason

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

aviral2809’s picture

I am working on this issue.

aviral2809’s picture

<?php

namespace Drupal\Tests\block\Functional;

//namespace Drupal\simpletest;

use \Drupal\Tests\BrowserTestBase;

use \Behat\Mink\WebAssert;
/**
* Tests that the blocked page's visibility paths are validated, using a path without leading forward slash works
*
* @group pages
*/
class ForwardSlashTest extends BrowserTestBase{
protected function setUp() {

parent::setUp();

$this->drupalLogin($this->rootUser);

}

public function testForwardSlash(){

$this->drupalGet('/admin/structure/block/manage/bartik_branding');

$this->submitForm(['visibility[request_path][pages' => 'user/login'], 'Show for the listed pages');

$this->assertText('Paths require a leading forward slash when used with the Pages setting.');

}

public function checkRenderingTest(){

$this->assertSession()->elementExists('css', '#bartik_branding');
}

}

aviral2809’s picture

StatusFileSize
new304.68 KB
new186.62 KB
tameeshb’s picture

Status: Needs work » Needs review

Change issue status to "needs review" for the testbot to test the patch.

jasonawant’s picture

Status: Needs review » Needs work

Hi,

The issue description describes a solution to patch RequestPath::validateConfigurationForm to validate the paths, but I don't see RequestPath.php in the provided patch anymore. Is this the right patch file? It looks like the patch file includes a lot more changes that what is currently in this issue. Was it created with a diff from a different version of Drupal core by mistake?

moshnoi’s picture

Status: Needs work » Needs review
StatusFileSize
new2.99 KB

Status: Needs review » Needs work

The last submitted patch, 13: LeadingForwardSlash-2843992-13.patch, failed testing.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests +Bug Smash Initiative
StatusFileSize
new1.42 KB
new2.41 KB

Rerolled and added tests-only patch.

smustgrave’s picture

StatusFileSize
new1.02 KB
new2.89 KB
new2.01 KB

Didn't realize the tests were incomplete.

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

Manibharathi E R’s picture

StatusFileSize
new197.71 KB
new510.82 KB

Patch #24 tested and Applied successfully on Drupal 9.4.x.

charchil khandelwal’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new51.21 KB
new50.18 KB

Patch no. #24 tested and applied successfully on Drupal 9.4.x version.

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

heni_deepak’s picture

Version: 9.4.x-dev » 9.5.x-dev
heni_deepak’s picture

StatusFileSize
new230.12 KB

#24 patch also working in D9.5

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

The last submitted patch, 24: 2843992-24-tests-only.patch, failed testing. View results

alexpott’s picture

Priority: Minor » Normal
Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/block/tests/src/Functional/BlockTest.php
    @@ -244,6 +244,11 @@ public function testBlock() {
    +    $this->submitForm(['visibility[request_path][pages]' => 'user/login'], 'Save block');
    

    I think the test would be better if we entered two paths with only the second path missing a leading slash.

  2. +++ b/core/modules/system/src/Plugin/Condition/RequestPath.php
    @@ -112,6 +112,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    +    $paths = explode("\r\n", $form_state->getValue('pages'));
    

    I think this should use the same as the logic as the summary method.

    $paths = array_map('trim', explode("\n", $this->configuration['pages']));
    

    The explode "\r\n" looks like it might break on some systems.

  3. +++ b/core/modules/system/src/Plugin/Condition/RequestPath.php
    @@ -112,6 +112,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    +      if (empty($path) || $path === '<front>' || strpos($path, '/') === 0) {
    

    strpos($path, '/') === 0 should be str_starts_with($path, '/') - this will work on both D9 and D10 as we have a PHP 8 polyfil.

  4. +++ b/core/modules/system/src/Plugin/Condition/RequestPath.php
    @@ -112,6 +112,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    +        $form_state->setErrorByName('pages', $this->t('Paths require a leading forward slash when used with the Pages setting.'));
    

    I think the error should contain which path has caused the problem.

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new2.24 KB
new2.1 KB

Addressed the points in #41

41.2 had to use form_state or couldn't get past the error once it triggered.

Status: Needs review » Needs work

The last submitted patch, 42: 2843992-42.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new758 bytes
new2.11 KB

Fixed test case

alexpott’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/block/tests/src/Functional/BlockTest.php
    @@ -244,6 +244,15 @@ public function testBlock() {
    +    $pages = [
    +      '/valid-page',
    +      'user/login',
    +    ];
    

    Let's make this array

        $pages = [
          '',
          '<front>',
          '/valid-page',
          'user/login',
        ];
    
  2. +++ b/core/modules/block/tests/src/Functional/BlockTest.php
    @@ -244,6 +244,15 @@ public function testBlock() {
    +    $this->assertSession()->pageTextContains('user/login require a leading forward slash when used with the Pages setting.');
    
    +++ b/core/modules/system/src/Plugin/Condition/RequestPath.php
    @@ -112,6 +112,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
    +        $form_state->setErrorByName('pages', $path . ' ' . $this->t('require a leading forward slash when used with the Pages setting.'));
    

    $path should be a placeholder in the string. Something like
    '%path requires a leading forward slash when used with the Pages setting.'

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new1.65 KB
new2.18 KB

Addressed points in #45

alexpott’s picture

Status: Needs review » Needs work
+++ b/core/modules/system/src/Plugin/Condition/RequestPath.php
@@ -112,6 +112,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
+        $form_state->setErrorByName('pages', $this->t("The path '%path' requires a leading forward slash when used with the Pages setting.", ['%path' => $path]));

I don't think the quotes are necessary. The % wraps the text in em tags already. I think this is inline with other validation messages.

narendra.rajwar27’s picture

Status: Needs work » Needs review
StatusFileSize
new2.18 KB

Updating patch as suggested in comment #47

Status: Needs review » Needs work

The last submitted patch, 48: 2843992-48.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new1.52 KB
new2.17 KB

@narendra.rajwar27 thank you for the patch but if you are going to contribute please upload an interdiff even for small changes.

Going back to #46

Status: Needs review » Needs work

The last submitted patch, 50: 2843992-50.patch, failed testing. View results

alexpott’s picture

Status: Needs work » Needs review

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Queuing for 10.1.x tests.

sonam.chaturvedi’s picture

StatusFileSize
new515.76 KB

Verified and tested patch #50 on 10.1.x-dev. Patch applied successfully.

Test Steps:
1. Goto Structure > Block Layout
2. Configure any block and select "Pages" tab under Visibility
3. Enter following URL paths - , valid page and "user/login"
4. Click "Save block"

Test Result: Proper validation error message is shown for missing leading forward slash specifying the URL path giving error.

Question: This works only for first path having missing leading forward slash. Should it display all the paths having missing forward slash ?

after patch 50

larowlan’s picture

+++ b/core/modules/system/src/Plugin/Condition/RequestPath.php
@@ -112,6 +112,22 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
+      else {

The else does nothing here, so let's drop it and the return for simplicity sake, less code++.

There's a new string here, so I think that makes this 10.1.x only, tagging as such

smustgrave’s picture

StatusFileSize
new758 bytes
new2.13 KB

Removed else

larowlan’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

This looks RTBC to me now, assuming the bot agrees

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 57: 2843992-57.patch, failed testing. View results

larowlan’s picture

Status: Needs work » Reviewed & tested by the community

Random fail

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 57: 2843992-57.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Another random failure

  • lauriii committed e47f5e75 on 10.1.x
    Issue #2843992 by smustgrave, jasonawant, moshnoi, alexpott: Block page...
lauriii’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

Committed e47f5e7 and pushed to 10.1.x. Thanks!

Status: Fixed » Closed (fixed)

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

kristen pol’s picture

Was looking at this issue and not sure why larowlan and sonam and maybe one or two others didn't get issue credit... but it's closed now so :shrug: :)

lauriii’s picture

I believe I didn't credit @sonam.chaturvedi because at the time of #55 there was already automated tests that verified that the message was triggered. I missed that there was a question in the comment which looking at it now probably should have been addressed. I opened a follow-up for that since I think it's a good suggestion for usability #3356462: Validate all invalid paths in block visibility.

We usually give credit for non-trivial code reviews. It's hard to evaluate how substantial a review is but this time it looks like I decided that the feedback from @larowlan wouldn't qualify for the credit.

kristen pol’s picture

Thank you for the explanation. I’ll reassess how we decide to do manual testing in the future.

cilefen’s picture

A contrib module called Block Exclude Pages was relying on the bug behavior.

#3375092: Drupal 10.1 + Rejecting paths not conforming to standard URL's set by Drupal