Skip to content

Pickletools Default Encoding #126997

Closed
Closed

Description

@Legoclones

Bug report

Bug description:

When unpickling using _pickle.c or pickle.py through load/loads, an encoding can be specified using the encoding argument, with the default being ASCII. However, pickletools does not support custom encodings and instead makes assumptions about what encoding it uses, which can lead to either incorrect data being displayed or erroring/not erroring when the normal unpickling process would error.

The three opcodes that I have found this in are STRING, BINSTRING, and SHORT_BINSTRING:

  • On line 359, it assumes ASCII for STRING
  • On line 456, it assumes latin-1 for BINSTRING
  • On line 422, it assumes latin-1 for SHORT_BINSTRING

I think the best solution would be to support encodings as an optional argument in pickletools.py, with the default being set to ASCII (since that's the default encoding for pickle.py and _pickle.c).

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Activity

  1. kanishka-coder0809 commented on Nov 19, 2024

    @kanishka-coder0809

    hey @Legoclones I think I can work with this issue ...can you please assign me with this

  2. Legoclones commented on Nov 19, 2024

    @Legoclones
    ContributorAuthor

    I don't think I have permission to assign someone

  3. kanishka-coder0809 commented on Nov 20, 2024

    @kanishka-coder0809

    So should I start working upon this and thank make a PR.?

  4. added a commit that references this issue on Nov 20, 2024
  5. serhiy-storchaka commented on Nov 20, 2024

    @serhiy-storchaka
    Member

    It is not a problem if pickletools accepts an input which is not valid for pickle. But there are real bugs here:

    • pickletools does not support STRING with non-ASCII argument. pickle allows to specify encoding (ASCII by default).
    • pickletools does not support GLOBAL with non-ASCII argument. pickle uses UTF-8.
    • pickletools.dis() outputs non-ASCII arguments for BINSTRING and SHORT_BINSTRING as Latin1-encoded. pickle allows to specify encoding (ASCII by default). It would be better to output non-ASCII bytes as \xXX.

    #127062 fixes them.

  6. serhiy-storchaka commented on Nov 20, 2024

    @serhiy-storchaka
    Member

    @kanishka-coder0809, sorry, I already worked on this issue. It is slightly larger than initially was reported. On other hand, I think that I found a better solution than supporting optional argument for encoding. The latter would require more significant rewriting of the pickletools module and likely could be too complex for backporting.

  7. Legoclones commented on Nov 20, 2024

    @Legoclones
    ContributorAuthor

    That looks great!

  8. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 21, 2024
  9. added a commit that references this issue on Nov 21, 2024
  10. added 2 commits that reference this issue on Nov 21, 2024
  11. serhiy-storchaka commented on Nov 21, 2024

    @serhiy-storchaka
    Member

    Thank you for your report @Legoclones.

  12. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Nov 21, 2024
  13. added 2 commits that reference this issue on Nov 21, 2024
  14. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard 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