fix(playbooks): correct off-by-one field count check in redfish.py ho…#154
Open
andrewwhitecdw wants to merge 1 commit into
Open
fix(playbooks): correct off-by-one field count check in redfish.py ho…#154andrewwhitecdw wants to merge 1 commit into
andrewwhitecdw wants to merge 1 commit into
Conversation
…sts parsing
## Summary
`playbooks/files/redfish.py` reads an Ansible-style hosts file and
queries each host's BMC over the RedFish API to verify BIOS settings for
confidential computing. If a host line in the hosts file is missing one
or two trailing fields (e.g. no `bmc_password=`), the script crashes
with an unhandled `IndexError` instead of printing the intended "Please
update BMC IP, Username and Password details in hosts file" hint.
## Root cause
```python
bmc = item.strip().split(' ')
if len(bmc) > 6:
host = bmc[6]
user = bmc[7]
pas = bmc[8]
```
The guard `len(bmc) > 6` only guarantees index 6 exists, but the body
also indexes `bmc[7]` and `bmc[8]`. A well-formed line has 9 fields
(indices 0-8; the quoted `ansible_ssh_common_args='-o ...'` value splits
into two tokens on the space). Lines with 7 or 8 fields pass the check
and then crash:
| fields in line | len(bmc) | old check passes? | result |
|---|---|---|---|
| 9 (well-formed) | 9 | yes | OK |
| 8 (missing password) | 8 | yes | IndexError at bmc[8] |
| 7 (missing user+password) | 7 | yes | IndexError at bmc[7] |
| <=6 | <=6 | no | prints usage hint |
Reproducer hosts file:
```
node1 ansible_ssh_user=nvidia ansible_ssh_pass=nvidia ansible_sudo_pass=nvidia bmc_ip=192.0.2.2 bmc_username=root
```
```
Traceback (most recent call last):
File "redfish.py", line 19, in <module>
user = bmc[7]
IndexError: list index out of range
```
## Fix
One-character-class change: require all three indices to exist.
```python
if len(bmc) > 8:
```
## Testing
No automated tests exist in this repo for the playbooks or helper
scripts (no CI configuration present), so the script was exercised
manually with `uv run --with requests python3 redfish.py hosts` against
a fixture hosts file containing:
1. a well-formed 9-field line (unreachable TEST-NET BMC IP),
2. a 7-field line (missing `bmc_password=` and one more field),
3. an 8-field line (missing `bmc_password=`).
- Unmodified script (from `master`): crashes with
`IndexError: list index out of range` on line 19 for case 2 — bug
reproduced.
- Fixed script: case 1 still attempts the RedFish connection (prints
"Server is not for RedFish API" for the unreachable IP, as before),
and cases 2 and 3 now print the intended "Please update BMC IP,
Username and Password details in hosts file" hint; exit code 0.
- `python3 -m py_compile playbooks/files/redfish.py` passes.
## Why existing tests missed it
The repository has no test suite or CI for this helper script; it is run
manually against a site-specific hosts file, which in practice always
contained well-formed lines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…sts parsing
Summary
playbooks/files/redfish.pyreads an Ansible-style hosts file and queries each host's BMC over the RedFish API to verify BIOS settings for confidential computing. If a host line in the hosts file is missing one or two trailing fields (e.g. nobmc_password=), the script crashes with an unhandledIndexErrorinstead of printing the intended "Please update BMC IP, Username and Password details in hosts file" hint.Root cause
The guard
len(bmc) > 6only guarantees index 6 exists, but the body also indexesbmc[7]andbmc[8]. A well-formed line has 9 fields (indices 0-8; the quotedansible_ssh_common_args='-o ...'value splits into two tokens on the space). Lines with 7 or 8 fields pass the check and then crash:| fields in line | len(bmc) | old check passes? | result | |---|---|---|---|
| 9 (well-formed) | 9 | yes | OK |
| 8 (missing password) | 8 | yes | IndexError at bmc[8] | | 7 (missing user+password) | 7 | yes | IndexError at bmc[7] | | <=6 | <=6 | no | prints usage hint |
Reproducer hosts file:
Fix
One-character-class change: require all three indices to exist.
Testing
No automated tests exist in this repo for the playbooks or helper scripts (no CI configuration present), so the script was exercised manually with
uv run --with requests python3 redfish.py hostsagainst a fixture hosts file containing:bmc_password=and one more field),bmc_password=).master): crashes withIndexError: list index out of rangeon line 19 for case 2 — bug reproduced.python3 -m py_compile playbooks/files/redfish.pypasses.Why existing tests missed it
The repository has no test suite or CI for this helper script; it is run manually against a site-specific hosts file, which in practice always contained well-formed lines.