Regular expression in drupalSetContent()

    if (preg_match('/var drupalSettings = (.*?);/', $content, $matches)) {
      $this->drupalSettings = drupal_json_decode($matches[1]);
    }

assumes that there are no semicolons in Drupal.settings, but semicolon is a valid character.
There are not that many D8 modules in the wild (yet), but at least lightbox2 and google_analytics for D7 known to use semicolons in Drupal.settings. I'm sure there are other contrib modules as well.

Since $matches[1] contains only part of Drupal.settings, drupal_json_decode() returns false. This, for example, prevents testing of AJAX forms (drupal.Settings.ajax is empty, so drupalPostAjax() cannot determine triggering element's properties.

Attached patch fixes the problem

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

valthebald’s picture

Status: Active » Needs review
FileSize
742 bytes

Status: Needs review » Needs work

The last submitted patch, drupalsetcontent-settings-boundary-1816556.patch, failed testing.

valthebald’s picture

Status: Needs work » Needs review
FileSize
731 bytes

Even simpler

valthebald’s picture

Issue tags: +Novice
miraje’s picture

Status: Needs review » Reviewed & tested by the community

Indeed, the original regexp intends to take the whole value of Drupal.settings js var and put it into $matches[1]. It just doesn't takes into consideration that since a semicolumn will appear inside its value - the parsing won't be correct. In order to take the whole value of the Drupal.settings variable we need to make sure that we are taking the full string. So, the sign of line end should be added to regexp. The 2nd patch should do the job.

catch’s picture

Status: Reviewed & tested by the community » Needs review

This seems like it could have a test written for it.

valthebald’s picture

Oops, seems I missed the last comment...
Anyway, here we go:

1. Test-only patch. I've added trivial setting to test-page (test-page-test module), and check it's value in HttpRequestTest.php. This should fail

2. Combined patch

valthebald’s picture

miraje’s picture

Status: Needs review » Reviewed & tested by the community

Last patch works the same as original (only change is test addition).

Dries’s picture

Status: Reviewed & tested by the community » Needs work

Let's add a bit of documentation to the test please. Could be as short as one line/sentence.

valthebald’s picture

Status: Needs work » Needs review
FileSize
2.26 KB

Here we go (I made comments line similar to "neighbour" comments)
Also, added other "suspicious" characters to the checked settings string.

miraje’s picture

Status: Needs review » Reviewed & tested by the community

Checked again - all working, comment presents.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 8.x. Thanks.

valthebald’s picture

Same patch, rerolled against 7.x

Status: Needs review » Needs work

The last submitted patch, drupalsetcontent-settings-boundary-1816556-d7-combined.patch, failed testing.

valthebald’s picture

Mistakingly used drupal_http_request() instead of DrupalWebTestCase::drupalGet(), fixing

valthebald’s picture

Can this be applied to 7.x as well, please?

mgifford’s picture

Assigned: valthebald » Unassigned
Issue summary: View changes
Status: Needs review » Needs work

No longer applies.

  • Dries committed 551868b on 8.3.x
    Issue #1816556 by valthebald: drupalSetContent() incorrectly defines...

  • Dries committed 551868b on 8.3.x
    Issue #1816556 by valthebald: drupalSetContent() incorrectly defines...

  • Dries committed 551868b on 8.4.x
    Issue #1816556 by valthebald: drupalSetContent() incorrectly defines...

  • Dries committed 551868b on 8.4.x
    Issue #1816556 by valthebald: drupalSetContent() incorrectly defines...