Exception: 'Only local files should be passed to _locale_parse_js_file().' for local files with a colon (":") in the path

Created on 23 January 2024, about 1 year ago

Problem/Motivation

Locale.module incorrectly flags URLs with a colon (":") in their paths as external, throwing an Exception.

Code in locale.module tries to detect whether a file is local or not before attempting to extract strings from it. If it determines that the file is not local, it throws an exception. The problem is how it determines whether the file is local or not -- it checks for the presence of a colon (":") to determine whether it is local, assuming that any file path that contains a colon must be external. Colons are valid for HTML paths and also are allowed in files in both MacOS and Linux filesystems, though, so this test is inaccurate. Fortunately, UrlHelper:: already contains static methods to determine whether a URL is local or external.

In my case, I hit this exception when trying to attach a theme's library to a render array, as the library included a JS file with a colon in the path:

$ cat games_theme.libraries.yml
[snip]
cg:tng-game:
  js:
    js/cg:tng-game.js: {}

Note: Per RFC 3986 Section 3.3 (Path), a colon is a valid character for inclusion in paths.

Steps to reproduce

  1. Define a library with a local JS file that contains a colon in path, as above
  2. Add that library as an #attachment to a page's render array
  3. Enable locale.module, clear cache
  4. Surf to the page in a browser
  5. Verify Exception: Only local files should be passed to _locale_parse_js_file()., although file is indeed a local file

Proposed resolution

Alter the code in locale.module to use UrlHelper::isExternal() and !UrlHelper::externalIsLocal() to properly determine whether the file should be read for string extraction.

Merge request link

https://git.drupalcode.org/project/drupal/-/merge_requests/6294

Remaining tasks

Write a unit test case for this, if needed.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

Locale.module incorrectly flags URLs with a colon (":") in their paths as external, throwing an Exception. Fixed.

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
LocaleΒ  β†’

Last updated 4 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States josephcheek Provo, UT

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024