Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. CWE-829: Securing Third-Party Scripts and CDN Resources in ASP.NET Core with SRI

CWE-829: Securing Third-Party Scripts and CDN Resources in ASP.NET Core with SRI

Date- Jun 06,2026 305
cwe 829 sri

Overview

Subresource Integrity (SRI) is a security feature that allows browsers to verify that fetched resources, such as scripts or styles, are delivered without unexpected manipulation. This mechanism exists to address the risks associated with using third-party resources, which are often loaded from Content Delivery Networks (CDNs). If an attacker compromises a CDN, they could serve malicious scripts that compromise the integrity of your web application, thereby exposing sensitive data and user information.

Real-world use cases of SRI are prevalent in modern web applications where developers rely on third-party libraries hosted on CDNs for enhanced functionality and performance. For instance, popular frameworks like jQuery, Bootstrap, and others are frequently included in projects via CDN links. By employing SRI, developers can ensure that the files remain unchanged and trustworthy, thus providing an additional layer of security.

Prerequisites

  • ASP.NET Core: Familiarity with ASP.NET Core framework and its middleware.
  • CDN Understanding: Basic knowledge of how Content Delivery Networks operate.
  • JavaScript: Understanding of JavaScript and how it interacts with HTML and CSS.
  • Security Principles: Awareness of common web security practices.

Understanding Subresource Integrity (SRI)

Subresource Integrity works by allowing developers to specify a cryptographic hash for the resource they are fetching. When the browser retrieves the resource, it computes the hash of the file and compares it to the provided hash. If they match, the resource is executed; if not, the browser blocks it. This process ensures that only resources that have not been altered can be executed, protecting against malicious code injection.

Using SRI is particularly important when integrating third-party scripts because these resources are outside of your control. By implementing SRI, you significantly reduce the attack surface of your application, making it much harder for attackers to inject harmful scripts. SRI can be used with any external resource like JavaScript, CSS files, and even images.

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" integrity="sha384-ZVP8s6M5Q1H5Tj6kG1B1b4Z4xd8U7uW4dL1U6H5y2G5V1z5mU5eJ2G5Q2G5E5z5Q" crossorigin="anonymous"></script>

This code snippet demonstrates how to include jQuery from a CDN with SRI. The integrity attribute contains the hash of the file, ensuring that only the correct version is loaded. The crossorigin attribute is also included, as it is necessary when using CORS with SRI.

How to Generate SRI Hashes

Generating an SRI hash can be done using various tools or online services. The hash is typically computed using the SHA-256, SHA-384, or SHA-512 algorithms. The hash is then included in the HTML tag of the resource you are loading. For instance, using the command line, you could compute the hash as follows:

openssl dgst -sha384 -binary jquery.min.js | openssl base64 -A

This command reads the file jquery.min.js, computes its SHA-384 hash, and outputs it in base64 format, which is suitable for use in the SRI integrity attribute.

Integrating SRI in ASP.NET Core

To effectively integrate SRI in your ASP.NET Core application, you first need to ensure that your HTML views are set up to include external resources. The integration involves dynamically generating the SRI hash for your scripts and styles, which can be handled through middleware or custom helpers.

public class SRIHelper { public static string GenerateSRI(string filePath) { using var sha = SHA384.Create(); using var stream = File.OpenRead(filePath); var hash = sha.ComputeHash(stream); return "sha384-" + Convert.ToBase64String(hash); } }

This code defines a helper class SRIHelper which contains a method GenerateSRI. This method computes the SHA-384 hash of a specified file and returns it in a format suitable for SRI use. This allows for programmatic generation of SRI hashes within your application.

Dynamically Including SRI in Layouts

To ensure that all your views use SRI when loading scripts, you can modify your layout files to include SRI hashes dynamically. This approach guarantees that you always use the correct hash even when the scripts are updated.

@inject SRIHelper SRIHelper @* In your layout view *@ 

In this example, we inject the SRIHelper into the Razor view and use the GenerateSRI method to dynamically create the integrity attribute for the jQuery script. This approach keeps your application secure while simplifying the management of external resources.

Edge Cases & Gotchas

While implementing SRI, developers may encounter several common pitfalls. One significant issue arises when the external resource is updated; if the hash is not updated accordingly, the resource will fail to load. This can lead to broken functionality or a degraded user experience.

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" integrity="sha384-INVALID_HASH" crossorigin="anonymous"></script>

The above code demonstrates a wrong approach with an invalid SRI hash. When the browser attempts to load this script, it will block the execution due to hash mismatch.

Correct Approach

To avoid such issues, always monitor the libraries you are using for updates and regenerate the SRI hashes accordingly. Tools like npm or yarn can help manage dependencies effectively, and integrating SRI checks into your CI/CD pipeline can automate this process.

Performance & Best Practices

Implementing SRI does not introduce significant performance overhead, as the hashing process is relatively lightweight compared to the benefits it provides. However, developers should be mindful of the following best practices:

  • Use a CDN: Using a reliable CDN can improve load times while SRI ensures integrity.
  • Monitor Dependencies: Regularly check for updates to third-party libraries and regenerate SRI hashes as needed.
  • Fallbacks: Consider implementing fallback scripts in case the CDN resource fails to load.
  • Testing: Always test your application thoroughly to ensure that SRI is functioning as expected and not blocking legitimate resources.

Real-World Scenario: Building a Secure ASP.NET Core Web Application

To demonstrate the practical application of SRI, let's consider a mini-project where we build a simple ASP.NET Core web application that utilizes Bootstrap and jQuery from a CDN with SRI. The goal is to create a secure and responsive UI.

public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } } }

This code sets up the ASP.NET Core application with basic routing and exception handling. Next, we will create a simple view that includes Bootstrap and jQuery with SRI.

@* In your Views/Home/Index.cshtml *@ <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Secure ASP.NET Core App</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-DyZv9A1Xj1d8eB0Jj3Q4d3iY2G4Q4d3iY2G4Q4d3iY2G4Q4d3iY2G4Q4d3iY2G4" crossorigin="anonymous" /> </head> <body> <h1>Welcome to Secure ASP.NET Core App</h1> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" integrity="sha384-ZVP8s6M5Q1H5Tj6kG1B1b4Z4xd8U7uW4dL1U6H5y2G5V1z5mU5eJ2G5Q2G5E5z5Q" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-pzjw8f+ua7Kw1TIq0C6P1FZ9Xx3W9j5mM3J0wA8Xx3W9j5mM3J0wA8Xx3W9j5M" crossorigin="anonymous"></script> </body> </html>

This Razor view includes Bootstrap for styling and jQuery for interactivity, both secured with SRI. The application is now ready to handle user interactions while ensuring that external scripts are verified for integrity.

Conclusion

  • Subresource Integrity is a crucial security feature for web applications using third-party resources.
  • Implementing SRI in ASP.NET Core enhances the security of your application against supply chain attacks.
  • Generating SRI hashes can be automated to streamline the development process.
  • Always monitor your dependencies and keep SRI hashes updated.
  • Testing and fallback mechanisms are essential for a robust implementation.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

CWE-89: Preventing SQL Injection in ASP.NET Core with Dapper and Entity Framework
May 28, 2026
CWE-614: Configuring Secure Cookie Attributes in ASP.NET Core for Enhanced Security
Apr 28, 2026
CWE-384: Preventing Session Fixation in ASP.NET Core with Secure Session Configuration
Apr 28, 2026
CWE-276: Fixing Insecure Default Configurations in ASP.NET Core Middleware Pipeline
Jun 09, 2026
Previous in ASP.NET Core
CWE-1021: Preventing Clickjacking in ASP.NET Core with X-Frame-Op…
Next in ASP.NET Core
CWE-532: Secure Logging in ASP.NET Core - Avoiding Sensitive Data…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 361 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,217 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,937 views
  • 4
    Error-An error occurred while processing your request in .… 11,965 views
  • 5
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 244 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 831 views
  • 7
    Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamle… 614 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26684 views
  • Exception Handling Asp.Net Core 21721 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21179 views
  • How to implement Paypal in Asp.Net Core 20129 views
  • Task Scheduler in Asp.Net core 18204 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor