Skip to content

fix: do not mutate httpx client inside storage, postgrest and functions - #1249

Merged
o-santi merged 9 commits into
mainfrom
fix-url-mutation
Oct 8, 2025
Merged

o-santi merged 9 commits into
mainfrom
fix-url-mutation

Conversation

@o-santi

@o-santi o-santi commented Oct 7, 2025 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Fix both storage and postgrest to not mutate external httpx client, given it is shared, by instead keeping their own copy of their parameters to pass into the request call. In order to properly manipulate URLs without mutation, I've decided to add the yarl dependency, which returns a new object upon path manipulation.

For the postgrest library, I've also decided to factor out all of the parameters that go into a request in a RequestConfig object, for it is very easy to miss a spot otherwise. I've had to change the tests to account for this. However, I think this greatly simplifies most of the libraries, as 99% of the code does not need to be async/sync, and can be stated only once in a further rewrite.

Additional context

Should fix #1245.

instead, use `yarl.URL` to parse and manipulate the URL in a stateless
way, by returning a new url when paths are appended
@o-santi
o-santi requested a review from grdsdev October 7, 2025 16:29
@o-santi

o-santi commented Oct 7, 2025

Copy link
Copy Markdown
Contributor Author

I do think letting the httpx client be passed in was a mistake in general, for it adds way too much ambiguity on how parameters are merged. For instance, base_url should be entirely ignored if set in httpx, while the other parameters should be additive in general.

@coveralls

coveralls commented Oct 7, 2025 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 18354726720

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 69 unchanged lines in 11 files lost coverage.
  • Overall coverage increased (+0.05%) to 93.928%

Files with Coverage Reduction New Missed Lines %
src/postgrest/base_client.py 1 92.0%
src/supabase_functions/_sync/functions_client.py 2 97.06%
src/supabase_functions/_async/functions_client.py 2 97.06%
src/postgrest/utils.py 2 93.75%
src/postgrest/_sync/client.py 3 93.33%
src/postgrest/_async/client.py 3 93.33%
src/storage3/_sync/file_api.py 6 96.09%
src/storage3/_async/file_api.py 6 96.09%
src/postgrest/_sync/request_builder.py 9 92.44%
src/postgrest/_async/request_builder.py 9 92.44%
Totals Coverage Status
Change from base Build 18285122346: 0.05%
Covered Lines: 8786
Relevant Lines: 9354

💛 - Coveralls

@silentworks silentworks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will pull this code down and test against my local codebase.

@silentworks

Copy link
Copy Markdown
Contributor

I do think letting the httpx client be passed in was a mistake in general, for it adds way too much ambiguity on how parameters are merged. For instance, base_url should be entirely ignored if set in httpx, while the other parameters should be additive in general.

It was either this or passing every parameter that httpx expects as a config parameter which felt like duplication work. Let's discuss in a new issue if you have another approach you think would work better.

@o-santi

o-santi commented Oct 7, 2025 •

Copy link
Copy Markdown
Contributor Author

I will pull this code down and test against my local codebase.

Thank you, please let me know of the results.

It was either this or passing every parameter that httpx expects as a config parameter which felt like duplication work.

The problem is that it is still happening, headers and base_url are such examples of arguments that can be set on both, and are ambiguous as to what setting both means. Furthermore, RequestConfig is basically a configuration class for httpx, which we might decide to just use directly instead. Setting things on it is much easier as we can assign immutable semantics, instead of httpx's mutable API, which is horrible for sharing configuration. I believe the only way forward is to expand something like RequestConfig into all libraries, and pass it around with immutable semantics, ditching the httpx client argument.

@o-santi

o-santi commented Oct 7, 2025

Copy link
Copy Markdown
Contributor Author

Just noticed that this problem is not only on these two libraries, functions also has it. realtime does not accept an httpx client, while auth accepts it but does store its own url and headers.

@o-santi o-santi changed the title fix: do not mutate httpx client inside storage and postgrest fix: do not mutate httpx client inside storage, postgrest and functions Oct 7, 2025
@silentworks

Copy link
Copy Markdown
Contributor

@o-santi I'm getting an error while using these changes in my test setup locally. The error is below.

Request URL is missing an 'http://' or 'https://' protocol.

@silentworks silentworks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Async/SyncBucketActionsMixin class is missing the base_url and only has the path as the URL hence the error above. This needs to be updated to match the Async/SyncStorageBucketAPI use of base_url.

@silentworks silentworks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and is working now.

@o-santi
o-santi merged commit 0543b91 into main Oct 8, 2025
@o-santi
o-santi deleted the fix-url-mutation branch October 8, 2025 19:08
@o-santi o-santi mentioned this pull request Oct 8, 2025
zeekay pushed a commit to Dream-AI-4444/supabase-py that referenced this pull request Sep 10, 2026
spydon added a commit that referenced this pull request Nov 5, 2026
Fixes #1984. Alternative to #1985.

## Problem

`relative_path_to_parts()` parsed every object key with `yarl.URL`. A
key is not a URL, so parts of it were read as URL syntax and dropped:

| Key                              | Sent as         |
| -------------------------------- | --------------- |
| `backup-2024-01-01T10:00:00.sql` | `00:00.sql`     |
| `user:42/avatar.png`             | `42/avatar.png` |
| `what?.txt`                      | `what`          |
| `report#1.pdf`                   | `report`        |
| `//folder/file.png`              | `file.png`      |

Every method that takes a path used the wrong key, so different keys
could overwrite each other and downloads could return another object's
bytes. This came in with #1249. Before that, the key was formatted into
the request path as a plain string.

## Fix

Split the key on `/` after stripping leading slashes, and percent-decode
each part as before. `joinpath()` already encodes each part when the
request URL is built, so `:` is kept and `?` and `#` are sent as `%3F`
and `%23`.

Percent-decoding is kept so that a pre-encoded key such as `a%20b.png`
still resolves to `a b.png`, as it did on earlier v2 releases. The only
input that behaves differently is a full URL passed as a key, such as
`https://host/a/b`, which used to be reduced to `a/b`.

## Tests

`tests/test_object_paths.py` checks the request path sent by `upload()`
and the URL returned by `get_public_url()` for both clients. Against the
old function, 15 of 33 cases fail.

From `src/storage`:

- `uv run --frozen --package storage3 pytest -o addopts=""
tests/test_object_paths.py tests/test_upload_headers.py
tests/test_utils.py tests/test_client.py`: 119 passed.
- `uv run --frozen --package storage3 --group mypy mypy src/storage3
tests`: no issues.
- `ruff format --check` and `ruff check`: passed.

The infra-backed storage suite was not run locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants