Problem/Motivation

I found out about this issue when trying to add a file with a query string through drupal_add_js(). I needed to do so to have a dynamic .js file (through php) work with my module.

Basically, when attempting to do so, the question mark and ampersand will be escaped to their percentage values in the following function calls

common.inc
-> drupal_get_js()
-> file_create_url()
-> drupal_encode_path()
-> rawurlencode()

As this could rename a file to example.js%3Ffoo%3Dbar%26baz%3Dbaw, it can lead to errors because of the following code in common.inc -> drupal_get_js()

$query_string_separator = (strpos($item['data'], '?') !== FALSE) ? '&' : '?';
$js_element['#attributes']['src'] = file_create_url($item['data']) . $query_string_separator . ($item['cache'] ? $query_string : REQUEST_TIME);

This code actually checks for a query string marker (?) and sets a separator based on it, even though there will be no question mark, but %3F, after file_create_url() is called on the URI.

Proposed resolution

I made a patch which correctly separates the already present query string from the file name and reattaches it after the file name has been turned into a URI.

Remaining tasks

Patch needs reviewing (will be supplied in next post)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kristiaanvandeneynde’s picture

As promised: the patch

Status: Needs review » Needs work

The last submitted patch, fixed-drupal-get-js-bug-1320996-1.patch, failed testing.

kristiaanvandeneynde’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, fixed-drupal-get-js-bug-1320996-1.patch, failed testing.

kristiaanvandeneynde’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, fixed-drupal-get-js-bug-1320996-1.patch, failed testing.

kristiaanvandeneynde’s picture

Version: 8.x-dev » 7.x-dev
Status: Needs review » Needs work

EDIT: See comment below, changing patch from D7 to D8 to avoid test bot problems (?) and to comply with patch submitting guide.

kristiaanvandeneynde’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs work » Needs review
FileSize
2.19 KB

Patched it in Drupal 8 so it can be backported.

Also, the patch in comment 1 contained a logical error in the adjustments to drupal_attributes.

Version: 7.x-dev » 8.x-dev

The last submitted patch, fixed-drupal-get-js-bug-1320996-7.patch, failed testing.

kristiaanvandeneynde’s picture

All changes above to drupal_attributes were unjust.
Ampersands should be escaped in HTML attributes as well.

As a result, the patch now only contains the working fix to drupal_get_js() and can be backported to D7.

kristiaanvandeneynde’s picture

Status: Needs work » Needs review
kscheirer’s picture

kscheirer’s picture

Issue summary: View changes

Removed any reference to drupal_attributes, which was unjustly changed

Status: Needs review » Needs work

The last submitted patch, 10: fixed-drupal-get-js-bug-1320996-10.patch, failed testing.

coredumperror’s picture

Any chance that this issue could get some love, please? It completely breaks the ability to include the Google Map Javascript API's libraries with any of the normal javascript inclusion mechanisms. For example, including the maps API with the Drawing library uses a URL like this:

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=drawing

Since the ampersand gets URL-encoded, both GET args become mangled together.

The only solution I've found is this:

 $script = array(
    '#type' => 'markup',
    '#markup' => "<script src='https://maps.googleapis.com/maps/api/js?key=$api_key&libraries=drawing'></script>"
  );
  drupal_add_html_head($script, 'gogole_maps_api_script_tag');

Every other method of adding a script tag to the page (drupal_add_js(), $form['#attached']) mangles the ampersand.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.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.

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.

quietone’s picture

Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

drupal_get_js was removed from Drupal 8.0.x in #2368797: Optimize ajaxPageState to keep Drupal 8 sites fast on high-latency networks, prevent CSS/JS aggregation from taking down sites and use HTTP GET for AJAX requests.

Therefore, closing as outdated. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.

Thanks!