Skip to content

Base64 decode with altchars still accepts non-altchars #125346

Description

@Anonymous3-a

Bug report

Bug description:

If you run

>>> import base64
>>> base64.b64decode("/+/+")

it gives

b'\xff\xef\xfe'

This is expected.
If you run

>>> base64.b64decode("_-_-", altchars="-_")

It gives the same output.
This is expected.
However, if you run

>>> base64.b64decode("/+/+", altchars="-_")

It also gives the same output.
This is not expected.

This is concerning because I'm trying to use file safe base64 (urlsafe_b64decode/encode do not have validate arg, which I need). I can work around this by also checking for / and + as well.

CPython versions tested on:

3.9, 3.11

Operating systems tested on:

Linux, Other

Linked PRs

Activity

  1. imfeh2 commented on Oct 12, 2024

    @imfeh2
    Contributor

    It's intentional behavior that was decided upon years ago. found the following as i'm trying to fix, which related to a commit in 2016.

    # Normal alphabet characters not discarded when alternative given
    res = b'\xFB\xEF\xBE\xFF\xFF\xFF'
    self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res)
    self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res)

    But I think the original alphabet should be discarded because an alphabet should always remain singular. if alternative characters are provided, the original ones should be deprecated to avoid confusion and maintain consistency.

  2. picnixz commented on Oct 12, 2024

    @picnixz
    Member

    Should we document this behaviour if it is not already the case? cc @serhiy-storchaka

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    pendingThe issue will be closed if no feedback is provided
    on Oct 12, 2024
  4. added
    docsDocumentation in the Doc dir
    and removed
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    pendingThe issue will be closed if no feedback is provided
    on Dec 2, 2024
  5. picnixz commented on Dec 2, 2024

    @picnixz
    Member

    cc @vadmium as the one who wrote this test in 2016

  6. serhiy-storchaka commented on Dec 2, 2024

    @serhiy-storchaka
    Member

    We should look at the discussion when that feature was added, but I think it is not intentional behavior.

  7. Anonymous3-a commented on Dec 3, 2024

    @Anonymous3-a
    Author

    I think it is not intentional behavior

    See #125346 (comment)

  8. 17 remaining items

  9. nikhilgv commented on Jan 28, 2026

    @nikhilgv

    Hello @serhiy-storchaka , will this fix be backported to v3.12 and 3.10?

  10. picnixz commented on Jan 28, 2026

    @picnixz
    Member

    Unless this is considered a security issue, it won't.

    cc @sethmlarson

  11. serhiy-storchaka commented on Jan 31, 2026

    @serhiy-storchaka
    Member

    No, it will not be backported, because there is a risk of breaking existing "working" code, when the code uses altchars, but the input actually uses the standard alphabet. This is a programming error, but the behavior should not be changed so drastically in a bugfix release, even a warning is too much.

    User can check that neither "+" nor "/" occurr in the input before decoding. They need to do this in 3.15 to avoid warnings (or they can ignore warnings).

    In 3.15+ you can also pass the ignorechars argument (even ignorechars=b'' works) to enable more strict and explicit behavior. If "+" and "/" in ignorechars, they will be silently ignored, otherwise they will be errors.

  12. Anonymous3-a commented on Jan 31, 2026

    @Anonymous3-a
    Author

    Should the issue be closed now, then?

  13. added
    type-bugAn unexpected behavior, bug, or error
    3.15bugs and security fixes
    on Feb 1, 2026
  14. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Feb 1, 2026
  15. Kanishk-Bansal commented on Feb 2, 2026

    @Kanishk-Bansal

    Can we expect a backport for 3.10 & 3.12 as well?

  16. picnixz commented on Feb 2, 2026

    @picnixz
    Member

    No, and the rationale is just a few comments above.

  17. added a commit that references this issue on Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15bugs and security fixesdocsDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions