Thanks to visit codestin.com
Credit goes to github.com

Skip to content

jsDeliver packages that use special characters in their 'import' statements fail the IMPORT_REGEX test in jsDelivrEsmResolver.php #57969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
IanAtOcucom opened this issue Aug 9, 2024 · 8 comments

Comments

@IanAtOcucom
Copy link

IanAtOcucom commented Aug 9, 2024

Symfony version(s) affected

6.4 and upward

Description

The package @progress/kendo-ui imports jQuery as jQuery$1 (url https://cdn.jsdelivr.net/npm/@progress/[email protected]/+esm )

This breaks the regex at https://github.com/symfony/asset-mapper/blob/7.1/ImportMap/Resolver/JsDelivrEsmResolver.php#L31

How to reproduce

Try to require any package where the import statement uses a valid javascript variable name (any char, number class plus $, _) that doesn't match the regex \w. as an example, @progress/kendo-ui which requires jsQuery using the variable name jQuery$1

Possible Solution

The regex could be rewritten to '#(?:import\s*(?:[\w\$\d]+,)?(?:(?:\{[^}]*\}|[\w\$\d]+|\*\s*as\s+\w+)\s*\bfrom\s*)?|export\s*(?:\{[^}]*\}|\*)\s*from\s*)("/npm/((?:@[^/]+/)?[^@]+?)(?:@([^/]+))?((?:/[^/]+)*?)/\+esm")#' to capture $, but this is still fraught with danger as javascript variable names may include Unicode surprises.

Additional Context

Additionally, if the content is very large, there may be pcre.backtrack_limit errors. (trying to replace the [\w\$\d] references with [^\s] references would lead to NULL results in preg_replace_callback due to backtrack errors in the regex.)

This feature may need to be completely rewritten to avoid regular expressions.

@smnandre
Copy link
Member

I started something some time ago, maybe it's time to rework on it #54134

@IanAtOcucom
Copy link
Author

IanAtOcucom commented Aug 14, 2024

For the immediate need, [\w\d\$] would be enough to cover most Latin-alphabet variable names, ignoring people who might want to write perfectly-valid import 변하기쉬운것 from "도서관";

And this is an immediate need.

@smnandre
Copy link
Member

Do you want to open a PR with your suggestion?

@IanAtOcucom
Copy link
Author

I do not. I am not well-positioned to contribute to OSS projects in this role.

@smnandre
Copy link
Member

Can you check if this PR solves your problem ? #58046

@IanAtOcucom
Copy link
Author

That PR Resolves my issue, thanks!

nicolas-grekas added a commit that referenced this issue Aug 21, 2024
This PR was merged into the 6.4 branch.

Discussion
----------

[AssetMapper] Fix JsDeliver import regexp

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #57969
| License       | MIT

Following import were not handled in the JSDeliverEsmResolver, due to the `$` character.

```js
import jQuery$1 from "/npm/[email protected]/+esm";
```

This PR updates the regex to handle this case.

Commits
-------

45f3835 [AssetMapper] Fix JsDeliver import regexp
@IanAtOcucom
Copy link
Author

This bug also affects 7.x

@smnandre
Copy link
Member

Yep, this PR targets the lowest maintained version concerned by the bug (6.4), and symfony mergers will propagate the fix up to the most recent one (7.1 & 7.2)

https://symfony.com/doc/current/_build/maintainer_guide.html#step-3-merge-it-into-the-other-branches

It's just a matter of time ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants