Skip to content

encode() raises IndexError when exclude contains a non-ASCII character #26

Open

Description

@visionik

Hi! I hit this while comparing mdurl with the JavaScript mdurl. get_encode_cache builds a 128-entry table, then does cache[ord(exclude[i])] = exclude[i] for every character of exclude, so any character above U+007F is out of range:

import mdurl
print(repr(mdurl.encode("café", exclude="é")))

On mdurl 0.1.2 (and main at 524d2ed), Python 3.13:

  File ".../mdurl/_encode.py", line 39, in get_encode_cache
    cache[ord(exclude[i])] = exclude[i]
    ~~~~~^^^^^^^^^^^^^^^^^
IndexError: list assignment index out of range

In JavaScript the same assignment just grows the array, and encode only looks characters below 128 up in the cache, so non-ASCII characters in exclude are ignored and still encoded:

$ node -e 'const m = require("mdurl"); console.log(JSON.stringify(m.encode("café!", "é!")))'
"caf%C3%A9!"

I expected the same here. The fix is to skip characters of 128 and above when filling the cache. I have a PR with a test.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions