---
title: When Python Case Folding Becomes a Security Bug
description: Python case folding with str.lower() and casefold() can quietly turn a string check into an auth bypass. A concrete Unicode footgun, and the fix.
canonical: https://lukeocodes.dev/python-case-folding
type: article
locale: en_GB
published: 2026-08-27T00:00:00.000Z
modified: 2026-08-27T00:00:00.000Z
authors:
  - Luke Oliff
section: Security
tags:
  - Security
  - Engineering
  - python
  - unicode
  - idna
  - cve
  - casefold
feed: https://lukeocodes.dev/rss.xml
search: https://lukeocodes.dev/search.json?q={query}
schema:
  - "@context": https://schema.org
    "@graph":
      - "@type": WebSite
        "@id": https://lukeocodes.dev/#website
        url: https://lukeocodes.dev
        name: Luke Oliff
        alternateName: lukeocodes
        inLanguage: en
        publisher:
          "@id": https://lukeocodes.dev/authors/luke#person
        description: Personal blog and writing by Luke Oliff — developer relations, voice AI, developer experience.
        potentialAction:
          - "@type": SearchAction
            target:
              "@type": EntryPoint
              urlTemplate: https://lukeocodes.dev/blog/search?q={search_term_string}
            query-input: required name=search_term_string
      - "@type": Person
        "@id": https://lukeocodes.dev/authors/luke#person
        name: Luke Oliff
        url: https://lukeocodes.dev/authors/luke
        description: Engineer and developer relations leader. Writing about developer relations, AI, and the voice industry, from the UK.
        mainEntityOfPage: https://lukeocodes.dev/authors/luke
        givenName: Luke
        familyName: Oliff
        image: https://lukeocodes.dev/authors/luke.jpg
        jobTitle: Engineer and developer relations leader
        sameAs:
          - https://www.linkedin.com/in/lukeocodes
          - https://github.com/lukeocodes
          - https://bsky.app/profile/lukeocodes.dev
          - https://x.com/lukeocodes
          - https://hashnode.com/@lukeocodes
          - https://dev.to/lukeocodes
        knowsAbout:
          - Developer Relations
          - Developer Experience
          - Voice AI
          - Text-to-Speech
          - Answer Engine Optimisation
          - Search Engine Optimisation
          - Open Source
          - Developer Tooling
          - API Design
          - Speech-to-Text
          - Voice Agents
          - Community Building
        knowsLanguage:
          - English
        nationality: British
        alumniOf:
          - "@type": Organization
            name: The Open University
  - "@context": https://schema.org
    "@graph":
      - "@type": Person
        "@id": https://lukeocodes.dev/authors/luke#person
        name: Luke Oliff
        url: https://lukeocodes.dev/authors/luke
        description: Engineer and developer relations leader. Writing about developer relations, AI, and the voice industry, from the UK.
        mainEntityOfPage: https://lukeocodes.dev/authors/luke
        givenName: Luke
        familyName: Oliff
        image: https://lukeocodes.dev/authors/luke.jpg
        jobTitle: Engineer and developer relations leader
        sameAs:
          - https://www.linkedin.com/in/lukeocodes
          - https://github.com/lukeocodes
          - https://bsky.app/profile/lukeocodes.dev
          - https://x.com/lukeocodes
          - https://hashnode.com/@lukeocodes
          - https://dev.to/lukeocodes
        knowsAbout:
          - Developer Relations
          - Developer Experience
          - Voice AI
          - Text-to-Speech
          - Answer Engine Optimisation
          - Search Engine Optimisation
          - Open Source
          - Developer Tooling
          - API Design
          - Speech-to-Text
          - Voice Agents
          - Community Building
        knowsLanguage:
          - English
        nationality: British
        alumniOf:
          - "@type": Organization
            name: The Open University
      - "@type": BlogPosting
        "@id": https://lukeocodes.dev/python-case-folding#article
        headline: When Python Case Folding Becomes a Security Bug
        alternativeHeadline: When Python Case Folding Becomes a Security Bug
        description: Python case folding with str.lower() and casefold() can quietly turn a string check into an auth bypass. A concrete Unicode footgun, and the fix.
        datePublished: 2026-08-27T00:00:00.000Z
        dateModified: 2026-08-27T00:00:00.000Z
        url: https://lukeocodes.dev/python-case-folding
        mainEntityOfPage: https://lukeocodes.dev/python-case-folding
        inLanguage: en
        author:
          - "@id": https://lukeocodes.dev/authors/luke#person
        isPartOf:
          "@id": https://lukeocodes.dev/#website
        keywords: Security, Engineering, python, unicode, idna, cve, casefold
        articleSection: Security
        about:
          - "@type": Person
            name: Seth Larson
          - "@type": SoftwareApplication
            name: Python
          - "@type": Thing
            name: Unicode
        abstract: str.lower() and str.casefold() use whatever Unicode version your interpreter shipped, not the version a spec expects. That mismatch let one hostname fold two ways, which is how case folding turns a trust check into a bypass. CVE-2026-17084 is a real one.
        speakable:
          "@type": SpeakableSpecification
          cssSelector:
            - .post-tldr
            - h1
        image: https://lukeocodes.dev/og.png?h=When+Python+Case+Folding+Becomes+a+Security+Bug&d=Python+case+folding+with+str.lower%28%29+and+casefold%28%29+can+quietly+turn+a+string+check+into+an+auth+bypass.+A+concrete+Unicode+footgun%2C+and+the+fix.&e=Writing
        wordCount: 529
        timeRequired: PT3M
        publisher:
          "@id": https://lukeocodes.dev/authors/luke#person
      - "@type": BreadcrumbList
        itemListElement:
          - "@type": ListItem
            position: 1
            name: Home
            item: https://lukeocodes.dev
          - "@type": ListItem
            position: 2
            name: Writing
            item: https://lukeocodes.dev/blog
          - "@type": ListItem
            position: 3
            name: When Python Case Folding Becomes a Security Bug
            item: https://lukeocodes.dev/python-case-folding
      - "@type": FAQPage
        "@id": https://lukeocodes.dev/python-case-folding#faq
        mainEntity:
          - "@type": Question
            name: Why is str.lower() a security problem?
            acceptedAnswer:
              "@type": Answer
              text: Because Unicode case folding is not ASCII case folding. Characters like the Kelvin sign lower to a plain 'k', so a folded string can match a value its raw bytes never equalled. A trust check on the folded form then disagrees with the layer using the raw one.
          - "@type": Question
            name: What is CVE-2026-17084?
            acceptedAnswer:
              "@type": Answer
              text: A Python IDNA issue where stringprep used the interpreter's current Unicode version instead of the Unicode 3.2 rules IDNA 2003 requires, so the same hostname could encode two different ways. Seth Larson wrote it up in August 2026.
          - "@type": Question
            name: How do I compare strings safely?
            acceptedAnswer:
              "@type": Answer
              text: Do not use lower() or casefold() as a security boundary on untrusted Unicode. Restrict to ASCII and reject the rest, or normalise with the exact algorithm the downstream consumer uses, then compare that form.
