2021-12-01 12:57:41 -05:00
|
|
|
PEP: 675
|
2022-02-15 15:40:49 -05:00
|
|
|
Title: Arbitrary Literal String Type
|
2021-12-01 12:57:41 -05:00
|
|
|
Author: Pradeep Kumar Srinivasan <gohanpra@gmail.com>, Graham Bleaney <gbleaney@gmail.com>
|
|
|
|
Sponsor: Jelle Zijlstra <jelle.zijlstra@gmail.com>
|
2022-02-21 18:49:13 -05:00
|
|
|
Discussions-To: https://mail.python.org/archives/list/typing-sig@python.org/thread/VB74EHNM4RODDFM64NEEEBJQVAUAWIAW/
|
2024-02-15 11:22:13 -05:00
|
|
|
Status: Final
|
2021-12-01 12:57:41 -05:00
|
|
|
Type: Standards Track
|
2022-10-06 20:36:39 -04:00
|
|
|
Topic: Typing
|
2021-12-01 12:57:41 -05:00
|
|
|
Created: 30-Nov-2021
|
|
|
|
Python-Version: 3.11
|
2022-02-15 15:40:49 -05:00
|
|
|
Post-History: 07-Feb-2022
|
2022-03-21 17:36:53 -04:00
|
|
|
Resolution: https://mail.python.org/archives/list/python-dev@python.org/message/XEOOSSPNYPGZ5NXOJFPLXG2BTN7EVRT5/
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2024-06-11 18:12:09 -04:00
|
|
|
.. canonical-typing-spec:: :ref:`typing:literalstring` and
|
|
|
|
:py:data:`typing.LiteralString`
|
2024-02-15 11:22:13 -05:00
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2022-02-15 15:40:49 -05:00
|
|
|
There is currently no way to specify, using type annotations, that a
|
|
|
|
function parameter can be of any literal string type. We have to
|
|
|
|
specify a precise literal string type, such as
|
2022-02-14 20:14:45 -05:00
|
|
|
``Literal["foo"]``. This PEP introduces a supertype of literal string
|
|
|
|
types: ``LiteralString``. This allows a function to accept arbitrary
|
|
|
|
literal string types, such as ``Literal["foo"]`` or
|
2021-12-01 12:57:41 -05:00
|
|
|
``Literal["bar"]``.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
2022-01-26 19:57:26 -05:00
|
|
|
Powerful APIs that execute SQL or shell commands often recommend that
|
|
|
|
they be invoked with literal strings, rather than arbitrary user
|
|
|
|
controlled strings. There is no way to express this recommendation in
|
|
|
|
the type system, however, meaning security vulnerabilities sometimes
|
|
|
|
occur when developers fail to follow it. For example, a naive way to
|
|
|
|
look up a user record from a database is to accept a user id and
|
|
|
|
insert it into a predefined SQL query:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def query_user(conn: Connection, user_id: str) -> User:
|
|
|
|
query = f"SELECT * FROM data WHERE user_id = {user_id}"
|
|
|
|
conn.execute(query)
|
2022-10-30 09:29:39 -04:00
|
|
|
... # Transform data to a User object and return it
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
query_user(conn, "user123") # OK.
|
|
|
|
|
|
|
|
However, the user-controlled data ``user_id`` is being mixed with the
|
|
|
|
SQL command string, which means a malicious user could run arbitrary
|
|
|
|
SQL commands:
|
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: bad
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
# Delete the table.
|
|
|
|
query_user(conn, "user123; DROP TABLE data;")
|
|
|
|
|
|
|
|
# Fetch all users (since 1 = 1 is always true).
|
|
|
|
query_user(conn, "user123 OR 1 = 1")
|
|
|
|
|
|
|
|
|
|
|
|
To prevent such SQL injection attacks, SQL APIs offer parameterized
|
|
|
|
queries, which separate the executed query from user-controlled data
|
|
|
|
and make it impossible to run arbitrary queries. For example, with
|
|
|
|
`sqlite3 <https://docs.python.org/3/library/sqlite3.html>`_, our
|
|
|
|
original function would be written safely as a query with parameters:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def query_user(conn: Connection, user_id: str) -> User:
|
|
|
|
query = "SELECT * FROM data WHERE user_id = ?"
|
|
|
|
conn.execute(query, (user_id,))
|
2022-10-30 09:29:39 -04:00
|
|
|
...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
The problem is that there is no way to enforce this
|
|
|
|
discipline. sqlite3's own `documentation
|
|
|
|
<https://docs.python.org/3/library/sqlite3.html>`_ can only admonish
|
|
|
|
the reader to not dynamically build the ``sql`` argument from external
|
|
|
|
input; the API's authors cannot express that through the type
|
|
|
|
system. Users can (and often do) still use a convenient f-string as
|
|
|
|
before and leave their code vulnerable to SQL injection.
|
|
|
|
|
|
|
|
Existing tools, such as the popular security linter `Bandit
|
|
|
|
<https://github.com/PyCQA/bandit/blob/aac3f16f45648a7756727286ba8f8f0cf5e7d408/bandit/plugins/django_sql_injection.py#L102>`_,
|
|
|
|
attempt to detect unsafe external data used in SQL APIs, by inspecting
|
|
|
|
the AST or by other semantic pattern-matching. These tools, however,
|
|
|
|
preclude common idioms like storing a large multi-line query in a
|
|
|
|
variable before executing it, adding literal string modifiers to the
|
|
|
|
query based on some conditions, or transforming the query string using
|
2022-01-31 22:20:47 -05:00
|
|
|
a function. (We survey existing tools in the `Rejected Alternatives`_
|
2021-12-01 12:57:41 -05:00
|
|
|
section.) For example, many tools will detect a false positive issue
|
|
|
|
in this benign snippet:
|
|
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def query_data(conn: Connection, user_id: str, limit: bool) -> None:
|
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
user.name,
|
|
|
|
user.age
|
|
|
|
FROM data
|
|
|
|
WHERE user_id = ?
|
|
|
|
"""
|
|
|
|
if limit:
|
|
|
|
query += " LIMIT 1"
|
|
|
|
|
|
|
|
conn.execute(query, (user_id,))
|
|
|
|
|
|
|
|
We want to forbid harmful execution of user-controlled data while
|
|
|
|
still allowing benign idioms like the above and not requiring extra
|
|
|
|
user work.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
To meet this goal, we introduce the ``LiteralString`` type, which only
|
2021-12-01 12:57:41 -05:00
|
|
|
accepts string values that are known to be made of literals. This is a
|
2022-01-21 06:03:51 -05:00
|
|
|
generalization of the ``Literal["foo"]`` type from :pep:`586`.
|
|
|
|
A string of type
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` cannot contain user-controlled data. Thus, any API
|
|
|
|
that only accepts ``LiteralString`` will be immune to injection
|
2022-01-31 22:20:47 -05:00
|
|
|
vulnerabilities (with `pragmatic limitations <Appendix B:
|
2021-12-01 12:57:41 -05:00
|
|
|
Limitations_>`_).
|
|
|
|
|
|
|
|
Since we want the ``sqlite3`` ``execute`` method to disallow strings
|
|
|
|
built with user input, we would make its `typeshed stub
|
|
|
|
<https://github.com/python/typeshed/blob/1c88ceeee924ec6cfe05dd4865776b49fec299e6/stdlib/sqlite3/dbapi2.pyi#L153>`_
|
2022-02-01 14:00:27 -05:00
|
|
|
accept a ``sql`` query that is of type ``LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
from typing import LiteralString
|
|
|
|
|
|
|
|
def execute(self, sql: LiteralString, parameters: Iterable[str] = ...) -> Cursor: ...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
This successfully forbids our unsafe SQL example. The variable
|
|
|
|
``query`` below is inferred to have type ``str``, since it is created
|
|
|
|
from a format string using ``user_id``, and cannot be passed to
|
|
|
|
``execute``:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def query_user(conn: Connection, user_id: str) -> User:
|
|
|
|
query = f"SELECT * FROM data WHERE user_id = {user_id}"
|
2022-10-30 09:29:39 -04:00
|
|
|
conn.execute(query) # Error: Expected LiteralString, got str.
|
|
|
|
...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
The method remains flexible enough to allow our more complicated
|
|
|
|
example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def query_data(conn: Connection, user_id: str, limit: bool) -> None:
|
|
|
|
# This is a literal string.
|
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
user.name,
|
|
|
|
user.age
|
|
|
|
FROM data
|
|
|
|
WHERE user_id = ?
|
|
|
|
"""
|
|
|
|
|
|
|
|
if limit:
|
2022-02-01 14:00:27 -05:00
|
|
|
# Still has type LiteralString because we added a literal string.
|
2021-12-01 12:57:41 -05:00
|
|
|
query += " LIMIT 1"
|
|
|
|
|
|
|
|
conn.execute(query, (user_id,)) # OK
|
|
|
|
|
|
|
|
Notice that the user did not have to change their SQL code at all. The
|
|
|
|
type checker was able to infer the literal string type and complain
|
2022-01-26 19:57:26 -05:00
|
|
|
only in case of violations.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` is also useful in other cases where we want strict
|
2022-01-26 19:57:26 -05:00
|
|
|
command-data separation, such as when building shell commands or when
|
|
|
|
rendering a string into an HTML response without escaping (see
|
|
|
|
`Appendix A: Other Uses`_). Overall, this combination of strictness
|
|
|
|
and flexibility makes it easy to enforce safer API usage in sensitive
|
|
|
|
code without burdening users.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Usage statistics
|
|
|
|
----------------
|
|
|
|
|
|
|
|
In a sample of open-source projects using ``sqlite3``, we found that
|
2022-01-26 19:57:26 -05:00
|
|
|
``conn.execute`` was called `~67% of the time
|
2021-12-01 12:57:41 -05:00
|
|
|
<https://grep.app/search?q=conn%5C.execute%5C%28%5Cs%2A%5B%27%22%5D®exp=true&filter[lang][0]=Python>`_
|
2022-01-26 19:57:26 -05:00
|
|
|
with a safe string literal and `~33% of the time
|
2021-12-01 12:57:41 -05:00
|
|
|
<https://grep.app/search?current=3&q=conn%5C.execute%5C%28%5Ba-zA-Z_%5D%2B%5C%29®exp=true&filter[lang][0]=Python>`_
|
2022-01-26 19:57:26 -05:00
|
|
|
with a potentially unsafe, local string variable. Using this PEP's
|
|
|
|
literal string type along with a type checker would prevent the unsafe
|
|
|
|
portion of that 33% of cases (ie. the ones where user controlled data
|
|
|
|
is incorporated into the query), while seamlessly allowing the safe
|
|
|
|
ones to remain.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
|
|
|
Firstly, why use *types* to prevent security vulnerabilities?
|
|
|
|
|
|
|
|
Warning users in documentation is insufficient - most users either
|
|
|
|
never see these warnings or ignore them. Using an existing dynamic or
|
|
|
|
static analysis approach is too restrictive - these prevent natural
|
|
|
|
idioms, as we saw in the `Motivation`_ section (and will discuss more
|
|
|
|
extensively in the `Rejected Alternatives`_ section). The typing-based
|
|
|
|
approach in this PEP strikes a user-friendly balance between
|
|
|
|
strictness and flexibility.
|
|
|
|
|
|
|
|
Runtime approaches do not work because, at runtime, the query string
|
|
|
|
is a plain ``str``. While we could prevent some exploits using
|
|
|
|
heuristics, such as regex-filtering for obviously malicious payloads,
|
|
|
|
there will always be a way to work around them (perfectly
|
|
|
|
distinguishing good and bad queries reduces to the halting problem).
|
|
|
|
|
2022-01-31 22:20:47 -05:00
|
|
|
Static approaches, such as checking the AST to see if the query string
|
|
|
|
is a literal string expression, cannot tell when a string is assigned
|
|
|
|
to an intermediate variable or when it is transformed by a benign
|
2021-12-01 12:57:41 -05:00
|
|
|
function. This makes them overly restrictive.
|
|
|
|
|
|
|
|
The type checker, surprisingly, does better than both because it has
|
|
|
|
access to information not available in the runtime or static analysis
|
|
|
|
approaches. Specifically, the type checker can tell us whether an
|
|
|
|
expression has a literal string type, say ``Literal["foo"]``. The type
|
|
|
|
checker already propagates types across variable assignments or
|
|
|
|
function calls.
|
|
|
|
|
|
|
|
In the current type system itself, if the SQL or shell command
|
|
|
|
execution function only accepted three possible input strings, our job
|
|
|
|
would be done. We would just say:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def execute(query: Literal["foo", "bar", "baz"]) -> None: ...
|
|
|
|
|
|
|
|
But, of course, ``execute`` can accept *any* possible query. How do we
|
|
|
|
ensure that the query does not contain an arbitrary, user-controlled
|
|
|
|
string?
|
|
|
|
|
|
|
|
We want to specify that the value must be of some type
|
|
|
|
``Literal[<...>]`` where ``<...>`` is some string. This is what
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` represents. ``LiteralString`` is the "supertype" of
|
2022-01-26 19:57:26 -05:00
|
|
|
all literal string types. In effect, this PEP just introduces a type
|
|
|
|
in the type hierarchy between ``Literal["foo"]`` and ``str``. Any
|
2022-02-01 14:00:27 -05:00
|
|
|
particular literal string, such as ``Literal["foo"]`` or
|
|
|
|
``Literal["bar"]``, is compatible with ``LiteralString``, but not the
|
|
|
|
other way around. The "supertype" of ``LiteralString`` itself is
|
|
|
|
``str``. So, ``LiteralString`` is compatible with ``str``, but not the
|
2022-01-26 19:57:26 -05:00
|
|
|
other way around.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Note that a ``Union`` of literal types is naturally compatible with
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` because each element of the ``Union`` is individually
|
|
|
|
compatible with ``LiteralString``. So, ``Literal["foo", "bar"]`` is
|
|
|
|
compatible with ``LiteralString``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
However, recall that we don't just want to represent exact literal
|
|
|
|
queries. We also want to support composition of two literal strings,
|
|
|
|
such as ``query + " LIMIT 1"``. This too is possible with the above
|
2022-02-01 14:00:27 -05:00
|
|
|
concept. If ``x`` and ``y`` are two values of type ``LiteralString``,
|
2021-12-01 12:57:41 -05:00
|
|
|
then ``x + y`` will also be of type compatible with
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``. We can reason about this by looking at specific
|
2021-12-01 12:57:41 -05:00
|
|
|
instances such as ``Literal["foo"]`` and ``Literal["bar"]``; the value
|
|
|
|
of the added string ``x + y`` can only be ``"foobar"``, which has type
|
|
|
|
``Literal["foobar"]`` and is thus compatible with
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``. The same reasoning applies when ``x`` and ``y`` are
|
2021-12-01 12:57:41 -05:00
|
|
|
unions of literal types; the result of pairwise adding any two literal
|
|
|
|
types from ``x`` and ``y`` respectively is a literal type, which means
|
|
|
|
that the overall result is a ``Union`` of literal types and is thus
|
2022-02-01 14:00:27 -05:00
|
|
|
compatible with ``LiteralString``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
In this way, we are able to leverage Python's concept of a ``Literal``
|
|
|
|
string type to specify that our API can only accept strings that are
|
|
|
|
known to be constructed from literals. More specific details follow in
|
|
|
|
the remaining sections.
|
|
|
|
|
2022-02-07 20:14:26 -05:00
|
|
|
Specification
|
|
|
|
=============
|
|
|
|
|
|
|
|
|
|
|
|
Runtime Behavior
|
|
|
|
----------------
|
|
|
|
|
|
|
|
We propose adding ``LiteralString`` to ``typing.py``, with an
|
|
|
|
implementation similar to ``typing.NoReturn``.
|
|
|
|
|
|
|
|
Note that ``LiteralString`` is a special form used solely for type
|
|
|
|
checking. There is no expression for which ``type(<expr>)`` will
|
|
|
|
produce ``LiteralString`` at runtime. So, we do not specify in the
|
|
|
|
implementation that it is a subclass of ``str``.
|
|
|
|
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Valid Locations for ``LiteralString``
|
2022-02-07 20:14:26 -05:00
|
|
|
-----------------------------------------
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` can be used where any other type can be used:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
variable_annotation: LiteralString
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def my_function(literal_string: LiteralString) -> LiteralString: ...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
class Foo:
|
2022-02-01 14:00:27 -05:00
|
|
|
my_attribute: LiteralString
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
type_argument: List[LiteralString]
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
T = TypeVar("T", bound=LiteralString)
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
It cannot be nested within unions of ``Literal`` types:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
bad_union: Literal["hello", LiteralString] # Not OK
|
|
|
|
bad_nesting: Literal[LiteralString] # Not OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
Type Inference
|
2022-02-07 20:14:26 -05:00
|
|
|
--------------
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
.. _inferring_literal_string:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-07 20:14:26 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Inferring ``LiteralString``
|
2022-02-07 20:14:26 -05:00
|
|
|
'''''''''''''''''''''''''''
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Any literal string type is compatible with ``LiteralString``. For
|
|
|
|
example, ``x: LiteralString = "foo"`` is valid because ``"foo"`` is
|
2021-12-01 12:57:41 -05:00
|
|
|
inferred to be of type ``Literal["foo"]``.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
As per the `Rationale`_, we also infer ``LiteralString`` in the
|
2021-12-01 12:57:41 -05:00
|
|
|
following cases:
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
+ Addition: ``x + y`` is of type ``LiteralString`` if both ``x`` and
|
|
|
|
``y`` are compatible with ``LiteralString``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
+ Joining: ``sep.join(xs)`` is of type ``LiteralString`` if ``sep``'s
|
|
|
|
type is compatible with ``LiteralString`` and ``xs``'s type is
|
|
|
|
compatible with ``Iterable[LiteralString]``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
+ In-place addition: If ``s`` has type ``LiteralString`` and ``x`` has
|
|
|
|
type compatible with ``LiteralString``, then ``s += x`` preserves
|
|
|
|
``s``'s type as ``LiteralString``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
+ String formatting: An f-string has type ``LiteralString`` if and only
|
2021-12-01 12:57:41 -05:00
|
|
|
if its constituent expressions are literal strings. ``s.format(...)``
|
2022-02-01 14:00:27 -05:00
|
|
|
has type ``LiteralString`` if and only if ``s`` and the arguments have
|
|
|
|
types compatible with ``LiteralString``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
+ Literal-preserving methods: In `Appendix C <PEP 675 Appendix C_>`_,
|
|
|
|
we have provided an exhaustive list of ``str`` methods that preserve the
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` type.
|
2022-01-31 22:20:47 -05:00
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
In all other cases, if one or more of the composed values has a
|
|
|
|
non-literal type ``str``, the composition of types will have type
|
|
|
|
``str``. For example, if ``s`` has type ``str``, then ``"hello" + s``
|
|
|
|
has type ``str``. This matches the pre-existing behavior of type
|
|
|
|
checkers.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` is compatible with the type ``str``. It inherits all
|
2021-12-01 12:57:41 -05:00
|
|
|
methods from ``str``. So, if we have a variable ``s`` of type
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``, it is safe to write ``s.startswith("hello")``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Some type checkers refine the type of a string when doing an equality
|
|
|
|
check:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def foo(s: str) -> None:
|
|
|
|
if s == "bar":
|
|
|
|
reveal_type(s) # => Literal["bar"]
|
|
|
|
|
|
|
|
Such a refined type in the if-block is also compatible with
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` because its type is ``Literal["bar"]``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
Examples
|
2022-02-07 20:14:26 -05:00
|
|
|
''''''''
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
See the examples below to help clarify the above rules:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
literal_string: LiteralString
|
2021-12-01 12:57:41 -05:00
|
|
|
s: str = literal_string # OK
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
literal_string: LiteralString = s # Error: Expected LiteralString, got str.
|
|
|
|
literal_string: LiteralString = "hello" # OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Addition of literal strings:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-02 07:49:27 -05:00
|
|
|
def expect_literal_string(s: LiteralString) -> None: ...
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string("foo" + "bar") # OK
|
|
|
|
expect_literal_string(literal_string + "bar") # OK
|
2022-02-15 20:08:01 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
literal_string2: LiteralString
|
|
|
|
expect_literal_string(literal_string + literal_string2) # OK
|
2022-02-15 20:08:01 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
plain_string: str
|
|
|
|
expect_literal_string(literal_string + plain_string) # Not OK.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Join using literal strings:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string(",".join(["foo", "bar"])) # OK
|
|
|
|
expect_literal_string(literal_string.join(["foo", "bar"])) # OK
|
|
|
|
expect_literal_string(literal_string.join([literal_string, literal_string2])) # OK
|
2022-02-15 20:08:01 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
xs: List[LiteralString]
|
|
|
|
expect_literal_string(literal_string.join(xs)) # OK
|
|
|
|
expect_literal_string(plain_string.join([literal_string, literal_string2]))
|
2022-02-02 07:49:27 -05:00
|
|
|
# Not OK because the separator has type 'str'.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
In-place addition using literal strings:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
literal_string += "foo" # OK
|
|
|
|
literal_string += literal_string2 # OK
|
2022-02-01 14:00:27 -05:00
|
|
|
literal_string += plain_string # Not OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Format strings using literal strings:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
literal_name: LiteralString
|
|
|
|
expect_literal_string(f"hello {literal_name}")
|
2021-12-01 12:57:41 -05:00
|
|
|
# OK because it is composed from literal strings.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string("hello {}".format(literal_name)) # OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string(f"hello") # OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-02 07:49:27 -05:00
|
|
|
username: str
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string(f"hello {username}")
|
2022-02-02 07:49:27 -05:00
|
|
|
# NOT OK. The format-string is constructed from 'username',
|
|
|
|
# which has type 'str'.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string("hello {}".format(username)) # Not OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Other literal types, such as literal integers, are not compatible with ``LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
some_int: int
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string(some_int) # Error: Expected LiteralString, got int.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
literal_one: Literal[1] = 1
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string(literal_one) # Error: Expected LiteralString, got Literal[1].
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
We can call functions on literal strings:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def add_limit(query: LiteralString) -> LiteralString:
|
2021-12-01 12:57:41 -05:00
|
|
|
return query + " LIMIT = 1"
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def my_query(query: LiteralString, user_id: str) -> None:
|
2021-12-01 12:57:41 -05:00
|
|
|
sql_connection().execute(add_limit(query), (user_id,)) # OK
|
|
|
|
|
|
|
|
Conditional statements and expressions work as expected:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def return_literal_string() -> LiteralString:
|
2021-12-01 12:57:41 -05:00
|
|
|
return "foo" if condition1() else "bar" # OK
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def return_literal_str2(literal_string: LiteralString) -> LiteralString:
|
|
|
|
return "foo" if condition1() else literal_string # OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def return_literal_str3() -> LiteralString:
|
2021-12-01 12:57:41 -05:00
|
|
|
if condition1():
|
|
|
|
result: Literal["foo"] = "foo"
|
|
|
|
else:
|
2022-02-01 14:00:27 -05:00
|
|
|
result: LiteralString = "bar"
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
return result # OK
|
|
|
|
|
|
|
|
|
|
|
|
Interaction with TypeVars and Generics
|
2022-02-07 20:14:26 -05:00
|
|
|
''''''''''''''''''''''''''''''''''''''
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
TypeVars can be bound to ``LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
from typing import Literal, LiteralString, TypeVar
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
TLiteral = TypeVar("TLiteral", bound=LiteralString)
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
def literal_identity(s: TLiteral) -> TLiteral:
|
|
|
|
return s
|
|
|
|
|
|
|
|
hello: Literal["hello"] = "hello"
|
|
|
|
y = literal_identity(hello)
|
|
|
|
reveal_type(y) # => Literal["hello"]
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
s: LiteralString
|
2021-12-01 12:57:41 -05:00
|
|
|
y2 = literal_identity(s)
|
2022-02-01 14:00:27 -05:00
|
|
|
reveal_type(y2) # => LiteralString
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
s_error: str
|
|
|
|
literal_identity(s_error)
|
2022-02-01 14:00:27 -05:00
|
|
|
# Error: Expected TLiteral (bound to LiteralString), got str.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` can be used as a type argument for generic classes:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
class Container(Generic[T]):
|
|
|
|
def __init__(self, value: T) -> None:
|
|
|
|
self.value = value
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
literal_string: LiteralString = "hello"
|
|
|
|
x: Container[LiteralString] = Container(literal_string) # OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
s: str
|
2022-02-01 14:00:27 -05:00
|
|
|
x_error: Container[LiteralString] = Container(s) # Not OK
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Standard containers like ``List`` work as expected:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
xs: List[LiteralString] = ["foo", "bar", "baz"]
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-07 20:14:26 -05:00
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Interactions with Overloads
|
2022-02-07 20:14:26 -05:00
|
|
|
'''''''''''''''''''''''''''
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Literal strings and overloads do not need to interact in a special
|
2022-02-01 14:00:27 -05:00
|
|
|
way: the existing rules work fine. ``LiteralString`` can be used as a
|
2021-12-01 12:57:41 -05:00
|
|
|
fallback overload where a specific ``Literal["foo"]`` type does not
|
|
|
|
match:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def foo(x: Literal["foo"]) -> int: ...
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def foo(x: LiteralString) -> bool: ...
|
2021-12-01 12:57:41 -05:00
|
|
|
@overload
|
|
|
|
def foo(x: str) -> str: ...
|
|
|
|
|
|
|
|
x1: int = foo("foo") # First overload.
|
|
|
|
x2: bool = foo("bar") # Second overload.
|
|
|
|
s: str
|
|
|
|
x3: str = foo(s) # Third overload.
|
|
|
|
|
2022-01-26 19:57:26 -05:00
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Backwards Compatibility
|
2022-01-26 19:57:26 -05:00
|
|
|
=======================
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
We propose adding ``typing_extensions.LiteralString`` for use in
|
|
|
|
earlier Python versions.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
As :pep:`PEP 586 mentions
|
|
|
|
<586#backwards-compatibility>`,
|
2021-12-01 12:57:41 -05:00
|
|
|
type checkers "should feel free to experiment with more sophisticated
|
|
|
|
inference techniques". So, if the type checker infers a literal string
|
|
|
|
type for an unannotated variable that is initialized with a literal
|
|
|
|
string, the following example should be OK:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
x = "hello"
|
2022-02-01 14:00:27 -05:00
|
|
|
expect_literal_string(x)
|
2022-02-02 07:49:27 -05:00
|
|
|
# OK, because x is inferred to have type 'Literal["hello"]'.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
This enables precise type checking of idiomatic SQL query code without
|
|
|
|
annotating the code at all (as seen in the `Motivation`_ section
|
|
|
|
example).
|
|
|
|
|
2022-01-21 06:03:51 -05:00
|
|
|
However, like :pep:`586`, this PEP does not mandate the above inference
|
2021-12-01 12:57:41 -05:00
|
|
|
strategy. In case the type checker doesn't infer ``x`` to have type
|
|
|
|
``Literal["hello"]``, users can aid the type checker by explicitly
|
2022-02-01 14:00:27 -05:00
|
|
|
annotating it as ``x: LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
x: LiteralString = "hello"
|
|
|
|
expect_literal_string(x)
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-01-26 19:57:26 -05:00
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Rejected Alternatives
|
|
|
|
=====================
|
|
|
|
|
|
|
|
Why not use tool X?
|
|
|
|
-------------------
|
|
|
|
|
2022-01-31 22:20:47 -05:00
|
|
|
Tools to catch issues such as SQL injection seem to come in three
|
|
|
|
flavors: AST based, function level analysis, and taint flow analysis.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-01-31 22:20:47 -05:00
|
|
|
**AST-based tools**: `Bandit
|
2021-12-01 12:57:41 -05:00
|
|
|
<https://github.com/PyCQA/bandit/blob/aac3f16f45648a7756727286ba8f8f0cf5e7d408/bandit/plugins/django_sql_injection.py#L102>`_
|
|
|
|
has a plugin to warn when SQL queries are not literal
|
|
|
|
strings. The problem is that many perfectly safe SQL
|
|
|
|
queries are dynamically built out of string literals, as shown in the
|
|
|
|
`Motivation`_ section. At the
|
|
|
|
AST level, the resultant SQL query is not going to appear as a string
|
|
|
|
literal anymore and is thus indistinguishable from a potentially
|
|
|
|
malicious string. To use these tools would require significantly
|
2022-02-01 14:00:27 -05:00
|
|
|
restricting developers' ability to build SQL queries. ``LiteralString``
|
2021-12-01 12:57:41 -05:00
|
|
|
can provide similar safety guarantees with fewer restrictions.
|
|
|
|
|
|
|
|
**Semgrep and pyanalyze**: Semgrep supports a more sophisticated
|
|
|
|
function level analysis, including `constant propagation
|
|
|
|
<https://semgrep.dev/docs/writing-rules/data-flow/#constant-propagation>`_
|
|
|
|
within a function. This allows us to prevent injection attacks while
|
|
|
|
permitting some forms of safe dynamic SQL queries within a
|
|
|
|
function. `pyanalyze
|
|
|
|
<https://github.com/quora/pyanalyze/blob/afcb58cd3e967e4e3fea9e57bb18b6b1d9d42ed7/README.md#extending-pyanalyze>`_
|
|
|
|
has a similar extension. But neither handles function calls that
|
|
|
|
construct and return safe SQL queries. For example, in the code sample
|
|
|
|
below, ``build_insert_query`` is a helper function to create a query
|
|
|
|
that inserts multiple values into the corresponding columns. Semgrep
|
2022-02-01 14:00:27 -05:00
|
|
|
and pyanalyze forbid this natural usage whereas ``LiteralString``
|
2021-12-01 12:57:41 -05:00
|
|
|
handles it with no burden on the programmer:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
def build_insert_query(
|
2022-02-01 14:00:27 -05:00
|
|
|
table: LiteralString
|
|
|
|
insert_columns: Iterable[LiteralString],
|
|
|
|
) -> LiteralString:
|
2021-12-01 12:57:41 -05:00
|
|
|
sql = "INSERT INTO " + table
|
|
|
|
|
|
|
|
column_clause = ", ".join(insert_columns)
|
|
|
|
value_clause = ", ".join(["?"] * len(insert_columns))
|
|
|
|
|
|
|
|
sql += f" ({column_clause}) VALUES ({value_clause})"
|
|
|
|
return sql
|
|
|
|
|
|
|
|
def insert_data(
|
|
|
|
conn: Connection,
|
2022-02-01 14:00:27 -05:00
|
|
|
kvs_to_insert: Dict[LiteralString, str]
|
2021-12-01 12:57:41 -05:00
|
|
|
) -> None:
|
|
|
|
query = build_insert_query("data", kvs_to_insert.keys())
|
|
|
|
conn.execute(query, kvs_to_insert.values())
|
|
|
|
|
|
|
|
# Example usage
|
|
|
|
data_to_insert = {
|
2022-01-31 22:20:47 -05:00
|
|
|
"column_1": value_1, # Note: values are not literals
|
2021-12-01 12:57:41 -05:00
|
|
|
"column_2": value_2,
|
|
|
|
"column_3": value_3,
|
|
|
|
}
|
|
|
|
insert_data(conn, data_to_insert)
|
|
|
|
|
|
|
|
|
|
|
|
**Taint flow analysis**: Tools such as `Pysa
|
|
|
|
<https://pyre-check.org/docs/pysa-basics/>`_ or `CodeQL
|
|
|
|
<https://codeql.github.com/>`_ are capable of tracking data flowing
|
|
|
|
from a user controlled input into a SQL query. These tools are
|
|
|
|
powerful but involve considerable overhead in setting up the tool in
|
|
|
|
CI, defining "taint" sinks and sources, and teaching developers how to
|
|
|
|
use them. They also usually take longer to run than a type checker
|
|
|
|
(minutes instead of seconds), which means feedback is not
|
|
|
|
immediate. Finally, they move the burden of preventing vulnerabilities
|
|
|
|
on to library users instead of allowing the libraries themselves to
|
|
|
|
specify precisely how their APIs must be called (as is possible with
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``).
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-01-31 22:20:47 -05:00
|
|
|
One final reason to prefer using a new type over a dedicated tool is
|
|
|
|
that type checkers are more widely used than dedicated security
|
|
|
|
tooling; for example, MyPy was downloaded `over 7 million times
|
|
|
|
<https://www.pypistats.org/packages/mypy>`_ in Jan 2022 vs `less than
|
|
|
|
2 million times <https://www.pypistats.org/packages/bandit>`_ for
|
|
|
|
Bandit. Having security protections built right into type checkers
|
|
|
|
will mean that more developers benefit from them.
|
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Why not use a ``NewType`` for ``str``?
|
|
|
|
--------------------------------------
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Any API for which ``LiteralString`` would be suitable could instead be
|
2021-12-01 12:57:41 -05:00
|
|
|
updated to accept a different type created within the Python type
|
|
|
|
system, such as ``NewType("SafeSQL", str)``:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
SafeSQL = NewType("SafeSQL", str)
|
|
|
|
|
|
|
|
def execute(self, sql: SafeSQL, parameters: Iterable[str] = ...) -> Cursor: ...
|
|
|
|
|
|
|
|
execute(SafeSQL("SELECT * FROM data WHERE user_id = ?"), user_id) # OK
|
|
|
|
|
|
|
|
user_query: str
|
|
|
|
execute(user_query) # Error: Expected SafeSQL, got str.
|
|
|
|
|
|
|
|
|
|
|
|
Having to create a new type to call an API might give some developers
|
|
|
|
pause and encourage more caution, but it doesn't guarantee that
|
|
|
|
developers won't just turn a user controlled string into the new type,
|
|
|
|
and pass it into the modified API anyway:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
query = f"SELECT * FROM data WHERE user_id = f{user_id}"
|
|
|
|
execute(SafeSQL(query)) # No error!
|
|
|
|
|
|
|
|
We are back to square one with the problem of preventing arbitrary
|
|
|
|
inputs to ``SafeSQL``. This is not a theoretical concern
|
|
|
|
either. Django uses the above approach with ``SafeString`` and
|
|
|
|
`mark_safe
|
|
|
|
<https://docs.djangoproject.com/en/dev/_modules/django/utils/safestring/#SafeString>`_. Issues
|
|
|
|
such as `CVE-2020-13596
|
|
|
|
<https://github.com/django/django/commit/2dd4d110c159d0c81dff42eaead2c378a0998735>`_
|
|
|
|
show how this technique can `fail
|
|
|
|
<https://nvd.nist.gov/vuln/detail/CVE-2020-13596>`_.
|
|
|
|
|
|
|
|
Also note that this requires invasive changes to the source code
|
2022-02-01 14:00:27 -05:00
|
|
|
(wrapping the query with ``SafeSQL``) whereas ``LiteralString``
|
2021-12-01 12:57:41 -05:00
|
|
|
requires no such changes. Users can remain oblivious to it as long as
|
|
|
|
they pass in literal strings to sensitive APIs.
|
|
|
|
|
|
|
|
Why not try to emulate Trusted Types?
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
`Trusted Types
|
|
|
|
<https://w3c.github.io/webappsec-trusted-types/dist/spec/>`_ is a W3C
|
|
|
|
specification for preventing DOM-based Cross Site Scripting (XSS). XSS
|
|
|
|
occurs when dangerous browser APIs accept raw user-controlled
|
|
|
|
strings. The specification modifies these APIs to accept only the
|
|
|
|
"Trusted Types" returned by designated sanitizing functions. These
|
|
|
|
sanitizing functions must take in a potentially malicious string and
|
|
|
|
validate it or render it benign somehow, for example by verifying that
|
|
|
|
it is a valid URL or HTML-encoding it.
|
|
|
|
|
|
|
|
It can be tempting to assume porting the concept of Trusted Types to
|
|
|
|
Python could solve the problem. The fundamental difference, however,
|
|
|
|
is that the output of a Trusted Types sanitizer is usually intended
|
|
|
|
*to not be executable code*. Thus it's easy to HTML encode the input,
|
|
|
|
strip out dangerous tags, or otherwise render it inert. With a SQL
|
|
|
|
query or shell command, the end result *still needs to be executable
|
|
|
|
code*. There is no way to write a sanitizer that can reliably figure
|
|
|
|
out which parts of an input string are benign and which ones are
|
|
|
|
potentially malicious.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Runtime Checkable ``LiteralString``
|
|
|
|
-----------------------------------
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
The ``LiteralString`` concept could be extended beyond static type
|
2021-12-01 12:57:41 -05:00
|
|
|
checking to be a runtime checkable property of ``str`` objects. This
|
|
|
|
would provide some benefits, such as allowing frameworks to raise
|
|
|
|
errors on dynamic strings. Such runtime errors would be a more robust
|
|
|
|
defense mechanism than type errors, which can potentially be
|
|
|
|
suppressed, ignored, or never even seen if the author does not use a
|
|
|
|
type checker.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
This extension to the ``LiteralString`` concept would dramatically
|
2021-12-01 12:57:41 -05:00
|
|
|
increase the scope of the proposal by requiring changes to one of the
|
|
|
|
most fundamental types in Python. While runtime taint checking on
|
2022-02-02 16:43:20 -05:00
|
|
|
strings, similar to Perl's `taint <https://metacpan.org/pod/Taint>`_,
|
|
|
|
has been `considered <https://bugs.python.org/issue500698>`_ and
|
|
|
|
`attempted <https://github.com/felixgr/pytaint>`_ in the past, and
|
2021-12-01 12:57:41 -05:00
|
|
|
others may consider it in the future, such extensions are out of scope
|
|
|
|
for this PEP.
|
|
|
|
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Rejected Names
|
|
|
|
--------------
|
|
|
|
|
|
|
|
We considered a variety of names for the literal string type and
|
|
|
|
solicited ideas on `typing-sig
|
|
|
|
<https://mail.python.org/archives/list/typing-sig@python.org/thread/VB74EHNM4RODDFM64NEEEBJQVAUAWIAW/>`_.
|
|
|
|
Some notable alternatives were:
|
|
|
|
|
|
|
|
+ ``Literal[str]``: This is a natural extension of the
|
|
|
|
``Literal["foo"]`` type name, but typing-sig `objected
|
|
|
|
<https://mail.python.org/archives/list/typing-sig@python.org/message/2ZQO4NTJEI42KTRJDBL77MNANEXOW7UI/>`_
|
|
|
|
that users could mistake this for the literal type of the ``str``
|
|
|
|
class.
|
|
|
|
|
|
|
|
+ ``LiteralStr``: This is shorter than ``LiteralString`` but looks
|
|
|
|
weird to the PEP authors.
|
|
|
|
|
|
|
|
+ ``LiteralDerivedString``: This (along with
|
|
|
|
``MadeFromLiteralString``) best captures the technical meaning of
|
|
|
|
the type. It represents not just the type of literal expressions,
|
|
|
|
such as ``"foo"``, but also that of expressions composed from
|
|
|
|
literals, such as ``"foo" + "bar"``. However, both names seem wordy.
|
|
|
|
|
|
|
|
+ ``StringLiteral``: Users might confuse this with the existing
|
|
|
|
concept of `"string literals"
|
|
|
|
<https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals>`_
|
|
|
|
where the string exists as a syntactic token in the source code,
|
|
|
|
whereas our concept is more general.
|
|
|
|
|
|
|
|
+ ``SafeString``: While this comes close to our intended meaning, it
|
|
|
|
may mislead users into thinking that the string has been sanitized in
|
|
|
|
some way, perhaps by escaping HTML tags or shell-related special
|
|
|
|
characters.
|
|
|
|
|
|
|
|
+ ``ConstantStr``: This does not capture the idea of composing literal
|
|
|
|
strings.
|
|
|
|
|
|
|
|
+ ``StaticStr``: This suggests that the string is statically
|
|
|
|
computable, i.e., computable without running the program, which is
|
|
|
|
not true. The literal string may vary based on runtime flags, as
|
|
|
|
seen in the `Motivation`_ examples.
|
|
|
|
|
|
|
|
+ ``LiteralOnly[str]``: This has the advantage of being extensible to
|
|
|
|
other literal types, such as ``bytes`` or ``int``. However, we did
|
|
|
|
not find the extensibility worth the loss of readability.
|
|
|
|
|
|
|
|
Overall, there was no clear winner on typing-sig over a long period,
|
|
|
|
so we decided to tip the scales in favor of ``LiteralString``.
|
|
|
|
|
|
|
|
|
|
|
|
``LiteralBytes``
|
|
|
|
----------------
|
|
|
|
|
|
|
|
We could generalize literal byte types, such as ``Literal[b"foo"]``,
|
|
|
|
to ``LiteralBytes``. However, literal byte types are used much less
|
|
|
|
frequently than literal string types and we did not find much user
|
|
|
|
demand for ``LiteralBytes``, so we decided not to include it in this
|
|
|
|
PEP. Others may, however, consider it in future PEPs.
|
|
|
|
|
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Reference Implementation
|
|
|
|
========================
|
|
|
|
|
|
|
|
This is implemented in Pyre v0.9.8 and is actively being used.
|
|
|
|
|
|
|
|
The implementation simply extends the type checker with
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` as a supertype of literal string types.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
To support composition via addition, join, etc., it was sufficient to
|
2022-01-31 22:20:47 -05:00
|
|
|
overload the stubs for ``str`` in Pyre's copy of typeshed.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
Appendix A: Other Uses
|
|
|
|
======================
|
|
|
|
|
|
|
|
To simplify the discussion and require minimal security knowledge, we
|
2022-02-01 14:00:27 -05:00
|
|
|
focused on SQL injections throughout the PEP. ``LiteralString``,
|
2021-12-01 12:57:41 -05:00
|
|
|
however, can also be used to prevent many other kinds of `injection
|
|
|
|
vulnerabilities <https://owasp.org/www-community/Injection_Flaws>`_.
|
|
|
|
|
|
|
|
Command Injection
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
APIs such as ``subprocess.run`` accept a string which can be run as a
|
|
|
|
shell command:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
subprocess.run(f"echo 'Hello {name}'", shell=True)
|
|
|
|
|
2022-02-15 20:08:01 -05:00
|
|
|
If user-controlled data is included in the command string, the code is
|
2022-02-15 22:20:15 -05:00
|
|
|
vulnerable to "command injection"; i.e., an attacker can run malicious
|
2022-02-15 20:08:01 -05:00
|
|
|
commands. For example, a value of ``' && rm -rf / #`` would result in
|
|
|
|
the following destructive command being run:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
echo 'Hello ' && rm -rf / #'
|
|
|
|
|
|
|
|
This vulnerability could be prevented by updating ``run`` to only
|
2022-02-01 14:00:27 -05:00
|
|
|
accept ``LiteralString`` when used in ``shell=True`` mode. Here is one
|
2021-12-01 12:57:41 -05:00
|
|
|
simplified stub:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def run(command: LiteralString, *args: str, shell: bool=...): ...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Cross Site Scripting (XSS)
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
Most popular Python web frameworks, such as Django, use a templating
|
|
|
|
engine to produce HTML from user data. These templating languages
|
|
|
|
auto-escape user data before inserting it into the HTML template and
|
|
|
|
thus prevent cross site scripting (XSS) vulnerabilities.
|
|
|
|
|
|
|
|
But a common way to `bypass auto-escaping
|
|
|
|
<https://django.readthedocs.io/en/stable/ref/templates/language.html#how-to-turn-it-off>`_
|
|
|
|
and render HTML as-is is to use functions like ``mark_safe`` in
|
|
|
|
`Django
|
|
|
|
<https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.safestring.mark_safe>`_
|
|
|
|
or ``do_mark_safe`` in `Jinja2
|
2022-01-31 21:49:58 -05:00
|
|
|
<https://github.com/pallets/jinja/blob/077b7918a7642ff6742fe48a32e54d7875140894/src/jinja2/filters.py#L1264>`_,
|
2021-12-01 12:57:41 -05:00
|
|
|
which cause XSS vulnerabilities:
|
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: bad
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
dangerous_string = django.utils.safestring.mark_safe(f"<script>{user_input}</script>")
|
|
|
|
return(dangerous_string)
|
|
|
|
|
|
|
|
This vulnerability could be prevented by updating ``mark_safe`` to
|
2022-02-01 14:00:27 -05:00
|
|
|
only accept ``LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: good
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def mark_safe(s: LiteralString) -> str: ...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Server Side Template Injection (SSTI)
|
|
|
|
-------------------------------------
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Templating frameworks, such as Jinja, allow Python expressions which
|
2021-12-01 12:57:41 -05:00
|
|
|
will be evaluated and substituted into the rendered result:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
template_str = "There are {{ len(values) }} values: {{ values }}"
|
|
|
|
template = jinja2.Template(template_str)
|
|
|
|
template.render(values=[1, 2])
|
|
|
|
# Result: "There are 2 values: [1, 2]"
|
|
|
|
|
|
|
|
If an attacker controls all or part of the template string, they can
|
|
|
|
insert expressions which execute arbitrary code and `compromise
|
|
|
|
<https://www.onsecurity.io/blog/server-side-template-injection-with-jinja2/>`_
|
|
|
|
the application:
|
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: bad
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
malicious_str = "{{''.__class__.__base__.__subclasses__()[408]('rm - rf /',shell=True)}}"
|
|
|
|
template = jinja2.Template(malicious_str)
|
|
|
|
template.render()
|
|
|
|
# Result: The shell command 'rm - rf /' is run
|
|
|
|
|
|
|
|
Template injection exploits like this could be prevented by updating
|
2022-02-01 14:00:27 -05:00
|
|
|
the ``Template`` API to only accept ``LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: good
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
class Template:
|
2022-02-01 14:00:27 -05:00
|
|
|
def __init__(self, source: LiteralString): ...
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
|
2022-01-31 22:20:47 -05:00
|
|
|
Logging Format String Injection
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
Logging frameworks often allow their input strings to contain
|
|
|
|
formatting directives. At its worst, allowing users to control the
|
|
|
|
logged string has led to `CVE-2021-44228
|
|
|
|
<https://nvd.nist.gov/vuln/detail/CVE-2021-44228>`_ (colloquially
|
|
|
|
known as ``log4shell``), which has been described as the `"most
|
|
|
|
critical vulnerability of the last decade"
|
|
|
|
<https://www.theguardian.com/technology/2021/dec/10/software-flaw-most-critical-vulnerability-log-4-shell>`_.
|
|
|
|
While no Python frameworks are currently known to be vulnerable to a
|
|
|
|
similar attack, the built-in logging framework does provide formatting
|
|
|
|
options which are vulnerable to Denial of Service attacks from
|
|
|
|
externally controlled logging strings. The following example
|
|
|
|
illustrates a simple denial of service scenario:
|
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: bad
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
external_string = "%(foo)999999999s"
|
|
|
|
...
|
|
|
|
# Tries to add > 1GB of whitespace to the logged string:
|
|
|
|
logger.info(f'Received: {external_string}', some_dict)
|
|
|
|
|
|
|
|
This kind of attack could be prevented by requiring that the format
|
2022-02-01 14:00:27 -05:00
|
|
|
string passed to the logger be a ``LiteralString`` and that all
|
2022-01-31 22:20:47 -05:00
|
|
|
externally controlled data be passed separately as arguments (as
|
|
|
|
proposed in `Issue 46200 <https://bugs.python.org/issue46200>`_):
|
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: good
|
2022-01-31 22:20:47 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def info(msg: LiteralString, *args: object) -> None:
|
2022-01-31 22:20:47 -05:00
|
|
|
...
|
|
|
|
|
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Appendix B: Limitations
|
|
|
|
=======================
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
There are a number of ways ``LiteralString`` could still fail to
|
2021-12-01 12:57:41 -05:00
|
|
|
prevent users from passing strings built from non-literal data to an
|
|
|
|
API:
|
|
|
|
|
|
|
|
1. If the developer does not use a type checker or does not add type
|
|
|
|
annotations, then violations will go uncaught.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
2. ``cast(LiteralString, non_literal_string)`` could be used to lie to
|
|
|
|
the type checker and allow a dynamic string value to masquerade as a
|
|
|
|
``LiteralString``. The same goes for a variable that has type ``Any``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
3. Comments such as ``# type: ignore`` could be used to ignore
|
|
|
|
warnings about non-literal strings.
|
|
|
|
|
|
|
|
4. Trivial functions could be constructed to convert a ``str`` to a
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``:
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2023-12-11 18:22:29 -05:00
|
|
|
.. code-block::
|
|
|
|
:class: bad
|
2021-12-01 12:57:41 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
def make_literal(s: str) -> LiteralString:
|
|
|
|
letters: Dict[str, LiteralString] = {
|
2021-12-01 12:57:41 -05:00
|
|
|
"A": "A",
|
|
|
|
"B": "B",
|
|
|
|
...
|
|
|
|
}
|
2022-02-01 14:00:27 -05:00
|
|
|
output: List[LiteralString] = [letters[c] for c in s]
|
2021-12-01 12:57:41 -05:00
|
|
|
return "".join(output)
|
|
|
|
|
|
|
|
|
|
|
|
We could mitigate the above using linting, code review, etc., but
|
|
|
|
ultimately a clever, malicious developer attempting to circumvent the
|
2022-02-01 14:00:27 -05:00
|
|
|
protections offered by ``LiteralString`` will always succeed. The
|
|
|
|
important thing to remember is that ``LiteralString`` is not intended
|
2021-12-01 12:57:41 -05:00
|
|
|
to protect against *malicious* developers; it is meant to protect
|
|
|
|
against benign developers accidentally using sensitive APIs in a
|
|
|
|
dangerous way (without getting in their way otherwise).
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Without ``LiteralString``, the best enforcement tool API authors have
|
2021-12-01 12:57:41 -05:00
|
|
|
is documentation, which is easily ignored and often not seen. With
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``, API misuse requires conscious thought and artifacts
|
2021-12-01 12:57:41 -05:00
|
|
|
in the code that reviewers and future developers can notice.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
.. _PEP 675 Appendix C:
|
2022-01-31 22:20:47 -05:00
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Appendix C: ``str`` methods that preserve ``LiteralString``
|
|
|
|
===========================================================
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
The ``str`` class has several methods that would benefit from
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString``. For example, users might expect
|
|
|
|
``"hello".capitalize()`` to have the type ``LiteralString`` similar to
|
|
|
|
the other examples we have seen in the `Inferring LiteralString
|
|
|
|
<inferring_literal_string_>`_ section. Inferring the type
|
|
|
|
``LiteralString`` is correct because the string is not an arbitrary
|
|
|
|
user-supplied string - we know that it has the type
|
|
|
|
``Literal["HELLO"]``, which is compatible with ``LiteralString``. In
|
|
|
|
other words, the ``capitalize`` method preserves the ``LiteralString``
|
|
|
|
type. There are several other ``str`` methods that preserve
|
|
|
|
``LiteralString``.
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
We propose updating the stub for ``str`` in typeshed so that the
|
2022-02-01 14:00:27 -05:00
|
|
|
methods are overloaded with the ``LiteralString``-preserving
|
2022-01-31 22:20:47 -05:00
|
|
|
versions. This means type checkers do not have to hardcode
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` behavior for each method. It also lets us easily
|
2022-01-31 22:20:47 -05:00
|
|
|
support new methods in the future by updating the typeshed stub.
|
|
|
|
|
|
|
|
For example, to preserve literal types for the ``capitalize`` method,
|
|
|
|
we would change the stub as below:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
# before
|
|
|
|
def capitalize(self) -> str: ...
|
|
|
|
|
|
|
|
# after
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def capitalize(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def capitalize(self) -> str: ...
|
|
|
|
|
|
|
|
The downside of changing the ``str`` stub is that the stub becomes
|
|
|
|
more complicated and can make error messages harder to
|
|
|
|
understand. Type checkers may need to special-case ``str`` to make
|
|
|
|
error messages understandable for users.
|
|
|
|
|
2022-02-15 20:08:01 -05:00
|
|
|
Below is an exhaustive list of ``str`` methods which, when called with
|
|
|
|
arguments of type ``LiteralString``, must be treated as returning a
|
|
|
|
``LiteralString``. If this PEP is accepted, we will update these
|
|
|
|
method signatures in typeshed:
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def capitalize(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def capitalize(self) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def casefold(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def casefold(self) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def center(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def center(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
|
|
|
|
|
|
|
|
if sys.version_info >= (3, 8):
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def expandtabs(self: LiteralString, tabsize: SupportsIndex = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def expandtabs(self, tabsize: SupportsIndex = ...) -> str: ...
|
|
|
|
|
|
|
|
else:
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def expandtabs(self: LiteralString, tabsize: int = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def expandtabs(self, tabsize: int = ...) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def format(self, *args: str, **kwargs: str) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def join(self: LiteralString, __iterable: Iterable[LiteralString]) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def join(self, __iterable: Iterable[str]) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def ljust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def ljust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def lower(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def lower(self) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def lstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def lstrip(self, __chars: str | None = ...) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def partition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def partition(self, __sep: str) -> tuple[str, str, str]: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def replace(self: LiteralString, __old: LiteralString, __new: LiteralString, __count: SupportsIndex = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def replace(self, __old: str, __new: str, __count: SupportsIndex = ...) -> str: ...
|
|
|
|
|
|
|
|
if sys.version_info >= (3, 9):
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def removeprefix(self: LiteralString, __prefix: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def removeprefix(self, __prefix: str) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def removesuffix(self: LiteralString, __suffix: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def removesuffix(self, __suffix: str) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def rjust(self: LiteralString, __width: SupportsIndex, __fillchar: LiteralString = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def rjust(self, __width: SupportsIndex, __fillchar: str = ...) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def rpartition(self: LiteralString, __sep: LiteralString) -> tuple[LiteralString, LiteralString, LiteralString]: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def rpartition(self, __sep: str) -> tuple[str, str, str]: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def rsplit(self: LiteralString, sep: LiteralString | None = ..., maxsplit: SupportsIndex = ...) -> list[LiteralString]: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def rsplit(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def rstrip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def rstrip(self, __chars: str | None = ...) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def split(self: LiteralString, sep: LiteralString | None = ..., maxsplit: SupportsIndex = ...) -> list[LiteralString]: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def split(self, sep: str | None = ..., maxsplit: SupportsIndex = ...) -> list[str]: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def splitlines(self: LiteralString, keepends: bool = ...) -> list[LiteralString]: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def splitlines(self, keepends: bool = ...) -> list[str]: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def strip(self: LiteralString, __chars: LiteralString | None = ...) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def strip(self, __chars: str | None = ...) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def swapcase(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def swapcase(self) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def title(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def title(self) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def upper(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def upper(self) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def zfill(self: LiteralString, __width: SupportsIndex) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def zfill(self, __width: SupportsIndex) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __add__(self: LiteralString, __s: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __add__(self, __s: str) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __iter__(self: LiteralString) -> Iterator[str]: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __iter__(self) -> Iterator[str]: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __mod__(self: LiteralString, __x: Union[LiteralString, Tuple[LiteralString, ...]]) -> str: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __mod__(self, __x: Union[str, Tuple[str, ...]]) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __mul__(self: LiteralString, __n: SupportsIndex) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __mul__(self, __n: SupportsIndex) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __repr__(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __repr__(self) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __rmul__(self: LiteralString, n: SupportsIndex) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __rmul__(self, n: SupportsIndex) -> str: ...
|
|
|
|
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def __str__(self: LiteralString) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def __str__(self) -> str: ...
|
|
|
|
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
Appendix D: Guidelines for using ``LiteralString`` in Stubs
|
|
|
|
===========================================================
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
Libraries that do not contain type annotations within their source may
|
|
|
|
specify type stubs in Typeshed. Libraries written in other languages,
|
|
|
|
such as those for machine learning, may also provide Python type
|
|
|
|
stubs. This means the type checker cannot verify that the type
|
|
|
|
annotations match the source code and must trust the type stub. Thus,
|
2022-02-15 20:08:01 -05:00
|
|
|
authors of type stubs need to be careful when using ``LiteralString``,
|
2022-01-31 22:20:47 -05:00
|
|
|
since a function may falsely appear to be safe when it is not.
|
|
|
|
|
2022-02-01 14:00:27 -05:00
|
|
|
We recommend the following guidelines for using ``LiteralString`` in stubs:
|
2022-01-31 22:20:47 -05:00
|
|
|
|
2022-02-02 07:49:27 -05:00
|
|
|
+ If the stub is for a pure function, we recommend using ``LiteralString``
|
2022-01-31 22:20:47 -05:00
|
|
|
in the return type of the function or of its overloads only if all
|
2022-02-15 20:08:01 -05:00
|
|
|
the corresponding parameters have literal types (i.e.,
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` or ``Literal["a", "b"]``).
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
# OK
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def my_transform(x: LiteralString, y: Literal["a", "b"]) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def my_transform(x: str, y: str) -> str: ...
|
|
|
|
|
|
|
|
# Not OK
|
|
|
|
@overload
|
2022-02-01 14:00:27 -05:00
|
|
|
def my_transform(x: LiteralString, y: str) -> LiteralString: ...
|
2022-01-31 22:20:47 -05:00
|
|
|
@overload
|
|
|
|
def my_transform(x: str, y: str) -> str: ...
|
|
|
|
|
|
|
|
+ If the stub is for a ``staticmethod``, we recommend the same
|
|
|
|
guideline as above.
|
|
|
|
|
|
|
|
+ If the stub is for any other kind of method, we recommend against
|
2022-02-01 14:00:27 -05:00
|
|
|
using ``LiteralString`` in the return type of the method or any of
|
2022-02-15 20:08:01 -05:00
|
|
|
its overloads. This is because, even if all the explicit parameters
|
2022-02-01 14:00:27 -05:00
|
|
|
have type ``LiteralString``, the object itself may be created using
|
2022-01-31 22:20:47 -05:00
|
|
|
user data and thus the return type may be user-controlled.
|
|
|
|
|
|
|
|
+ If the stub is for a class attribute or global variable, we also
|
2022-02-01 14:00:27 -05:00
|
|
|
recommend against using ``LiteralString`` because the untyped code
|
2022-01-31 22:20:47 -05:00
|
|
|
may write arbitrary values to the attribute.
|
|
|
|
|
|
|
|
However, we leave the final call to the library author. They may use
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` if they feel confident that the string returned by
|
2022-01-31 22:20:47 -05:00
|
|
|
the method or function or the string stored in the attribute is
|
|
|
|
guaranteed to have a literal type - i.e., the string is created by
|
|
|
|
applying only literal-preserving ``str`` operations to a string
|
|
|
|
literal.
|
|
|
|
|
|
|
|
Note that these guidelines do not apply to inline type annotations
|
|
|
|
since the type checker can verify that, say, a method returning
|
2022-02-01 14:00:27 -05:00
|
|
|
``LiteralString`` does in fact return an expression of that type.
|
2022-01-31 22:20:47 -05:00
|
|
|
|
|
|
|
|
2021-12-01 12:57:41 -05:00
|
|
|
Resources
|
|
|
|
=========
|
|
|
|
|
|
|
|
Literal String Types in Scala
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
Scala `uses
|
|
|
|
<https://www.scala-lang.org/api/2.13.x/scala/Singleton.html>`_
|
|
|
|
``Singleton`` as the supertype for singleton types, which includes
|
2022-02-01 14:00:27 -05:00
|
|
|
literal string types, such as ``"foo"``. ``Singleton`` is Scala's
|
|
|
|
generalized analogue of this PEP's ``LiteralString``.
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Tamer Abdulradi showed how Scala's literal string types can be used
|
|
|
|
for "Preventing SQL injection at compile time", Scala Days talk
|
|
|
|
`Literal types: What are they good for?
|
|
|
|
<https://slideslive.com/38907881/literal-types-what-they-are-good-for>`_
|
|
|
|
(slides 52 to 68).
|
|
|
|
|
|
|
|
Thanks
|
|
|
|
------
|
|
|
|
|
|
|
|
Thanks to the following people for their feedback on the PEP:
|
|
|
|
|
2022-01-31 22:20:47 -05:00
|
|
|
Edward Qiu, Jia Chen, Shannon Zhu, Gregory P. Smith, Никита Соболев,
|
2022-02-02 16:43:20 -05:00
|
|
|
CAM Gerlach, Arie Bovenberg, David Foster, and Shengye Wan
|
2021-12-01 12:57:41 -05:00
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document is placed in the public domain or under the
|
|
|
|
CC0-1.0-Universal license, whichever is more permissive.
|