| 1 |
# Internationalized Domain Names in Applications (IDNA) |
| 2 |
|
| 3 |
Support for [Internationalized Domain Names in Applications |
| 4 |
(IDNA)](https://tools.ietf.org/html/rfc5891) and [Unicode IDNA |
| 5 |
Compatibility Processing](https://unicode.org/reports/tr46/). It |
| 6 |
supersedes the standard library's `encodings.idna`, which only |
| 7 |
implements the 2003 specification, offering broader script coverage and |
| 8 |
limiting domains with known security vulnerabilities. |
| 9 |
|
| 10 |
## Usage |
| 11 |
|
| 12 |
Package may be installed from [PyPI](https://pypi.org/project/idna/) via |
| 13 |
the typical methods (e.g. `python3 -m pip install idna`) |
| 14 |
|
| 15 |
For typical usage, the `encode` and `decode` functions will take a |
| 16 |
domain name argument and perform a conversion to ASCII-compatible encoding |
| 17 |
(known as A-labels), or to Unicode strings (known as U-labels) |
| 18 |
respectively. |
| 19 |
|
| 20 |
```pycon |
| 21 |
>>> import idna |
| 22 |
>>> idna.encode('ドメイン.テスト') |
| 23 |
b'xn--eckwd4c7c.xn--zckzah' |
| 24 |
>>> print(idna.decode('xn--eckwd4c7c.xn--zckzah')) |
| 25 |
ドメイン.テスト |
| 26 |
``` |
| 27 |
|
| 28 |
Conversions can be applied at a per-label basis using the `ulabel` or |
| 29 |
`alabel` functions for specialized use cases. |
| 30 |
|
| 31 |
|
| 32 |
### Compatibility Mapping (UTS #46) |
| 33 |
|
| 34 |
This library provides support for [Unicode IDNA Compatibility |
| 35 |
Processing](https://unicode.org/reports/tr46/) which normalizes input from |
| 36 |
different potential ways a user may input a domain prior to performing the IDNA |
| 37 |
conversion operations. This functionality, known as a |
| 38 |
[mapping](https://tools.ietf.org/html/rfc5895), is considered by the |
| 39 |
specification to be a local user-interface issue distinct from IDNA |
| 40 |
conversion functionality. |
| 41 |
|
| 42 |
For example, "Königsgäßchen" is not a permissible label as capital letters |
| 43 |
are not allowed. UTS #46 will convert this into lower case prior to applying |
| 44 |
the IDNA conversion. |
| 45 |
|
| 46 |
```pycon |
| 47 |
>>> import idna |
| 48 |
>>> idna.encode('Königsgäßchen') |
| 49 |
... |
| 50 |
idna.core.InvalidCodepoint: Codepoint U+004B at position 1 of 'Königsgäßchen' not allowed |
| 51 |
>>> idna.encode('Königsgäßchen', uts46=True) |
| 52 |
b'xn--knigsgchen-b4a3dun' |
| 53 |
>>> idna.decode('xn--knigsgchen-b4a3dun') |
| 54 |
'königsgäßchen' |
| 55 |
``` |
| 56 |
|
| 57 |
When performing a decode operation for display purposes, `decode()` |
| 58 |
accepts a `display=True` argument that leaves any `xn--` label that |
| 59 |
fails to decode unchanged. This is useful for user interface display |
| 60 |
where a domain is in use, the A-label form can be presented when it |
| 61 |
is not a valid IDN. |
| 62 |
|
| 63 |
|
| 64 |
## Exceptions |
| 65 |
|
| 66 |
All errors raised during conversion derive from the `idna.IDNAError` |
| 67 |
base class. The more specific exceptions are: |
| 68 |
|
| 69 |
* `idna.IDNABidiError` — raised when a label contains an illegal |
| 70 |
combination of left-to-right and right-to-left characters. |
| 71 |
* `idna.InvalidCodepoint` — raised when a label contains a codepoint |
| 72 |
that is INVALID for IDNA. |
| 73 |
* `idna.InvalidCodepointContext` — raised when a CONTEXTO or CONTEXTJ |
| 74 |
codepoint appears in a position whose contextual requirements are |
| 75 |
not satisfied. |
| 76 |
|
| 77 |
Exceptions carry machine-readable attributes so that applications |
| 78 |
do not need to parse the message: `code` is a short, stable identifier |
| 79 |
for the rule that failed (listed below); and, when the failure can be |
| 80 |
attributed to a particular character, `text` (the label, or domain for |
| 81 |
UTS #46 processing, being validated), `codepoint` (the offending |
| 82 |
codepoint as an integer) and `position` (its 1-based index within |
| 83 |
`text`, as quoted in the message) are set. Each is `None` when it does |
| 84 |
not apply. Message wording is not part of the API and may change. |
| 85 |
|
| 86 |
```pycon |
| 87 |
>>> try: |
| 88 |
... idna.encode('Königsgäßchen') |
| 89 |
... except idna.IDNAError as err: |
| 90 |
... print(err.code, err.codepoint, err.position, err.text) |
| 91 |
disallowed_codepoint 75 1 Königsgäßchen |
| 92 |
``` |
| 93 |
|
| 94 |
| `code` | Meaning | |
| 95 |
|---|---| |
| 96 |
| `input_too_long` | Input exceeds the library's defensive length limit and was not processed | |
| 97 |
| `label_too_long` | A label exceeds 63 octets | |
| 98 |
| `domain_too_long` | The domain exceeds 253 octets | |
| 99 |
| `empty_label` | A label is empty (e.g. consecutive dots) | |
| 100 |
| `empty_domain` | The domain is empty | |
| 101 |
| `not_nfc` | The label is not in Unicode Normalization Form C | |
| 102 |
| `hyphen_3_4` | The label has hyphens in the 3rd and 4th positions | |
| 103 |
| `hyphen_start_end` | The label starts or ends with a hyphen | |
| 104 |
| `leading_combiner` | The label starts with a combining mark | |
| 105 |
| `disallowed_codepoint` | A codepoint is DISALLOWED or UNASSIGNED under IDNA 2008 | |
| 106 |
| `contextj` | A CONTEXTJ codepoint (joiner) appears in an invalid context | |
| 107 |
| `contexto` | A CONTEXTO codepoint appears in an invalid context | |
| 108 |
| `unknown_codepoint` | A codepoint next to a joiner is unknown to this Python's Unicode database | |
| 109 |
| `bidi_rule_1` … `bidi_rule_6` | The corresponding rule of RFC 5893 (the Bidi Rule) is violated | |
| 110 |
| `bidi_unknown_direction` | A codepoint's directionality is unknown to this Python's Unicode database | |
| 111 |
| `invalid_alabel` | An `xn--` label is malformed or is not valid Punycode | |
| 112 |
| `non_canonical_alabel` | An `xn--` label is not the canonical Punycode encoding of its U-label (a "fake A-label") | |
| 113 |
| `invalid_ascii` | Byte input is not ASCII | |
| 114 |
| `invalid_utf8` | Byte input is not UTF-8 | |
| 115 |
| `uts46_disallowed` | A codepoint is disallowed by the UTS #46 mapping table | |
| 116 |
| `uts46_std3` | An ASCII character is rejected by the UTS #46 STD3 rules | |
| 117 |
| `unsupported_errors` | The codec was given an `errors` handler other than `strict` | |
| 118 |
|
| 119 |
|
| 120 |
## Command-line tool |
| 121 |
|
| 122 |
The package supports command-line usage to convert domain names |
| 123 |
between their Unicode and ASCII-compatible forms. It can be run either |
| 124 |
as a module (`python3 -m idna`) or, once installed (such as with `uv |
| 125 |
tool` or `pipx`), via the `idna` script: |
| 126 |
|
| 127 |
```bash |
| 128 |
$ uv tool install idna |
| 129 |
$ idna xn--e1afmkfd.xn--p1ai |
| 130 |
пример.рф |
| 131 |
$ idna пример.рф |
| 132 |
xn--e1afmkfd.xn--p1ai |
| 133 |
``` |
| 134 |
|
| 135 |
Mode can be specified with `-e`/`--encode` or `-d`/`--decode`, otherwise |
| 136 |
it will be chosen automatically based on the first input. Multiple |
| 137 |
domains can be supplied either as arguments or through standard input. |
| 138 |
UTS #46 mapping is applied by default, which lets the tool accept |
| 139 |
inputs that aren't strictly valid IDNA 2008 by normalising them first, |
| 140 |
pass `--strict` to disable UTS #46. |
| 141 |
|
| 142 |
Conversion failures are reported on stderr together with the |
| 143 |
offending input; processing continues with the remaining domains and |
| 144 |
the tool exits with a non-zero status if any conversion failed. |
| 145 |
|
| 146 |
|
| 147 |
## Additional Notes |
| 148 |
|
| 149 |
* **Python version support**. This library supports Python 3.9 and higher. |
| 150 |
As this library serves as a low-level toolkit for a variety of |
| 151 |
applications, we strive to support all versions of Python that are |
| 152 |
not beyond end-of-life. Free-threaded Python is also supported, |
| 153 |
as the library holds no mutable global state the functions can be |
| 154 |
called concurrently from multiple threads. |
| 155 |
|
| 156 |
* **Unicode version**. The IDNA and UTS #46 lookup tables are generated |
| 157 |
from a specific Unicode release. Some Unicode data depends on the |
| 158 |
running Python's `unicodedata` module, so on an older Python a |
| 159 |
character new to Unicode may be rejected as unknown even if this |
| 160 |
library knows about it. |
| 161 |
|
| 162 |
* **Emoji**. It is an occasional request to support emoji domains in |
| 163 |
this library. Encoding of symbols like emoji is expressly prohibited by |
| 164 |
the IDNA technical standard, and emoji domains are broadly phased |
| 165 |
out across the domain industry due to associated security risks. |
| 166 |
|
| 167 |
* **Regenerating lookup tables**. The IDNA and UTS #46 functionality |
| 168 |
relies upon pre-calculated lookup tables, generated using the |
| 169 |
`idna-data` script in [`tools/`](tools/README.md). |