---

[Luke Oliff.](/)

- [Blog](/blog)
- [CV](/cv)
- [Uses](/uses)
- [User manual](/user-manual)
- [Living with ADHD](/living-and-working-with-adhd)
- [RSS](/rss.xml)

[Luke Oliff · Writing](/blog)

# When Python Case Folding Becomes a Security Bug

Aug 2026 · [Security](/blog/category/security) · 3 min read · [Luke Oliff](/authors/luke)

**TL;DR:** str.lower() and str.casefold() use whatever Unicode version your interpreter shipped, not the version a spec expects. That mismatch let one hostname fold two ways, which is how case folding turns a trust check into a bypass. CVE-2026-17084 is a real one.

Python case folding looks harmless. You call `str.lower()` or `str.casefold()` to compare two strings without caring about capitalisation, and most days that is fine. But Unicode case folding is not ASCII case folding, and in August 2026 Seth Larson wrote up a real one: [CVE-2026-17084](https://sethmlarson.dev/when-str-lower-is-a-security-vulnerability), where Python's IDNA handling folded with the interpreter's current Unicode version instead of the Unicode 3.2 rules the spec demands, so one hostname could fold two different ways.

That gap is small. The consequences are not.

## The footgun in three lines

Some non-ASCII characters lower to ASCII letters. The Kelvin sign is the classic example: it renders like a capital K, but it is codepoint U+212A, and Python lowers it to a plain `k`.

```python
>>> "K"            # U+212A, the Kelvin sign
'K'
>>> "K".lower()
'k'
>>> "K".lower() == "k"
True
```

`casefold()` goes further, because it exists to fold aggressively for caseless matching. The German eszett folds to two letters:

```python
>>> "ß".casefold()
'ss'
```

So two strings that share no bytes can compare equal after folding, and one string can fold to something a different system never sees. That is the whole bug class.

## Where Python case folding turns into a bypass

Picture an allowlist check written the obvious way:

```python
ALLOWED_HOST = "keys.internal"

def is_allowed(host: str) -> bool:
    return host.lower() == ALLOWED_HOST   # looks safe
```

Send `"Keys.internal"`, a Kelvin sign followed by `eys.internal`. It lowers to `keys.internal`, so `is_allowed` returns `True`. Then the code that acts on the value, a DNS resolver, an HTTP client, a downstream service, sees the original string with the raw Kelvin sign in it. Your security layer said yes based on a folded form the network layer never handles. That disagreement between two parsers is exactly what Larson's IDNA case is: the trust decision and the resolution decision fold the string differently.

The same shape shows up in username uniqueness, email matching, and anything where you casefold to dedupe. [PEP 672](https://peps.python.org/pep-0672/) documents these Unicode traps in Python specifically, and it is worth reading once so the category stops surprising you.

## The fix

The blunt rule: do not use `lower()` or `casefold()` as a security boundary on untrusted Unicode. They are for display and for loose human-facing matching, not for deciding trust.

For hostnames, encode through IDNA with a library that enforces the correct profile, or restrict input to ASCII and reject anything else outright. Rejecting is underrated. If the value should be an ASCII hostname, a non-ASCII character is an error, not something to normalise into shape.

When you genuinely must compare Unicode, normalise with the exact algorithm the consumer will use, `unicodedata.normalize` with an explicit form plus a codepoint allowlist, and compare that. Never fold one way for the check and let something else fold another way for the action.

Python's own remediation, per Larson's writeup, was to give `stringprep` explicit exceptions so its folding behaves as Unicode 3.2 regardless of the interpreter's Unicode version. That is the spec-correct answer for IDNA. For your own code, the lesson is smaller and sharper: a `.lower()` in front of an equality check is not a safe way to compare anything an attacker controls.

[← Newer: The Case for a Keyboard-Driven Workflow](/keyboard-driven-workflow)

[All posts](/blog) →

## Search this site

`GET https://lukeocodes.dev/search.json?q=<words>` returns matching posts as JSON, each with its page URL and its markdown URL. Add `&limit=<1-50>` for more than 10. How it works: [/blog/search](https://lukeocodes.dev/blog/search.md).

```sh
curl "https://lukeocodes.dev/search.json?q=developer+relations"
```

---

© 2026 Luke Oliff. Posts live at the root of this site.

- [GitHub](https://github.com/lukeocodes)
- [LinkedIn](https://www.linkedin.com/in/lukeocodes)
- [X](https://x.com/lukeocodes)
- [Bluesky](https://bsky.app/profile/lukeocodes.dev)
- [Email](mailto:luke@lukeoliff.com)
- [RSS](/rss.xml)

## Cookies

This site sets one analytics cookie, from Google Analytics, and only in a browser that accepts the cookie banner. A client that does not run scripts sets no cookies, so there is nothing to accept or decline here.

[Cookie policy](/cookies)
