# unverified-hostname

Rule `sonarjs/unverified-hostname`. Server hostnames should be verified during SSL/TLS connections

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1380 by reach, #681 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/unverified-hostname` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-5527/) |

## Code examples

### Incorrect: the rule reports this

```js
let options = {
  hostname: 'www.example.com',
  port: 443,
  path: '/',
  method: 'GET',
  secureProtocol: 'TLSv1_2_method',
  checkServerIdentity: function() {} // Noncompliant: hostname is not verified
};

let req = https.request(options, (res) => {
  res.on('data', (d) => {
    process.stdout.write(d);
  });
}); // Noncompliant
```

```js
let options = {
    secureProtocol: 'TLSv1_2_method',
    checkServerIdentity: function() {}  // Noncompliant: hostname is not verified
};

let socket = tls.connect(443, "www.example.com", options, () => {
  process.stdin.pipe(socket);
  process.stdin.resume();
});  // Noncompliant
```

```js
let socket = request.get({
    url: 'https://www.example.com',
    secureProtocol: 'TLSv1_2_method',
    checkServerIdentity: function() {}  // Noncompliant: hostname is not verified
});
```

### Correct: the rule accepts this

```js
let options = {
  hostname: 'www.example.com',
  port: 443,
  path: '/',
  method: 'GET',
  secureProtocol: 'TLSv1_2_method'
};

let req = https.request(options, (res) => {
  res.on('data', (d) => {
    process.stdout.write(d);
  });
}); // Compliant: default checkServerIdentity function is secure
```

```js
let options = {
    secureProtocol: 'TLSv1_2_method',
    checkServerIdentity: (servername, peer) => {
        if (servername !== "www.example.com") {
            return new Error ('Error');  // Compliant: there is at least one check
        }
    }
};

let socket = tls.connect(443, "www.example.com", options, () => {
  process.stdin.pipe(socket);
  process.stdin.resume();
}); // Compliant
```

```js
let socket = request.get({
    url: 'https://www.example.com/',
    secureProtocol: 'TLSv1_2_method' // Compliant
}); // Compliant:  default checkServerIdentity function is secure
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/unverified-hostname": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/unverified-certificate](https://lintradar.dev/rules/sonarjs/unverified-certificate.md): Server certificates should be verified during SSL/TLS connections
- [sonarjs/updated-const-var](https://lintradar.dev/rules/sonarjs/updated-const-var.md): "const" variables should not be reassigned
- [sonarjs/unused-named-groups](https://lintradar.dev/rules/sonarjs/unused-named-groups.md): Names of regular expressions named groups should be used
- [sonarjs/updated-loop-counter](https://lintradar.dev/rules/sonarjs/updated-loop-counter.md): Loop counters should not be assigned within the loop body
- [sonarjs/unused-import](https://lintradar.dev/rules/sonarjs/unused-import.md): Unnecessary imports should be removed
- [sonarjs/use-type-alias](https://lintradar.dev/rules/sonarjs/use-type-alias.md): Type aliases should be used

---

Web page: https://lintradar.dev/rules/sonarjs/unverified-hostname. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
