ASPNet: template compiler csp feature detection#34474
Draft
vorobey wants to merge 1 commit into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds CSP feature detection to the ASP.NET template compiler so it can switch away from new Function-based compilation in CSP-restricted environments and attempt a <script>/globalEval-based fallback.
Changes:
- Introduces CSP detection helpers (
readCspDirective,detectCspRestricted) with memoizedisCspRestricted(). - Adds
compileViaScript()and updates template compilation flow to prefer it when CSP is detected. - Refactors
aspnet.jsformatting/structure (UMD wrapper, indentation, trailing commas).
Comments suppressed due to low confidence (3)
packages/devextreme/js/aspnet.js:97
compileViaScriptwarns when!src.nonce, but the nonce may be present only as an attribute (viagetAttribute('nonce')) depending on the environment. This can produce false warnings and mis-detect whether the compiled script will be allowed by CSP.
if(isCspRestricted() && !src.nonce) {
packages/devextreme/js/aspnet.js:107
src.id.replaceAllwill throw in environments whereString.prototype.replaceAllis not available, and stripping only-does not guarantee a valid function identifier forfunction <name>() {}. This can break template compilation under CSP.
var funcName = src.id.replaceAll('-', '');
var func =
'function ' + funcName + '(obj,encodeHtml){\n' + code + '\n}';
packages/devextreme/js/aspnet.js:178
- When
isCspRestricted()is true andcompileViaScriptreturnsnull(e.g., templates not backed by a<script>tag), the compiler currently returns the raw template text and skips thenew Functionpath. This can break rendering if CSP detection is a false positive (e.g., nonce is present butunsafe-evalis allowed).
if(isCspRestricted()) {
var compiled = compileViaScript(src, code);
return null !== compiled ? compiled : text;
}
Comment on lines
+65
to
+80
| var meta = doc.querySelectorAll( | ||
| 'meta[http-equiv=\'Content-Security-Policy\' i]', | ||
| ); | ||
| for(var j = 0; j < meta.length; j++) { | ||
| var content = meta[j].getAttribute('content') || ''; | ||
| var directive = readCspDirective(content, 'script-src'); | ||
| if(null === directive) { | ||
| directive = readCspDirective(content, 'default-src'); | ||
| } | ||
| if( | ||
| null !== directive && | ||
| -1 === directive.indexOf('\'unsafe-eval\'') | ||
| ) { | ||
| return true; | ||
| } | ||
| } |
Comment on lines
+57
to
+61
| function detectCspRestricted() { | ||
| var doc = window.document; | ||
| var scripts = doc.getElementsByTagName('script'); | ||
| for(var i = 0; i < scripts.length; i++) { | ||
| if(scripts[i].nonce || scripts[i].getAttribute('nonce')) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.