SubjectAltNames Validator¶
Inspects the certificate's Subject Alternative Names (SANs): the authoritative list of hostnames and IPs a certificate is valid for. Use it to check that extra names you expect (apex + www, alternate domains) are present, and to surface the full SAN inventory.
Opt-in
Enable explicitly via enabled_validators=["subject_alt_names", ...] or the ENABLED_VALIDATORS environment variable.
Try it¶
from certmonitor import CertMonitor
with CertMonitor("example.com", enabled_validators=["subject_alt_names"]) as monitor:
monitor.get_cert_info()
result = monitor.validate(
validator_args={"subject_alt_names": {"alternate_names": ["www.example.com"]}}
)
print(result["subject_alt_names"])
These examples show selected fields from illustrative scans. validate() also adds status and code, described in the result contract.
{
"is_valid": true,
"sans": {"DNS": ["example.com", "*.example.com"], "IP Address": []},
"count": 2,
"contains_host": {
"name": "example.com",
"is_valid": true,
"reason": "Exact match for example.com found in DNS SANs"
},
"contains_alternate": {
"www.example.com": {
"name": "www.example.com",
"is_valid": true,
"reason": "www.example.com matches wildcard SAN(s): *.example.com"
}
},
"warnings": []
}
Arguments¶
Pass via validator_args={"subject_alt_names": {...}}:
| Argument | Type | Default | Description |
|---|---|---|---|
alternate_names |
list[str] \| None |
None |
Extra hostnames/IPs to confirm are covered by the SANs. Each gets its own entry under contains_alternate. |
Reading the result¶
| Field | Meaning |
|---|---|
sans |
The SAN inventory, including DNS, IP Address, and other types such as email/URI when present. None if the extension is absent. |
count |
Total number of SANs. |
contains_alternate |
One entry per name in alternate_names, each with its own match result. |
Top-level is_valid vs. per-name results
The top-level is_valid requires every requested alternate name to match. Check each contains_alternate[...]["is_valid"] for per-name outcomes. Unmatched names are also surfaced in warnings.
Pass the names you require in alternate_names; every one of them must match for is_valid to be true, and the primary host is still reported in contains_host for context. With no requested alternates (None or []) the validator falls back to checking the primary host, so enabling it by name alone behaves as it did in 0.4.0. Identity for the host you connected to is still best read from Hostname.
Reference¶
certmonitor.validators.subject_alt_names.SubjectAltNamesValidator ¶
Bases: BaseCertValidator
A validator for checking the Subject Alternative Names (SANs) in an SSL certificate.
This validator checks explicitly requested alternate names against both DNS and IP Address SANs. Without alternate names it checks the primary host instead, so enabling it by name alone still produces a meaningful verdict.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the validator. |
validate ¶
validate(cert: dict[str, Any], host: str, port: int, *, alternate_names: list[str] | None = None) -> SubjectAltNamesResult
Validates the SANs in the provided SSL certificate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cert
|
dict
|
The SSL certificate. |
required |
host
|
str
|
The primary host. Always reported in |
required |
port
|
int
|
The port number. |
required |
alternate_names
|
list
|
Alternate names to validate against the SANs.
A list or tuple of non-empty strings. When given, every requested name must
match for |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
SubjectAltNamesResult
|
A dictionary containing the validation results, including whether the SANs are valid, the SANs themselves, the count of SANs, and any warnings or reasons for validation failure. |
Examples:
Example output (success): This example shows a certificate where an alternate name is present in the DNS SANs, so validation passes.
```json
{
"is_valid": true,
"sans": {
"DNS": [
"example.com",
"www.example.com"
],
"IP Address": []
},
"count": 2,
"contains_host": {
"name": "example.com",
"is_valid": true,
"reason": "Exact match for example.com found in DNS SANs"
},
"contains_alternate": {
"www.example.com": {
"name": "www.example.com",
"is_valid": true,
"reason": "Exact match for www.example.com found in DNS SANs"
}
},
"warnings": []
}
```
Example output (failure): This example shows a certificate where the alternate name is absent from the DNS SANs, so validation fails and a warning is included.
```json
{
"is_valid": false,
"reason": "One or more required alternate names are absent from the SANs.",
"sans": {
"DNS": [
"demo.nautobot.com"
],
"IP Address": []
},
"count": 1,
"contains_host": {
"name": "demo.nautobot.com",
"is_valid": true,
"reason": "Exact match for demo.nautobot.com found in DNS SANs"
},
"contains_alternate": {
"example.com": {
"name": "example.com",
"is_valid": false,
"reason": "No match found for example.com in DNS SANs: ['demo.nautobot.com']"
}
},
"warnings": [
"Alternate name example.com: No match found for example.com in DNS SANs: ['demo.nautobot.com']"
]
}
```
Source code in certmonitor/validators/subject_alt_names.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |