Registry / auth-security / safe-netrc

safe-netrc

JSON →
library1.0.1pypypi✓ verified 85d ago

A drop-in replacement for Python's netrc module that safely parses .netrc files without leaking credentials. Provides the same API but with better error handling and support for permission checks. Current version: 1.0.1, released 2023-08. Monthly releases.

pip install safe-netrc
INSTALL
IMPORT
SIG · SAFE-NETRC
S
safe-netrc
auth-securitypythonv1.0.1
Install
1.7s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

netrc
from safe_netrc import netrc
import netrc
The standard library module has no permission checks and may expose passwords. Use safe-netrc's version.

Parse .netrc file and authenticate to a host. The file must have permissions 600 or less (owner-only read/write).

from safe_netrc import netrc import os host = 'example.com' login, _, password = netrc(os.path.expanduser('~/.netrc')).authenticators(host) print(f'Login: {login}')
Debug
Known issues
breakingThe `netrc` function from safe-netrc returns a different object type than stdlib `netrc.netrc`. It returns a `Netrc` object from safe_netrc, which behaves identically but is not a subclass of stdlib's. Do not use `isinstance` checks.
fix
Avoid isinstance checks; just use the object directly.
affects: all
gotchaThe file permissions are checked strictly. If your .netrc file has group or world permissions (e.g., 644), safe-netrc raises `NetrcParseError` with "permissions too open". stdlib netrc silently accepts such files.
fix
Run `chmod 600 ~/.netrc` to fix permissions. Or handle the error gracefully.
affects: all
gotchaThe `authenticators` method requires the host argument to be a string. Passing bytes or other types will raise TypeError. stdlib netrc also has this requirement, but it's a common mistake.
fix
Always pass a string hostname: `netrc_obj.authenticators('example.com')`.
affects: all
Errors
Common errors & fixes
safe_netrc.NetrcParseError: permissions too open
The .netrc file has permissions that allow other users to read it (e.g., 644). safe-netrc requires permissions to be 600 or less.
fix
Run `chmod 600 ~/.netrc` in the terminal to restrict permissions.
AttributeError: module 'netrc' has no attribute 'netrc'
You are importing the standard library module `netrc` instead of `safe_netrc`. The safe-netrc module is `safe_netrc` and you must import from it.
fix
Use `from safe_netrc import netrc` or `import safe_netrc` and access via `safe_netrc.netrc`.
Upgrade
Version history
1.0.1latest on PyPI · released Nov 29, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
safe-netrc — pip install safe-netrc · libregistry