Skip to content

ASPNet: template compiler csp feature detection#34474

Draft
vorobey wants to merge 1 commit into
DevExpress:mainfrom
vorobey:feature/aspnet-template-compiler-csp-detect
Draft

ASPNet: template compiler csp feature detection#34474
vorobey wants to merge 1 commit into
DevExpress:mainfrom
vorobey:feature/aspnet-template-compiler-csp-detect

Conversation

@vorobey

@vorobey vorobey commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 memoized isCspRestricted().
  • Adds compileViaScript() and updates template compilation flow to prefer it when CSP is detected.
  • Refactors aspnet.js formatting/structure (UMD wrapper, indentation, trailing commas).
Comments suppressed due to low confidence (3)

packages/devextreme/js/aspnet.js:97

  • compileViaScript warns when !src.nonce, but the nonce may be present only as an attribute (via getAttribute('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.replaceAll will throw in environments where String.prototype.replaceAll is not available, and stripping only - does not guarantee a valid function identifier for function <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 and compileViaScript returns null (e.g., templates not backed by a <script> tag), the compiler currently returns the raw template text and skips the new Function path. This can break rendering if CSP detection is a false positive (e.g., nonce is present but unsafe-eval is 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')) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants