Commit Graph

17811 Commits

Author SHA1 Message Date
Anders Kaseorg 1eee06e961 ruff: Fix E226 Missing whitespace around arithmetic operator.
This is a preview rule, not yet enabled by default.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 82a9fd927b)
2024-03-07 15:07:12 -08:00
Anders Kaseorg e546bc5d26 ruff: Fix PLW0108 Lambda may be unnecessary.
This is a preview rule, not yet enabled by default.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit d748ec8d52)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 9eaec853d1 ruff: Fix RUF027 Possible f-string without an `f` prefix.
This is a preview rule, not yet enabled by default.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 553f268b04)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 13a037a799 ruff: Fix RUF021 Parenthesize when chaining `and` and `or` together.
This is a preview rule, not yet enabled by default.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 865febb307)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 9ea290d5ae python: Reformat with Ruff formatter.
https://docs.astral.sh/ruff/formatter/

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 570f3dd447)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 230574ebbf ruff: Fix SIM113 Use `enumerate()` for index variable in `for` loop.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 53e80c41ea)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 3f9e4bd54b ruff: Fix SIM910 Use `d.get(…)` instead of `d.get(…, None)`.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit f165ba0fb3)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 1fa8862e48 ruff: Fix RUF019 Unnecessary key check before dictionary access.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 712917b2c9)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 3bf1934598 openapi: Validate real requests and responses, not fictional mocks.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 029e765e20)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 9f2172c0f9 openapi: Get parameters from requestBody too.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 5cac872e4b)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 899819fb2f openapi: Use Parameter class for generating curl examples.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit a67d1b57b9)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 33e77b6d15 openapi: Represent OpenAPI parameters with a Parameter class.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 131b230e2b)
2024-03-07 15:07:12 -08:00
Anders Kaseorg fc8e023da2 openapi: Remove confusing check for 200 responses.
This error message didn’t make sense for the check as written, and our
OpenAPI document already provides the expected format for our 200
responses.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 0cee3bee00)
2024-03-07 15:07:12 -08:00
Anders Kaseorg b5a1a9af25 test_openapi: Provide missing defs_mapping argument.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit bb699f7ee3)
2024-03-07 15:07:12 -08:00
Anders Kaseorg cf034cf19f openapi: Loosen anchor parameter specification to string.
Real requests would not validate against the previous version.  There
seems to be no consistent way to determine whether a string parameter
should be coerced to an integer for validation against an allOf
schema (which works at the level of JSON objects, not strings).

See also https://github.com/python-openapi/openapi-core/issues/698.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 0514f92bdb)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 9fd5e9af4a test_classes: Add Content-Type header to empty DELETE/POST bodies.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 0dd92d2116)
2024-03-07 15:07:12 -08:00
Anders Kaseorg f03cc51aca test_classes: Default client_post to application/x-www-form-urlencoded.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit a356ec7011)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 09706f820d requirements: Upgrade to openapi-core pre-release.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit d9f2f23c6a)
2024-03-07 15:07:12 -08:00
Anders Kaseorg 0c67227f88 Revert "zproject: Prevent having exactly 17/18 middlewares, for Python 3.11 bug."
This reverts commit cf0b803d50.

(cherry picked from commit 41f253774d)
2024-03-07 15:07:12 -08:00
Mateusz Mandera 726812c80b context_managers: Open file in write mode in lockfile_nonblocking.
Otherwise this fails if the file doesn't yet exist.

(cherry picked from commit dd6516d157)
2024-03-07 15:01:15 -08:00
Mateusz Mandera 96001b19fc update_analytics_count: Use a correct lock mechanism.
Adds a re-usable lockfile_nonblocking helper to context_managers.

Relying on naive `os.mkdir` is not enough especially now that the
successful operation of this command is necessary for push notifications
to work for many servers.

We can't use `lockfile` context manager from
`zerver.lib.context_managers`, because we want the custom behavior of
failing if the lock can't be acquired, instead of waiting.
That's because if an instance of this gets stuck, we don't want to start
queueing up more processes waiting forever whenever the cronjob runs
again and fail->exit is preferrable instead.

(cherry picked from commit f61ed58c8f)
2024-03-07 15:01:15 -08:00
Mateusz Mandera 5cecbcdfb3 config_error: Support passing arguments specifying the "go back" link.
Depending on the kind of config error being shown, different "go back"
links may be more appropriate.
We probably hard-coded /login/ for it, because these config errors are
most commonly used for authentication backend config error, where it
makes sense to have /login/ as "go back", because the user most likely
indeed got there from the login page.

However, for remote_billing_bouncer_not_configured, it doesn't make
sense, because the user almost surely is already logged in and got there
by clicking "Plan management" inside the gear menu in the logged in app.

(cherry picked from commit fcc3d88daf)
2024-03-07 15:01:15 -08:00
Mateusz Mandera 7d0917c3c7 remote_billing: Tweak /self-hosted-billing/ endpoints access model.
It's best for these to just be consistent. Therefore:
1. The .../not-configured/ error page endpoint should be restricted to
   .has_billing_access users only.
2. For consistency, self_hosting_auth_view_common is tweaked to also do
   the .has_billing_access check as the first thing, to avoid revealing
   configuration information via its redirect/error-handling behavior.

The revealed configuration information seems super harmless, but it's
simpler to not have to worry about it and just be consistent.

(cherry picked from commit 7e8c645280)
2024-03-07 15:01:15 -08:00
Mateusz Mandera 836dc9f1c9 test_helpers: Remove /self-hosted-billing/ from exempt_patterns.
This was added early on and hasn't been removed since, even though we
have tests for the endpoint for a while now.

(cherry picked from commit a7f187bec3)
2024-03-07 15:01:15 -08:00
Mateusz Mandera b67e3d5ffa remote_billing: Make "plan management" always available.
Just shows a config error page if the bouncer is not enabled. Uses a new
endpoint for this so that it can work nicely for both browser and
desktop app clients.
It's necessary, because the desktop app expects to get a json response
with either an error or billing_access_url to redirect to. Showing a
nice config error page can't be done via the json error mechanism, so
instead we just serve a redirect to the new error page, which the app
will open in the browser in a new window or tab.

(cherry picked from commit f6d526f798)
2024-03-07 15:01:15 -08:00
Alex Vandiver 6a6e59f4d6 message: Merge unnecessary cache_transformer step.
Having a non-identity `cache_transformer` is no different from running
it on every row of the query_function.  Simplify understanding of the
codepath used in caching by merging the pieces of code.

(cherry picked from commit 547c8f895d)
2024-03-07 09:48:20 -08:00
Alex Vandiver 8b23b81f56 message_cache: Rename "to_dict" functions which deal with bytes.
(cherry picked from commit 548bb5362e)
2024-03-07 09:48:20 -08:00
Alex Vandiver 73116023a6 message_cache: Rename update_to_dict_cache to update_message_cache.
This better describes what it does.

(cherry picked from commit 96119e45b9)
2024-03-07 09:48:20 -08:00
Alex Vandiver e15a175a42 message: Split MessageDict and friends into its own file.
(cherry picked from commit 93a071a1f8)
2024-03-07 09:48:20 -08:00
Alex Vandiver 4c1b1ae2f6 message: Move render_markdown into zerver.lib.markdown.
(cherry picked from commit 11bde84580)
2024-03-07 09:48:20 -08:00
Alex Vandiver 3e2b295140 message_edit: Carry the QuerySet through as much as possible.
Rather than pass around a list of message objects in-memory, we
instead keep the same constructed QuerySet which includes the later
propagated messages (if any), and use that same query to pick out
affected Attachment objects, rather than limiting to the set of ids.
This is not necessarily a win -- the list of message-ids *may* be very
long, and thus the query may be more concise, easier to send to
PostgreSQL, and faster for PostgreSQL to parse.  However, the list of
ids is almost certainly better-indexed.

After processing the move, the QuerySet must be re-defined as a search
of ids (and possibly a very long list of such), since there is no
other way which is guaranteed to correctly single out the moved
messages.  At this point, it is mostly equivalent to the list of
Message objects, and certainly takes no less memory.

(cherry picked from commit eaf58438ec)
2024-03-07 09:48:20 -08:00
Alex Vandiver 210c9aaf1c topic: Use a single SQL statement to propagate message moves.
Rather than use `bulk_update()` to batch-move chunks of messages, use
a single SQL query to move the messages.  This is much more efficient
for large topic moves.  Since the `edit_history` field is not yet
JSON (see #26496) this requires that PostgreSQL cast the current data
into `jsonb`, append the new data (also cast to `jsonb`), and then
re-cast that as text.

For single-message moves, this _increases_ the SQL query count by one,
since we have to re-query for the updated data from the database after
the bulk update.  However, this is overall still a performance
improvement, which improves to 2x or 3x for larger topic moves.  Below
is a table of duration in seconds to run `do_update_message` to move a
topic to a new stream, based on messages in the topic, for before and
after this change:

| Topic size |  Before  |  After  |
| ---------- | -------- | ------- |
| 1          |   0.1036 |  0.0868 |
| 2          |   0.1108 |  0.0925 |
| 5          |   0.1139 |  0.0959 |
| 10         |   0.1218 |  0.0972 |
| 20         |   0.1310 |  0.1098 |
| 50         |   0.1759 |  0.1366 |
| 100        |   0.2307 |  0.1662 |
| 200        |   0.3880 |  0.2229 |
| 500        |   0.7676 |  0.4052 |
| 1000       |   1.3990 |  0.6848 |
| 2000       |   2.9706 |  1.3370 |
| 5000       |   7.5218 |  3.2882 |
| 10000      |  14.0272 |  5.4434 |

(cherry picked from commit a2657b843c)
2024-03-07 09:48:20 -08:00
Alex Vandiver 9a2a5b5910 message: Add a bulk_access_stream_messages_query method.
This applies access restrictions in SQL, so that individual messages
do not need to be walked one-by-one.  It only functions for stream
messages.

Use of this method significantly speeds up checks if we moved "all
visible messages" in a topic, since we no longer need to walk every
remaining message in the old topic to determine that at least one was
visible to the user.  Similarly, it significantly speeds up merging
into existing topics, since it no longer must walk every message in
the new topic to determine if the user could see at least one.

Finally, it unlocks the ability to bulk-update only messages the user
has access to, in a single query (see subsequent commit).

(cherry picked from commit 7dcc7540f9)
2024-03-07 09:48:20 -08:00
Alex Vandiver 9ac6ca1545 streams: Remove a lie from the docstring.
(cherry picked from commit c118f1874e)
2024-03-07 09:48:20 -08:00
Alex Vandiver a25ee8aad0 message_edit: len(changed_messages) is always > 0.
It is only ever added to, and starts as a one-element list.

(cherry picked from commit db3bcb8889)
2024-03-07 09:48:20 -08:00
Tim Abbott eeedc34e58 message_edit: Ensure gaining_usermessage_user_ids is set.
This makes this code path a bit easier to follow.

(cherry picked from commit 68be070306)
2024-03-07 09:48:20 -08:00
Tim Abbott e928fce232 message_edit: Clarify unmodified_user_messages logic.
(cherry picked from commit fc679b09a3)
2024-03-07 09:48:20 -08:00
Alex Vandiver fd1db72276 tests: Reserve "Internal" client, used by email gateway and topic moves.
(cherry picked from commit 13b9c87f93)
2024-03-07 09:48:20 -08:00
Alex Vandiver aee36b2b49 tests: Clear in-memory Client caches before testing query counts.
This makes counts more apples-to-apples comparable when run
back-to-back.

(cherry picked from commit a84de411a9)
2024-03-07 09:48:20 -08:00
Alex Vandiver 3c8796a2ad export_search: Support exporting DMs or huddles.
(cherry picked from commit 6b8365024c)
2024-03-01 09:26:25 -08:00
Lauryn Menard 85bd8dd33d push-bouncer: Exclude LoggingCountStats with partial data.
LoggingCountStats with a daily duration and that are directly stored
on the RealmCount table (not via aggregation in process_count_stat),
can be in a state, after the hourly cron job to update analytics
counts, where the logged value will be live-updated later, because
the end time for the stat is still in the future.

As these logging counts are designed to be used on the self-hosted
installation for either debugging or rate limiting, sending these
partial/incomplete counts to the bouncer has low value.

(cherry picked from commit 40daf10a74)
2024-03-01 09:26:25 -08:00
Anders Kaseorg 2e4785f51c requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 93198a19ed)
2024-02-16 12:17:34 -08:00
Anders Kaseorg 893c10f869 ruff: Fix DJ012 Order of model's inner classes, methods, and fields.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit e56863fa85)
2024-02-16 12:17:34 -08:00
Anders Kaseorg 7b604d3cde ruff: Fix SIM300 Yoda conditions are discouraged.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit ab25ebd5ce)
2024-02-16 12:17:34 -08:00
Mateusz Mandera d977dfe6e9 push_notifs: Gracefully handle exception when server cant push.
The problem was that earlier this was just an uncaught JsonableError,
leading to a full traceback getting spammed to the admins.
The prior commit introduced a clear .code for this error on the bouncer
side, meaning the self-hosted server can now detect that and handle it
nicely, by just logging.error about it and also take the opportunity to
adjust the realm.push_notifications_... flags.

(cherry picked from commit e8018a7285)
2024-02-16 11:26:05 -08:00
Mateusz Mandera 0a1905ea8d zilencer: Improve json error when plan doesn't allow push notifs.
This allows the self-hosted server to explicitly test for that exception
and catch and log it nicely.

(cherry picked from commit 5b03932d5c)
2024-02-16 11:26:05 -08:00
Sahil Batra 0df7bd71f3 CVE-2024-21630: Check permission to subscribe others using invite link.
This commit updates the API to check the permission to subscribe other
users while creating multi-use invites. The API will raise error if
the user passes the "stream_ids" parameter (even when it contains only
default streams) and the calling user does not have permission to
subscribe others to streams.

We did not add this before as we only allowed admins to create
multiuse invites, but now we have added a setting which can be used
to allow users with other roles as well to create multiuse invites.
2024-01-24 16:46:02 -08:00
Alex Vandiver fcef73fbbf import: Rewrite "delivered_message" column of scheduled messages.
This also requires shuffling the message import to before the
scheduled messages.

Fixes: #28690.
(cherry picked from commit c974b421e3)
2024-01-24 16:38:46 -08:00
Alex Vandiver eac2fe6b81 import: Merge duplicate slack email addresses.
It is possible to have multiple users with the same email address --
for instance, when two users are guests in shared channels via two
different other Slack instances.

Combine those Slack user-ids into one Zulip user, by their user-id;
otherwise, we run into problems during import due to duplicate keys.

(cherry picked from commit 937e41bac8)
2024-01-22 18:19:09 -08:00
Alex Vandiver 4d465cb698 import: Show slack user-ids.
(cherry picked from commit e41810c0b3)
2024-01-22 18:19:09 -08:00