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

Skip to content

[AssetMapper] Add integrity hash to the default es-module-shims script #53251

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

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
*/
class ImportMapRenderer
{
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL = 'https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js';
// https://generator.jspm.io/#S2NnYGAIzSvJLMlJTWEAAMYOgCAOAA
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL = 'https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js';
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_INTEGRITY = 'sha384-+dzlBT6NPToF0UZu7ZUA6ehxHY8h/TxJOZxzNXKhFD+5He5Hbex+0AIOiSsEaokw';

public function __construct(
private readonly ImportMapGenerator $importMapGenerator,
Expand All @@ -47,7 +49,7 @@ public function render(string|array $entryPoint, array $attributes = []): string
$importMap = [];
$modulePreloads = [];
$cssLinks = [];
$polyFillPath = null;
$polyfillPath = null;
foreach ($importMapData as $importName => $data) {
$path = $data['path'];

Expand All @@ -58,7 +60,7 @@ public function render(string|array $entryPoint, array $attributes = []): string

// if this represents the polyfill, hide it from the import map
if ($importName === $this->polyfillImportName) {
$polyFillPath = $path;
$polyfillPath = $path;
continue;
}

Expand Down Expand Up @@ -102,22 +104,31 @@ public function render(string|array $entryPoint, array $attributes = []): string
</script>
HTML;

if (false !== $this->polyfillImportName && null === $polyFillPath) {
if (false !== $this->polyfillImportName && null === $polyfillPath) {
if ('es-module-shims' !== $this->polyfillImportName) {
throw new \InvalidArgumentException(sprintf('The JavaScript module polyfill was not found in your import map. Either disable the polyfill or run "php bin/console importmap:require "%s"" to install it.', $this->polyfillImportName));
}

// a fallback for the default polyfill in case it's not in the importmap
$polyFillPath = self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL;
$polyfillPath = self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL;
}

if ($polyFillPath) {
$url = $this->escapeAttributeValue($polyFillPath);
if ($polyfillPath) {
$url = $this->escapeAttributeValue($polyfillPath);
$polyfillAttributes = $scriptAttributes;

// Add security attributes for the default polyfill hosted on jspm.io
if (self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL === $polyfillPath) {
$polyfillAttributes = $this->createAttributesString([
'crossorigin' => 'anonymous',
'integrity' => self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_INTEGRITY,
] + $attributes);
}

$output .= <<<HTML

<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support -->
<script async src="$url"$scriptAttributes></script>
<script async src="$url"$polyfillAttributes></script>
HTML;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function testDefaultPolyfillUsedIfNotInImportmap()
);
$html = $renderer->render(['app']);
$this->assertStringContainsString('<script async src="https://ga.jspm.io/npm:es-module-shims@', $html);
$this->assertStringContainsString('es-module-shims.js" crossorigin="anonymous" integrity="sha384-', $html);
}

public function testCustomScriptAttributes()
Expand Down