Skip to content

fix: prevent crash on unclosed quotes in os-release files - #388

Open
pro-laxmi wants to merge 1 commit into
python-distro:masterfrom
pro-laxmi:fix/os-release-shlex-error
Open

pro-laxmi wants to merge 1 commit into
python-distro:masterfrom
pro-laxmi:fix/os-release-shlex-error

Conversation

@pro-laxmi

Copy link
Copy Markdown

Summary of Changes

When /etc/os-release is truncated, corrupted, or contains unclosed quotation marks (e.g. NAME="Fedora Linux), shlex.shlex raises ValueError("No closing quotation"). Currently, this unhandled exception crashes the entire program and any application or library importing distro.

Root Cause

_parse_os_release_content called list(lexer) without catching ValueError.

Fix

Wrapped shlex lexer tokenization in a try...except ValueError block with an automatic fallback to robust line-by-line parsing (key, val = line.split("=", 1)), stripping surrounding quotes.

Verification & Testing

Tested with malformed os-release streams and unclosed quotes:

bad_content = io.StringIO('NAME="Fedora Linux\nID=fedora\nVERSION_ID=44\n')
info = distro.LinuxDistribution._parse_os_release_content(bad_content)
assert info['id'] == 'fedora'
assert info['name'] == 'Fedora Linux'

Parsed successfully without crashing.

@HorlogeSkynet HorlogeSkynet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @pro-laxmi, thanks for you PR.

Could you address the following points :

  • code comments in the for loop shouldn't be removed
  • io.StringIO wrapping of lines could be replaced by a lines.seek(os.SEEK_SET) if input file ends up being manually parsed the hard way (shouldn't occur much, and these files don't usually contain much data)
  • only shlex.shlex call should be wrapped in the try..except ValueError block

Thanks, bye 👋

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants