# Hardcoded Domain in HTML

#### Tools recognizing this:

<kbd>Opengrep</kbd> <kbd>Fortify</kbd> <kbd>Checkmarx</kbd> <kbd>SonarQube</kbd> <kbd>Snyk</kbd> <kbd>Semgrep</kbd> <kbd>CodeQL</kbd>

## 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:

{% code overflow="wrap" %}

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

{% endcode %}

These hardcoded URLs can cause issues when:

<mark style="color:red;">• Moving between development and production environments</mark> <mark style="color:red;">• Switching between HTTP and HTTPS</mark> <mark style="color:red;">• Changing content delivery networks</mark>

## 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

{% tabs %}
{% tab title="CDN Resources" %}
**Vulnerable Code**

```html


```

**Fixed Code**

```html
<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.
{% endtab %}

{% tab title="Internal Resources" %}
**Vulnerable Code**

```html
<img src="https://myapp.com/images/logo.png">
<a href="http://myapp.com/about">About</a>
```

**Fixed Code**

```html
<img src="/images/logo.png">
<a href="/about">About</a>
```

**Fix Explanation**

The vulnerable code uses absolute URLs with hardcoded domains.The fix uses relative paths from the root.Allows for flexible deployment across different domains.Automatically adapts to the current protocol (HTTP/HTTPS).
{% endtab %}

{% tab title="Environment Variables" %}
**Vulnerable Code**

```html
<meta property="og:image" content="https://production.myapp.com/social.jpg">
<script src="https://static.myapp.com/js/main.js"></script>
```

**Fixed Code**

```html
<meta property="og:image" content="${ASSET_URL}/social.jpg">
<script src="${CDN_URL}/js/main.js"></script>
```

**Fix Explanation**

The vulnerable code hardcodes production URLs.The fix uses environment variables for domain configuration.Allows for different URLs in development and production.Enables easy CDN configuration changes.
{% endtab %}

{% tab title="Dynamic URLs" %}
**Vulnerable Code**

```html
<form action="https://api.myapp.com/submit" method="POST">
    <input type="hidden" name="callback" value="https://myapp.com/callback">
</form>
```

**Fixed Code**

```html
<form action="/submit" method="POST">
    <input type="hidden" name="callback" value="<%= request.getScheme() %>://<%= request.getServerName() %>/callback">
</form>
```

**Fix Explanation**

The vulnerable code uses hardcoded domains for API endpoints.The fix uses relative paths and dynamic URL generation.Automatically adapts to the current domain and protocol.Maintains flexibility for different deployment environments.
{% endtab %}

{% tab title="Mixed Content" %}
**Vulnerable Code**

```html
<script src="http://analytics.example.com/tracker.js"></script>
<iframe src="http://widgets.example.com/widget.html"></iframe>
```

**Fixed Code**

```html
<script src="https://analytics.example.com/tracker.js"></script>
<iframe src="https://widgets.example.com/widget.html"></iframe>
```

**Fix Explanation**

The vulnerable code uses HTTP protocol, causing mixed content warnings.The fix enforces HTTPS for all external resources.Prevents security warnings in modern browsers.Ensures secure content delivery.
{% endtab %}
{% endtabs %}

## Need more help in preventing Hardcoded Domain vulnerabilities?

[Mobb](https://mobb.ai) supports fixing many forms of Hardcoded Domain vulnerabilities, and can mitigate your issues in batch.

Start now for free at [app.mobb.ai](https://app.mobb.ai)

### We'd love your feedback!

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

[Book a meeting](https://calendly.com/mobbai/demo) or [Contact us](https://content.mobb.ai/contact) if you have any corrections, questions or suggestions. Start now for free at <https://app.mobb.ai>
