aio-libs / aio-libs/yarl

`host_port_subcomponent` raises `IndexError` for an empty host

Đang mở
#1,821 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
1.5k
Fork
215
Merge trung bình
1 ngày 2 phút
Pull request đã merge (30 ngày)
13

Mô tả

### Please confirm the following

- [x] I agree to follow the [aio-libs Code of Conduct][CoC]

[CoC]: ../../.github/blob/master/CODE_OF_CONDUCT.md

- [x] I have checked the [current issues][issue search] for duplicates.

[issue search]: ../search?q=is%3Aissue&type=issues

- [x] I understand this is open source software provided for free and that I might not receive a timely response.
- [x] I am positive I am **NOT** reporting a (potential) security
vulnerability, to the best of my knowledge. *(These must be shared by
submitting [this report form][vulnerability report form] instead, if
any hesitation exists.)*

[vulnerability report form]: ../security/advisories/new

- [x] I am willing to submit a pull request with reporoducers as xfailing test cases or even entire fix. *(Assign this issue to me.)*

### Describe the bug

`URL("//user@")` constructs successfully and has an empty `raw_host`, but reading `host_port_subcomponent` raises an accidental `IndexError`.

The property is documented as returning a host-and-port string or `None`. Leaking an indexing error makes callers handle an implementation detail rather than a URL validation result.

### To Reproduce

```python
from yarl import URL

url = URL("//user@")

print("raw_host:", repr(url.raw_host))
print("host_port_subcomponent:", url.host_port_subcomponent)
```

### Expected behavior

The property should handle an empty host without raising `IndexError`.
Returning `""` would preserve the parsed host; returning `None` or rejecting
the URL with `ValueError` would also be preferable if that better matches the
intended API contract.

### Logs/tracebacks

```python-traceback
raw_host: ''
Traceback (most recent call last):
...
IndexError: string index out of range
```

### Python Version

```console
$ python --version
3.12.3
```

### multidict Version

```console
$ python -m pip show multidict
6.7.1
```

### propcache Version

```console
$ python -m pip show propcache
0.4.1
```

### yarl Version

```console
$ python -m pip show yarl
1.24.5, commit `577e75f`
```

### OS

Linux 6.6.87.2, WSL2, x86-64

### Additional context

`URL.host_port_subcomponent` checks `raw[-1] == "."` without first checking
whether `raw` is empty:

```python
if (raw := self.raw_host) is None:
return None
if raw[-1] == ".":
...
```

Changing the second condition to `if raw and raw[-1] == ".":` avoids the
accidental exception.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.