Skip to content
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

Accept additional attributes for assets to support SRI #436

Merged
merged 4 commits into from
Sep 25, 2019
Merged
Changes from 2 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
55 changes: 48 additions & 7 deletions packages/server/src/ChunkExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ function extraPropsToString(asset, extraProps) {
)
}

function getSriHtmlAttributes(asset) {
if (!asset.integrity) {
return ''
}
return ` integrity="${asset.integrity}"`
}

function assetToScriptTag(asset, extraProps) {
return `<script async data-chunk="${asset.chunk}" src="${
asset.url
}"${extraPropsToString(asset, extraProps)}></script>`
}"${getSriHtmlAttributes(asset)}${extraPropsToString(asset, extraProps)}></script>`
}

function assetToScriptElement(asset, extraProps) {
Expand Down Expand Up @@ -66,7 +73,7 @@ function assetToStyleString(asset, { inputFileSystem }) {
function assetToStyleTag(asset, extraProps) {
return `<link data-chunk="${asset.chunk}" rel="stylesheet" href="${
asset.url
}"${extraPropsToString(asset, extraProps)}>`
}"${getSriHtmlAttributes(asset)}${extraPropsToString(asset, extraProps)}>`
}

function assetToStyleTagInline(asset, extraProps, { inputFileSystem }) {
Expand Down Expand Up @@ -128,7 +135,7 @@ function assetToLinkTag(asset, extraProps) {
const hint = LINK_ASSET_HINTS[asset.type]
return `<link ${hint}="${asset.chunk}" rel="${asset.linkType}" as="${
asset.scriptType
}" href="${asset.url}"${extraPropsToString(asset, extraProps)}>`
}" href="${asset.url}"${getSriHtmlAttributes(asset)}${extraPropsToString(asset, extraProps)}>`
}

function assetToLinkElement(asset, extraProps) {
Expand Down Expand Up @@ -325,6 +332,24 @@ class ChunkExtractor {
return smartRequire(mainAsset.path)
}

getSriFromFileName(filename) {
if (!filename) {
return null
}

const asset = this.stats.assets.find(x => x.name === filename)
if (!asset) {
return null
}

const { integrity } = asset
if (!integrity) {
return null
}

return { integrity }
}

// Main assets

getMainAssets(scriptType) {
Expand All @@ -340,7 +365,10 @@ class ChunkExtractor {
const requiredScriptTag = this.getRequiredChunksScriptTag(extraProps)
const mainAssets = this.getMainAssets('script')
const assetsScriptTags = mainAssets.map(asset =>
assetToScriptTag(asset, extraProps),
assetToScriptTag({
...asset,
...this.getSriFromFileName(asset.filename),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these is useless, the integrity should already be in asset. The only needed part is : getSriHtmlAttributes and all usage of it.

}, extraProps)
)
return joinTags([requiredScriptTag, ...assetsScriptTags])
}
Expand All @@ -351,7 +379,10 @@ class ChunkExtractor {
)
const mainAssets = this.getMainAssets('script')
const assetsScriptElements = mainAssets.map(asset =>
assetToScriptElement(asset, extraProps),
assetToScriptElement({
...asset,
...this.getSriFromFileName(asset.filename),
}, extraProps),
)
return [requiredScriptElement, ...assetsScriptElements]
}
Expand All @@ -366,7 +397,12 @@ class ChunkExtractor {

getStyleTags(extraProps = {}) {
const mainAssets = this.getMainAssets('style')
return joinTags(mainAssets.map(asset => assetToStyleTag(asset, extraProps)))
return joinTags(mainAssets.map(asset =>
assetToStyleTag({
...asset,
...this.getSriFromFileName(asset.filename),
}, extraProps)
))
}

getInlineStyleTags(extraProps = {}) {
Expand All @@ -379,7 +415,12 @@ class ChunkExtractor {

getStyleElements(extraProps = {}) {
const mainAssets = this.getMainAssets('style')
return mainAssets.map(asset => assetToStyleElement(asset, extraProps))
return mainAssets.map(asset =>
assetToStyleElement({
...asset,
...this.getSriFromFileName(asset.filename),
}, extraProps)
)
}

getInlineStyleElements(extraProps = {}) {
Expand Down