Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix secret not being case-insensitive #81

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions onetimepass/base32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import base64
import functools

# > Optional casefold is a flag specifying whether a lowercase alphabet is
# > acceptable as input. For security purposes, the default is False.
# > —https://docs.python.org/3/library/base64.html#base64.b32decode
#
# We prefer to avoid using something that is disabled by default for security
# purposes, but some services, unfortunately, provide the secret in lowercase
# (e.g. Google!), so not doing that would make the application not working for
# those.
#
# The reason that they provide the secret in lowercase may come from the
# official spec being ambiguous:
#
# > The Base 32 encoding is designed to represent arbitrary sequences of octets
# > in a form that needs to be **case insensitive**
# > […]
# > These characters […] are selected from US-ASCII digits and **uppercase letters**.
# > —https://datatracker.ietf.org/doc/html/rfc3548#section-5
#
# It mentions using uppercase letters, but also the value being case-insensitive.
# The reason is just a guess, though
decode = functools.partial(base64.b32decode, casefold=True)
4 changes: 2 additions & 2 deletions onetimepass/db/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

import base64
import binascii
import datetime
import typing

from pydantic import validator

from onetimepass import base32
from onetimepass import settings
from onetimepass.base_model import BaseModel
from onetimepass.db import exceptions
Expand Down Expand Up @@ -81,7 +81,7 @@ class AliasSchema(BaseModel):
@validator("secret")
def valid_base32_secret(cls, v):
try:
base64.b32decode(v)
base32.decode(v)
except binascii.Error as e:
raise ValueError(f"invalid Base32 value; {e}")
return v
Expand Down
8 changes: 4 additions & 4 deletions onetimepass/otp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
import binascii
import datetime
import functools
Expand All @@ -12,6 +11,7 @@
import pydantic
from rich.console import Console

from onetimepass import base32
from onetimepass import master_key
from onetimepass import otp_algorithm
from onetimepass import settings
Expand Down Expand Up @@ -158,7 +158,7 @@ def show(ctx: click.Context, alias: str, wait: int | None, minimum_verbose: bool
if wait is not None:
remaining_seconds = otp_algorithm.get_seconds_remaining(
otp_algorithm.TOTPParameters(
secret=base64.b32decode(alias_data.secret),
secret=base32.decode(alias_data.secret),
digits_count=alias_data.digits_count,
hash_algorithm=alias_data.hash_algorithm,
time_step_seconds=alias_data.params.time_step_seconds,
Expand All @@ -169,7 +169,7 @@ def show(ctx: click.Context, alias: str, wait: int | None, minimum_verbose: bool
time.sleep(remaining_seconds)
# Reinitialize parameters to get valid result
params = otp_algorithm.TOTPParameters(
secret=base64.b32decode(alias_data.secret),
secret=base32.decode(alias_data.secret),
digits_count=alias_data.digits_count,
hash_algorithm=alias_data.hash_algorithm,
time_step_seconds=alias_data.params.time_step_seconds,
Expand All @@ -187,7 +187,7 @@ def show(ctx: click.Context, alias: str, wait: int | None, minimum_verbose: bool
elif alias_data.otp_type == OTPType.HOTP:
alias_data.params.counter += 1
params = otp_algorithm.HOTPParameters(
secret=base64.b32decode(alias_data.secret),
secret=base32.decode(alias_data.secret),
digits_count=alias_data.digits_count,
hash_algorithm=alias_data.hash_algorithm,
counter=alias_data.params.counter,
Expand Down