Skip to content

fix(playbooks): correct off-by-one field count check in redfish.py ho…#154

Open
andrewwhitecdw wants to merge 1 commit into
NVIDIA:masterfrom
andrewwhitecdw:sweep/bugfix-redfish-host-line-index
Open

fix(playbooks): correct off-by-one field count check in redfish.py ho…#154
andrewwhitecdw wants to merge 1 commit into
NVIDIA:masterfrom
andrewwhitecdw:sweep/bugfix-redfish-host-line-index

Conversation

@andrewwhitecdw

Copy link
Copy Markdown

…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

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.

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.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant