Commit Graph

15149 Commits

Author SHA1 Message Date
Mateusz Mandera
43606b6e39 do_deactivate_user: Use .on_commit around send_event calls.
The previous commit did this for revoking sessions. send_events should
be handled similarly too, to correctly handle calling do_deactivate_user
inside a transaction.

(cherry picked from commit 470c0458e6)
2022-11-14 16:06:25 -08:00
Mateusz Mandera
a3d9297c37 do_deactivate_user: Revoke sessions in transaction.on_commit().
Fixes #21709.

(cherry picked from commit a94b2572be)
2022-11-14 16:06:25 -08:00
Anders Kaseorg
7f6bd415f0 timeout: Correct thread id type passed to PyThreadState_SetAsyncExc.
This type changed in Python 3.7:
https://github.com/python/cpython/pull/781

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 89e4233b68)
2022-11-14 16:05:05 -08:00
Anders Kaseorg
c61799aef9 test_message_flags: Fix “accessibility” spelling.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 019c9527c1)
2022-11-14 17:11:39 -05:00
Mateusz Mandera
897476d4fb middleware: Log 5xx json_errors in JsonErrorHandler.
django.request logs responses with 5xx response codes (our configuration
of the logger prevents it from logging 4xx as well which it normally
does too). However, it does it without the traceback which results in
quite unhelpful log message that look like
"Bad Gateway:/api/v1/users/me/apns_device_token" - particularly
confusing when sent via email to server admins.

The solution here is to do the logging ourselves, using Django's
log_response() (which is meant for this purpose), and including the
traceback. Django tracks (via response._has_been_logged attribute) that
the response has already been logged, and knows to not duplicate that
action. See log_response() in django's codebase for these details.

Fixes #19596.
2022-10-17 17:45:44 -04:00
Mateusz Mandera
cdd01b9f8e send_analytics_to_remote_server: Log connection errors with traceback.
It seems helpful for this to get logged with the traceback rather than
just the general
"<exception name>  while trying to connect to push notification bouncer."
2022-10-17 17:45:44 -04:00
Anders Kaseorg
55c0a15f1c middleware: Pass unhandled API exceptions through to the test suite.
This results in more useful stack traces in failing tests.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-10-17 17:45:44 -04:00
Anders Kaseorg
b12a5be4a0 CVE-2022-36048: Rewrite only specific local links to relative.
Due to mismatches between the URL parsers in Python and browsers, it
was possible to hoodwink rewrite_local_links_to_relative into
generating links that browsers would interpret as absolute.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-08-24 13:07:42 -07:00
Anders Kaseorg
09fb71f95a requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 74f7b6e4fd)
2022-08-23 15:25:01 -07:00
Anders Kaseorg
64cbd4e7c7 retention: Inline move_rows query arguments.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit b4b8691239)
2022-08-23 15:25:01 -07:00
Anders Kaseorg
170d805d9f upload: Replace boto3.Session with boto3.session.Session.
boto3-stubs seems to have dropped the former for some reason.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 2508b579a6)
2022-08-23 15:25:01 -07:00
Anders Kaseorg
540060d389 settings: Add two_factor.plugins.phonenumber to INSTALLED_APPS.
I missed this in commit feff1d0411
(#22383) for upgrading to django-two-factor-auth 1.14.0.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 2439914a50)
2022-08-22 22:41:49 -07:00
Anders Kaseorg
90c45bd230 requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit feff1d0411)
2022-08-22 22:41:49 -07:00
Anders Kaseorg
1ae9922a86 test_tornado: Avoid deprecated AsyncHTTPTestCase.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 6c79b8f2f1)
2022-08-22 22:41:49 -07:00
Anders Kaseorg
4a618ed973 db: Use cursor_factory psycopg2 option.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit b4cf9ad777)
2022-08-22 22:41:49 -07:00
Anders Kaseorg
6f08dbe11b decorator: Check Tornado secret with constant-time comparison.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-08-22 22:23:24 -07:00
Anders Kaseorg
84064e82c7 zilencer: Check remote server API keys with constant-time comparison.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-08-22 22:22:19 -07:00
Anders Kaseorg
c9f6830ba6 CVE-2022-31168: Fix authorization check for changing bot roles.
Due to an incorrect authorization check in Zulip Server 5.4 and
earlier, a member of an organization could craft an API call that
grants organization administrator privileges to one of their bots.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-07-21 20:09:02 -04:00
Mateusz Mandera
305c13faeb test_import_export: Test exclusion of private uploads from realm export. 2022-07-12 04:36:16 +00:00
Anders Kaseorg
c6a5903280 CVE-2022-31134: Exclude private attachments from realm exports.
Zulip Server 2.1.0 and above have a UI tool, accessible only to server
owners and server administrators, which provides a way to download a
“public data” export. While this export tool is only accessible to
administrators, in many configurations server administrators are not
expected to have access to private messages and private
streams. However, the “public data” export which administrators could
generate contained the attachment contents for all attachments, even
those from private messages and streams.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-07-12 04:36:14 +00:00
Anders Kaseorg
82adae451e email_notifications: Give absolute path to html2text.
Our uWSGI configuration doesn’t correctly activate our virtualenv.  We
should investigate that, but until we do, we need to invoke html2text
by an absolute path.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 7c992422f0)
2022-07-07 17:08:22 -07:00
Anders Kaseorg
3f0919cc65 markdown: Parse included blocks in a new parser state.
This fixes inclusion of a multi-paragraph file into a list item.

Followup to commit dc33a0ae67 (#22315).

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 869fe60689)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
e61ffc5bd7 integrations: Satisfy Python-Markdown’s archaic 4-space requirement.
Followup to commit dc33a0ae67 (#22315).

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit a2e1d61172)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
a303c27a16 tornado: Send request_started signal in Django thread.
Django’s ASGIHandler does this too and it seems like a good idea.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit a7e10ee47e)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
6b3399d7e6 markdown: Rewrite include plugin without markdown-include.
markdown-include is GPL licensed.

Also, rewrite it as a block processor, so that it works correctly
inside indented blocks.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit dc33a0ae67)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
ad692da6aa markdown: Rename preprocessor_priorities module to priorities.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 7f0e11bd06)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
cc1244afa3 mattermost: Run html2text as a subprocess.
html2text is GPL licensed.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit f3254bb558)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
b6c8acbf14 email_notifications: Run html2text as a subprocess.
html2text is GPL licensed.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit e10b7f2f3c)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
82155e15a5 tornado: Fix race condition on handler._request.
Commit 6fd1a558b7 (#21469) introduced an
await point where get_events_backend calls fetch_events in order to
switch threads.  This opened the possibility that, in the window
between the connect_handler call in fetch_events and the old location
of this assignment in get_events_backend, an event could arrive,
causing ClientDescriptor.add_event to crash on missing
handler._request.  Fix this by assigning handler._request earlier.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit e112b619cc)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
c9e00e6391 test_tornado: Call process_event on first fetch_events return.
The 0.1 second delay was sometimes not long enough to guarantee we hit
the async response path, resulting in a nondeterministic coverage
failure.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 0430705d13)
2022-07-07 13:31:32 -07:00
Anders Kaseorg
51d0886f60 requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 9ee636e920)
2022-07-07 13:31:32 -07:00
Alex Vandiver
214df3ea1f compilemessages: Use a consistent ordering for the languages list.
This makes the generated data more consistent.

(cherry picked from commit 160a8b9945)
2022-06-23 23:25:51 +00:00
Anders Kaseorg
b031537fe9 CVE-2022-31017: Fix edit event exposure in protected-history streams.
When editing an old message in a private stream with protected
history, the server would incorrectly send an API event including the
edited message to all of the stream’s current subscribers, including
those who should not have access to the old message. This API event is
ignored by official clients, so it could only be observed by a user
using a modified client or their browser’s developer tools.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-06-21 18:23:30 +00:00
Mateusz Mandera
b5e64dd1ef get_old_unclaimed_attachments: Add docstring explaining the logic.
(cherry picked from commit a671ae9749)
2022-06-20 11:13:24 -07:00
Mateusz Mandera
b1156e6d67 do_delete_old_unclaimed_attachments: Consider ArchivedAttachment rows.
This function is oblivious to the existence of ArchivedAttachment, which
is incorrect. A file can be removed if and only if it is not referenced
by any Messages or ArchivedMessages.

(cherry picked from commit 09dc166b45)
2022-06-20 11:13:24 -07:00
Mateusz Mandera
d918a09db8 test_upload: Fix some URLs to uploaded files.
Using http://localhost:9991 is incorrect - e.g. messages sent with file
urls constructed trigger do_claim_attachments to be called with empty
list in potential_path_ids.

realm.host should be used in all these places, like in the other tests
in the file.

(cherry picked from commit 5ff4754090)
2022-06-20 11:13:24 -07:00
Alex Vandiver
6a3488d7ed fill_memcached_caches: Document possible arguments to --cache.
(cherry picked from commit 3d66dd9eeb)
2022-06-20 11:00:14 -07:00
Anders Kaseorg
5de2969275 requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit f29553d809)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
e124464fea requirements: Upgrade to Tornado 6.
Fixes #8913.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 7acb642fa5)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
0ccc706f7a runtornado: Switch to asyncio event loop.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 6fd1a558b7)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
b4a0684201 queue: Use a thread-local Pika connection.
According to the documentation: “Pika does not have any notion of
threading in the code. If you want to use Pika with threading, make
sure you have a Pika connection per thread, created in that thread. It
is not safe to share one Pika connection across threads, with one
exception: you may call the connection method add_callback_threadsafe
from another thread to schedule a callback within an active pika
connection.”

https://pika.readthedocs.io/en/stable/faq.html

This also means that synchronous Django code running in Tornado will
use its own synchronous SimpleQueueClient rather than sharing the
asynchronous TornadoQueueClient, which is unfortunate but necessary as
they’re about to be on different threads.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit c263bfdb41)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
ad9187d9f7 cache: Instantiate only one BMemcached cache backend.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit c9faefd50e)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
ce8d8f3846 runtornado: Avoid deprecated IOLoop debugging methods.
IOLoop.set_blocking_log_threshold and IOLoop.handle_callback_exception
are removed in Tornado 6.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit 5d69dafddb)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
d632e2c6bf tornado: Remove instrument_tornado_ioloop.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit e4bf7066f3)
2022-05-16 12:05:23 -07:00
Anders Kaseorg
ac5e31ce04 tornado: Unfork tornado.autoreload.
We previously forked tornado.autoreload to work around a problem where
it would crash if you introduce a syntax error and not recover if you
fix it (https://github.com/tornadoweb/tornado/issues/2398).

A much more maintainable workaround for that issue, at least in
current Tornado, is to use tornado.autoreload as the main module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit bded7180f7)
2022-05-16 12:05:23 -07:00
Alex Vandiver
789e960672 test_link_embed: Remove unnecessary TEST_CACHES.
The only purpose of this seems to be to not have to reset the cache;
fae59502ab added it without any explanation for why it is necessary.

Remove it, and explicitly flush the cache in the one place where it is
necessary.

(cherry picked from commit 9030d53acb)
2022-05-03 16:12:08 -07:00
Alex Vandiver
572138d983 caches: Remove unnecessary "in-memory" cache.
This cache was added in da33b72848 to serve as a replacement for the
durable database cache, in development; the previous commit has
switched that to be the non-durable memcached backend.

The special-case for "in-memory" in development is mostly-unnecessary
in contrast to memcached -- `./tools/run-dev.py` flushes memcached on
every startup.  This differs in behaviour slightly, in that if the
codepath is changed and `run-dev` restarts Django, the cache is not
cleared.  This seems an unlikely occurrence, however, and the code
cleanup from its removal is worth it.

(cherry picked from commit 56058f3316)
2022-05-03 16:12:08 -07:00
Alex Vandiver
df8ac69d90 caches: Cache link preview data in memcached, not in PostgreSQL.
The choice to cache these in the database dates back to c93f1d4eda,
with the comment added in da33b72848 while working around the
durability of the "database" cache in local development.

The values were stored in a durable cache, as they needed to be
ensured to persist between when they were inserted in
`get_link_embed_data` and when they were used in
`render_incoming_message` via `link_embed_data_from_cache`.

However, database accesses are not fast compared to memcached, and we
wish to avoid the overhead of the database connection from the
`embed_links` worker.  Specifically, making the connection may not be
thread-safe -- and in low-memory (and Docker) configurations, all
workers run as separate threads in a single process.  This can lead to
stalled database connections in `embed_links` workers, and failed
previews.

Since the previous commit made the durability of the cache no longer
necessary, this will have minimal effect; at worst, posting the same
URL twice, on either side of an upgrade, will result in two preview
fetches of it.

(cherry picked from commit 04ca2e92f7)
2022-05-03 16:12:08 -07:00
Alex Vandiver
9a9c6730ff preview: Use cache only as a non-durable cache, not an IPC.
The `get_link_embed_data` / `link_embed_data_from_cache` pair as
introduced in c93f1d4eda uses the cache
as a temporary store inside of the `embed_links` worker; this means
that it must be durable storage, or the worker will stall and re-fetch
the same links to preview them.

Switch to plumbing through the fetched URL embed data as an parameter
to the Markdown evaluation which uses them, rather than using the
cache as an intermediary.  This frees up the cache to be merely a
non-durable cache.

As a side-effect, this removes get_cache_with_key, and
link_embed_data_from_cache which was its only callsite.

(cherry picked from commit 351bdfaf78)
2022-05-03 16:12:04 -07:00
Alex Vandiver
5ff82c82ae preview: Use a dataclass for the embed data.
This is significantly cleaner than passing around `Dict[str, Any]` all
of the time.

(cherry picked from commit 327ff9ea0f)
2022-05-03 16:10:25 -07:00