Skip to content

Fix: OAuth token save crashes on Windows (os.fchmod is Unix-only)#147

Open
SENTMarketing wants to merge 1 commit into
AgriciDaniel:mainfrom
SENTMarketing:fix/windows-fchmod-oauth-token
Open

Fix: OAuth token save crashes on Windows (os.fchmod is Unix-only)#147
SENTMarketing wants to merge 1 commit into
AgriciDaniel:mainfrom
SENTMarketing:fix/windows-fchmod-oauth-token

Conversation

@SENTMarketing

Copy link
Copy Markdown

Problem

_save_oauth_token() in scripts/google_auth.py calls os.fchmod(fd, 0o600). On Windows os.fchmod does not exist, so the call raises AttributeError — which is not caught by the surrounding except OSError. The exception propagates before os.fdopen(...).write() runs, leaving an empty oauth-token.json and making --auth fail on Windows.

Observed error during google_auth.py --auth:

Error exchanging authorization code: module 'os' has no attribute 'fchmod'

(token file ends up empty; refresh/login broken on Windows.)

Fix

Guard the call with hasattr(os, "fchmod") so non-POSIX platforms simply skip it and proceed to write the token. POSIX behaviour is unchanged (fchmod still enforces 0o600), and _chmod_quiet() already set 0o600 on the file beforehand.

try:
    if hasattr(os, "fchmod"):  # fchmod is Unix-only; absent on Windows
        os.fchmod(fd, 0o600)
except OSError:
    pass

Verified: --auth now writes a valid token with refresh_token on Windows 11 (Python 3.12); unchanged on POSIX.

_save_oauth_token() calls os.fchmod, which does not exist on Windows and
raises AttributeError (not OSError), so the except OSError does not catch it.
Result: the token file is created empty and --auth fails on Windows.
Guard with hasattr(os, "fchmod") so non-POSIX platforms skip it and the
write proceeds; POSIX behaviour is unchanged.
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