Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ For example::
.. versionadded:: 3.10

The usual dictionary methods are available for :class:`Counter` objects
except for two which work differently for counters.
except for these two which work differently for counters:

.. method:: fromkeys(iterable)

Expand Down
8 changes: 6 additions & 2 deletions Lib/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,21 @@ def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.OutputString())

def js_output(self, attrs=None):
import base64
# Print javascript
output_string = self.OutputString(attrs)
if _has_control_character(output_string):
raise CookieError("Control characters are not allowed in cookies")
# Base64-encode value to avoid template
# injection in cookie values.
output_encoded = base64.b64encode(output_string.encode('utf-8')).decode("ascii")
return """
<script type="text/javascript">
<!-- begin hiding
document.cookie = \"%s\";
document.cookie = atob(\"%s\");
// end hiding -->
</script>
""" % (output_string.replace('"', r'\"'))
""" % (output_encoded,)

def OutputString(self, attrs=None):
# Build up our result
Expand Down
29 changes: 18 additions & 11 deletions Lib/test/test_http_cookies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Simple test suite for http/cookies.py

import base64
import copy
import unittest
import doctest
Expand Down Expand Up @@ -175,17 +175,19 @@ def test_load(self):

self.assertEqual(C.output(['path']),
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
self.assertEqual(C.js_output(), r"""
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme; Version=1').decode('ascii')
self.assertEqual(C.js_output(), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
document.cookie = atob("{cookie_encoded}");
// end hiding -->
</script>
""")
self.assertEqual(C.js_output(['path']), r"""
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme').decode('ascii')
self.assertEqual(C.js_output(['path']), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
document.cookie = atob("{cookie_encoded}");
// end hiding -->
</script>
""")
Expand Down Expand Up @@ -290,17 +292,19 @@ def test_quoted_meta(self):

self.assertEqual(C.output(['path']),
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
self.assertEqual(C.js_output(), r"""
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1').decode('ascii')
self.assertEqual(C.js_output(), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
document.cookie = atob("{expected_encoded_cookie}");
// end hiding -->
</script>
""")
self.assertEqual(C.js_output(['path']), r"""
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme').decode('ascii')
self.assertEqual(C.js_output(['path']), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
document.cookie = atob("{expected_encoded_cookie}");
// end hiding -->
</script>
""")
Expand Down Expand Up @@ -391,13 +395,16 @@ def test_setter(self):
self.assertEqual(
M.output(),
"Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i))
expected_encoded_cookie = base64.b64encode(
("%s=%s; Path=/foo" % (i, "%s_coded_val" % i)).encode("ascii")
).decode('ascii')
expected_js_output = """
<script type="text/javascript">
<!-- begin hiding
document.cookie = "%s=%s; Path=/foo";
document.cookie = atob("%s");
// end hiding -->
</script>
""" % (i, "%s_coded_val" % i)
""" % (expected_encoded_cookie,)
self.assertEqual(M.js_output(), expected_js_output)
for i in ["foo bar", "foo@bar"]:
# Try some illegal characters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a race in :c:type:`!_PyRawMutex` on the free-threaded build where a
``Py_PARK_INTR`` return from ``_PySemaphore_Wait`` could let the waiter
destroy its semaphore before the unlocking thread's
``_PySemaphore_Wakeup`` completed, causing a fatal ``ReleaseSemaphore``
error.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Base64-encode values when embedding cookies to JavaScript using the
:meth:`http.cookies.BaseCookie.js_output` method to avoid injection
and escaping.
11 changes: 10 additions & 1 deletion Python/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,16 @@ _PyRawMutex_LockSlow(_PyRawMutex *m)

// Wait for us to be woken up. Note that we still have to lock the
// mutex ourselves: it is NOT handed off to us.
_PySemaphore_Wait(&waiter.sema, -1);
//
// Loop until we observe an actual wakeup. A return of Py_PARK_INTR
// could otherwise let us exit _PySemaphore_Wait and destroy
// `waiter.sema` while _PyRawMutex_UnlockSlow's matching
// _PySemaphore_Wakeup is still pending, since the unlocker has
// already CAS-removed us from the waiter list without any handshake.
int res;
do {
res = _PySemaphore_Wait(&waiter.sema, -1);
} while (res != Py_PARK_OK);
}

_PySemaphore_Destroy(&waiter.sema);
Expand Down
12 changes: 8 additions & 4 deletions Python/parking_lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ _PySemaphore_Init(_PySemaphore *sema)
NULL // unnamed
);
if (!sema->platform_sem) {
Py_FatalError("parking_lot: CreateSemaphore failed");
_Py_FatalErrorFormat(__func__,
"parking_lot: CreateSemaphore failed (error: %u)",
GetLastError());
}
#elif defined(_Py_USE_SEMAPHORES)
if (sem_init(&sema->platform_sem, /*pshared=*/0, /*value=*/0) < 0) {
Expand Down Expand Up @@ -141,8 +143,8 @@ _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout)
}
else {
_Py_FatalErrorFormat(__func__,
"unexpected error from semaphore: %u (error: %u)",
wait, GetLastError());
"unexpected error from semaphore: %u (error: %u, handle: %p)",
wait, GetLastError(), sema->platform_sem);
}
#elif defined(_Py_USE_SEMAPHORES)
int err;
Expand Down Expand Up @@ -230,7 +232,9 @@ _PySemaphore_Wakeup(_PySemaphore *sema)
{
#if defined(MS_WINDOWS)
if (!ReleaseSemaphore(sema->platform_sem, 1, NULL)) {
Py_FatalError("parking_lot: ReleaseSemaphore failed");
_Py_FatalErrorFormat(__func__,
"parking_lot: ReleaseSemaphore failed (error: %u, handle: %p)",
GetLastError(), sema->platform_sem);
}
#elif defined(_Py_USE_SEMAPHORES)
int err = sem_post(&sema->platform_sem);
Expand Down
7 changes: 3 additions & 4 deletions Tools/pixi-packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ Each package definition is contained in a subdirectory, but they share the build

- More package variants (such as UBSan)
- Support for Windows
- Using a single `pixi.toml` and `recipe.yaml` for all package variants is blocked on
[pixi#5364](https://github.com/prefix-dev/pixi/pull/5364)
and [pixi#5248](https://github.com/prefix-dev/pixi/issues/5248)
- Using a single `pixi.toml` for all package variants is blocked on
[pixi#5248](https://github.com/prefix-dev/pixi/issues/5248)

## Troubleshooting

Expand All @@ -48,7 +47,7 @@ FATAL: ThreadSanitizer: unexpected memory mapping 0x7977bd072000-0x7977bd500000
```
To fix it, try reducing `mmap_rnd_bits`:

```bash
```console
$ sudo sysctl vm.mmap_rnd_bits
vm.mmap_rnd_bits = 32 # too high for TSan
$ sudo sysctl vm.mmap_rnd_bits=28 # reduce it
Expand Down
4 changes: 4 additions & 0 deletions Tools/pixi-packages/asan/pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
channels = ["https://prefix.dev/conda-forge"]
platforms = ["linux-64", "linux-aarch64", "osx-64", "osx-arm64"]
preview = ["pixi-build"]
requires-pixi = ">=0.66.0"

[package.build.backend]
name = "pixi-build-rattler-build"
version = "*"

[package.build.config]
recipe = "../default/recipe.yaml"
94 changes: 0 additions & 94 deletions Tools/pixi-packages/asan/recipe.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion Tools/pixi-packages/clone-recipe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ set -o errexit
cd "$(dirname "$0")"

for variant in asan freethreading tsan-freethreading; do
cp -av default/recipe.yaml default/pixi.toml ${variant}/
cp -av default/pixi.toml ${variant}/
done
4 changes: 4 additions & 0 deletions Tools/pixi-packages/default/pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
channels = ["https://prefix.dev/conda-forge"]
platforms = ["linux-64", "linux-aarch64", "osx-64", "osx-arm64"]
preview = ["pixi-build"]
requires-pixi = ">=0.66.0"

[package.build.backend]
name = "pixi-build-rattler-build"
version = "*"

[package.build.config]
recipe = "../default/recipe.yaml"
4 changes: 4 additions & 0 deletions Tools/pixi-packages/freethreading/pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
channels = ["https://prefix.dev/conda-forge"]
platforms = ["linux-64", "linux-aarch64", "osx-64", "osx-arm64"]
preview = ["pixi-build"]
requires-pixi = ">=0.66.0"

[package.build.backend]
name = "pixi-build-rattler-build"
version = "*"

[package.build.config]
recipe = "../default/recipe.yaml"
Loading
Loading