Hardcoded Domain in HTML

Learn how to prevent hardcoded domain vulnerabilities in HTML with real code examples and best practices. Protect your web applications from security risks associated with hardcoded domains.

Tools recognizing this:

Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is Hardcoded Domain in HTML and How Does it Work?

Hardcoded domains in HTML refer to the practice of embedding absolute URLs with specific domain names directly in the HTML code. This can lead to security vulnerabilities and maintenance issues, particularly when:

  • The application needs to be deployed across different environments

  • The domain needs to change in the future

  • Content needs to be served from different CDNs

  • SSL/TLS protocols need to be enforced consistently

This guide covers hardcoded domain vulnerabilities, examples, prevention methods, and how to implement secure domain references in your HTML code.

One Simple Hardcoded Domain Example

Consider this classic example of a hardcoded domain:

<script src="http://example.com/js/script.js"></script>
<img src="https://example.com/images/logo.png">

These hardcoded URLs can cause issues when:

• Moving between development and production environments • Switching between HTTP and HTTPS • Changing content delivery networks

Hardcoded Domain Prevention Methods: How to Fix Your Code

The most efficient way to fix hardcoded domain issues is to use protocol-relative URLs or environment variables for domain configuration. This allows for flexible deployment across different environments while maintaining security.

Code Samples

Vulnerable Code


Fixed Code

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js" 
        integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" 
        crossorigin="anonymous"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 
      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" 
      crossorigin="anonymous" rel="stylesheet">

Fix Explanation

The vulnerable code uses hardcoded HTTP protocol.The fix uses protocol-relative URLs (starting with //).Added integrity hashes for security.Added crossorigin attribute for proper CORS handling.

Need more help in preventing Hardcoded Domain vulnerabilities?

Mobb supports fixing many forms of Hardcoded Domain vulnerabilities, and can mitigate your issues in batch.

Start now for free at app.mobb.ai

We'd love your feedback!

We're excited to hear your thoughts and ideas about fixing vulnerabilities.

Book a meeting or Contact us if you have any corrections, questions or suggestions. Start now for free at https://app.mobb.ai

Last updated

Was this helpful?