From 7fc62d8178bfc362231436746457d14c44d238ff Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 4 Mar 2022 10:24:26 +0000 Subject: [PATCH 1/7] Fix a panic in `OnIncomingMessagesRequest` (#2250) It's possible for `GetStateEvent` to return `nil` if there was no error but the state event wasn't found. Therefore we need to be prepared for that case. This should fix #2247. --- syncapi/routing/messages.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/syncapi/routing/messages.go b/syncapi/routing/messages.go index 7cd54eef0..9aef5db14 100644 --- a/syncapi/routing/messages.go +++ b/syncapi/routing/messages.go @@ -184,16 +184,18 @@ func OnIncomingMessagesRequest( // at least fetch the membership events for the users returned in chunk if LazyLoadMembers is set state := []gomatrixserverlib.ClientEvent{} if filter.LazyLoadMembers { - memberShipToUser := make(map[string]*gomatrixserverlib.HeaderedEvent) + membershipToUser := make(map[string]*gomatrixserverlib.HeaderedEvent) for _, evt := range clientEvents { - memberShip, err := db.GetStateEvent(req.Context(), roomID, gomatrixserverlib.MRoomMember, evt.Sender) + membership, err := db.GetStateEvent(req.Context(), roomID, gomatrixserverlib.MRoomMember, evt.Sender) if err != nil { util.GetLogger(req.Context()).WithError(err).Error("failed to get membership event for user") continue } - memberShipToUser[evt.Sender] = memberShip + if membership != nil { + membershipToUser[evt.Sender] = membership + } } - for _, evt := range memberShipToUser { + for _, evt := range membershipToUser { state = append(state, gomatrixserverlib.HeaderedToClientEvent(evt, gomatrixserverlib.FormatAll)) } } From 5e694cd362ed21fa824f219cd0058fb57897e079 Mon Sep 17 00:00:00 2001 From: S7evinK <2353100+S7evinK@users.noreply.github.com> Date: Fri, 4 Mar 2022 12:03:51 +0100 Subject: [PATCH 2/7] Un-ratelimit calls to /thumbnail (#2251) --- mediaapi/routing/routing.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mediaapi/routing/routing.go b/mediaapi/routing/routing.go index fc2136bb6..0e1583991 100644 --- a/mediaapi/routing/routing.go +++ b/mediaapi/routing/routing.go @@ -120,13 +120,16 @@ func makeDownloadAPI( w.Header().Set("Content-Type", "application/json") // Ratelimit requests - if r := rateLimits.Limit(req); r != nil { - if err := json.NewEncoder(w).Encode(r); err != nil { - w.WriteHeader(http.StatusInternalServerError) + // NOTSPEC: The spec says everything at /media/ should be rate limited, but this causes issues with thumbnails (#2243) + if name != "thumbnail" { + if r := rateLimits.Limit(req); r != nil { + if err := json.NewEncoder(w).Encode(r); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusTooManyRequests) return } - w.WriteHeader(http.StatusTooManyRequests) - return } vars, _ := httputil.URLDecodeMapValues(mux.Vars(req)) From 22a034dcba6e1437435011c5e0648972ad025140 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 4 Mar 2022 15:05:42 +0000 Subject: [PATCH 3/7] Fix memory leaks with SQLite prepared statements (#2253) --- .../storage/sqlite3/event_state_keys_table.go | 1 + .../storage/sqlite3/event_types_table.go | 1 + roomserver/storage/sqlite3/events_table.go | 37 ++++++++++++------- roomserver/storage/sqlite3/rooms_table.go | 5 ++- .../storage/sqlite3/state_block_table.go | 5 ++- .../storage/sqlite3/state_snapshot_table.go | 5 ++- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/roomserver/storage/sqlite3/event_state_keys_table.go b/roomserver/storage/sqlite3/event_state_keys_table.go index bf12d5b83..8af40024a 100644 --- a/roomserver/storage/sqlite3/event_state_keys_table.go +++ b/roomserver/storage/sqlite3/event_state_keys_table.go @@ -154,6 +154,7 @@ func (s *eventStateKeyStatements) BulkSelectEventStateKey( if err != nil { return nil, err } + defer selectPrep.Close() stmt := sqlutil.TxStmt(txn, selectPrep) rows, err := stmt.QueryContext(ctx, iEventStateKeyNIDs...) if err != nil { diff --git a/roomserver/storage/sqlite3/event_types_table.go b/roomserver/storage/sqlite3/event_types_table.go index f2c9c42fe..f794a3d0e 100644 --- a/roomserver/storage/sqlite3/event_types_table.go +++ b/roomserver/storage/sqlite3/event_types_table.go @@ -140,6 +140,7 @@ func (s *eventTypeStatements) BulkSelectEventTypeNID( if err != nil { return nil, err } + defer selectPrep.Close() stmt := sqlutil.TxStmt(txn, selectPrep) /////////////// diff --git a/roomserver/storage/sqlite3/events_table.go b/roomserver/storage/sqlite3/events_table.go index 969a10ce5..2ab1151d5 100644 --- a/roomserver/storage/sqlite3/events_table.go +++ b/roomserver/storage/sqlite3/events_table.go @@ -198,11 +198,12 @@ func (s *eventStatements) BulkSelectStateEventByID( iEventIDs[k] = v } selectOrig := strings.Replace(bulkSelectStateEventByIDSQL, "($1)", sqlutil.QueryVariadic(len(iEventIDs)), 1) - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, err } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) /////////////// rows, err := selectStmt.QueryContext(ctx, iEventIDs...) @@ -266,11 +267,12 @@ func (s *eventStatements) BulkSelectStateEventByNID( } } selectOrig += " ORDER BY event_type_nid, event_state_key_nid ASC" - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, fmt.Errorf("s.db.Prepare: %w", err) } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) rows, err := selectStmt.QueryContext(ctx, params...) if err != nil { return nil, fmt.Errorf("selectStmt.QueryContext: %w", err) @@ -307,11 +309,12 @@ func (s *eventStatements) BulkSelectStateAtEventByID( iEventIDs[k] = v } selectOrig := strings.Replace(bulkSelectStateAtEventByIDSQL, "($1)", sqlutil.QueryVariadic(len(iEventIDs)), 1) - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, err } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) /////////////// rows, err := selectStmt.QueryContext(ctx, iEventIDs...) if err != nil { @@ -390,10 +393,11 @@ func (s *eventStatements) BulkSelectStateAtEventAndReference( if err != nil { return nil, err } - selectPrep = sqlutil.TxStmt(txn, selectPrep) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) ////////////// - rows, err := sqlutil.TxStmt(txn, selectPrep).QueryContext(ctx, iEventNIDs...) + rows, err := sqlutil.TxStmt(txn, selectStmt).QueryContext(ctx, iEventNIDs...) if err != nil { return nil, fmt.Errorf("sqlutil.TxStmt.QueryContext: %w", err) } @@ -441,6 +445,7 @@ func (s *eventStatements) BulkSelectEventReference( if err != nil { return nil, err } + defer selectPrep.Close() // nolint:errcheck /////////////// selectStmt := sqlutil.TxStmt(txn, selectPrep) @@ -471,11 +476,12 @@ func (s *eventStatements) BulkSelectEventID(ctx context.Context, txn *sql.Tx, ev iEventNIDs[k] = v } selectOrig := strings.Replace(bulkSelectEventIDSQL, "($1)", sqlutil.QueryVariadic(len(iEventNIDs)), 1) - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, err } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) /////////////// rows, err := selectStmt.QueryContext(ctx, iEventNIDs...) @@ -526,11 +532,12 @@ func (s *eventStatements) bulkSelectEventNID(ctx context.Context, txn *sql.Tx, e } else { selectOrig = strings.Replace(bulkSelectEventNIDSQL, "($1)", sqlutil.QueryVariadic(len(iEventIDs)), 1) } - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, err } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) /////////////// rows, err := selectStmt.QueryContext(ctx, iEventIDs...) if err != nil { @@ -560,6 +567,7 @@ func (s *eventStatements) SelectMaxEventDepth(ctx context.Context, txn *sql.Tx, if err != nil { return 0, err } + defer sqlPrep.Close() err = sqlutil.TxStmt(txn, sqlPrep).QueryRowContext(ctx, iEventIDs...).Scan(&result) if err != nil { return 0, fmt.Errorf("sqlutil.TxStmt.QueryRowContext: %w", err) @@ -575,12 +583,13 @@ func (s *eventStatements) SelectRoomNIDsForEventNIDs( if err != nil { return nil, err } - sqlPrep = sqlutil.TxStmt(txn, sqlPrep) + defer sqlPrep.Close() + sqlStmt := sqlutil.TxStmt(txn, sqlPrep) iEventNIDs := make([]interface{}, len(eventNIDs)) for i, v := range eventNIDs { iEventNIDs[i] = v } - rows, err := sqlPrep.QueryContext(ctx, iEventNIDs...) + rows, err := sqlStmt.QueryContext(ctx, iEventNIDs...) if err != nil { return nil, err } diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go index 5413475e2..a81b78148 100644 --- a/roomserver/storage/sqlite3/rooms_table.go +++ b/roomserver/storage/sqlite3/rooms_table.go @@ -233,12 +233,13 @@ func (s *roomStatements) SelectRoomVersionsForRoomNIDs( if err != nil { return nil, err } - sqlPrep = sqlutil.TxStmt(txn, sqlPrep) + defer sqlPrep.Close() // nolint:errcheck + sqlStmt := sqlutil.TxStmt(txn, sqlPrep) iRoomNIDs := make([]interface{}, len(roomNIDs)) for i, v := range roomNIDs { iRoomNIDs[i] = v } - rows, err := sqlPrep.QueryContext(ctx, iRoomNIDs...) + rows, err := sqlStmt.QueryContext(ctx, iRoomNIDs...) if err != nil { return nil, err } diff --git a/roomserver/storage/sqlite3/state_block_table.go b/roomserver/storage/sqlite3/state_block_table.go index d51fc492d..3c829cdcd 100644 --- a/roomserver/storage/sqlite3/state_block_table.go +++ b/roomserver/storage/sqlite3/state_block_table.go @@ -108,11 +108,12 @@ func (s *stateBlockStatements) BulkSelectStateBlockEntries( intfs[i] = int64(stateBlockNIDs[i]) } selectOrig := strings.Replace(bulkSelectStateBlockEntriesSQL, "($1)", sqlutil.QueryVariadic(len(intfs)), 1) - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, err } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) rows, err := selectStmt.QueryContext(ctx, intfs...) if err != nil { return nil, err diff --git a/roomserver/storage/sqlite3/state_snapshot_table.go b/roomserver/storage/sqlite3/state_snapshot_table.go index 01df31e90..1f5e9ee3b 100644 --- a/roomserver/storage/sqlite3/state_snapshot_table.go +++ b/roomserver/storage/sqlite3/state_snapshot_table.go @@ -113,11 +113,12 @@ func (s *stateSnapshotStatements) BulkSelectStateBlockNIDs( nids[k] = v } selectOrig := strings.Replace(bulkSelectStateBlockNIDsSQL, "($1)", sqlutil.QueryVariadic(len(nids)), 1) - selectStmt, err := s.db.Prepare(selectOrig) + selectPrep, err := s.db.Prepare(selectOrig) if err != nil { return nil, err } - selectStmt = sqlutil.TxStmt(txn, selectStmt) + defer selectPrep.Close() // nolint:errcheck + selectStmt := sqlutil.TxStmt(txn, selectPrep) rows, err := selectStmt.QueryContext(ctx, nids...) if err != nil { From 24df85b4282c3a320417bc7651ed1a1400c86b36 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 4 Mar 2022 15:27:10 +0000 Subject: [PATCH 4/7] Mark soft-failed events as rejected in `roomserver_events` (#2252) --- roomserver/internal/input/input_events.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roomserver/internal/input/input_events.go b/roomserver/internal/input/input_events.go index 531d6959e..1acbf0443 100644 --- a/roomserver/internal/input/input_events.go +++ b/roomserver/internal/input/input_events.go @@ -294,7 +294,7 @@ func (r *Inputer) processRoomEvent( } // Store the event. - _, _, stateAtEvent, redactionEvent, redactedEventID, err := r.DB.StoreEvent(ctx, event, authEventNIDs, isRejected) + _, _, stateAtEvent, redactionEvent, redactedEventID, err := r.DB.StoreEvent(ctx, event, authEventNIDs, isRejected || softfail) if err != nil { return fmt.Errorf("updater.StoreEvent: %w", err) } From 0297929b76092e7ddbc53709fd9b16f2ae031653 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 4 Mar 2022 15:56:31 +0000 Subject: [PATCH 5/7] Revert NATS server upgrade in 00b3545b14ca5e6987be93cabafc51e771f8bcc7 --- go.mod | 2 +- go.sum | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 525950daa..7311dd067 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/matrix-org/dendrite -replace github.com/nats-io/nats-server/v2 => github.com/neilalexander/nats-server/v2 v2.7.4-0.20220302103432-6b04b9f12740 +replace github.com/nats-io/nats-server/v2 => github.com/neilalexander/nats-server/v2 v2.7.2-0.20220217100407-087330ed46ad replace github.com/nats-io/nats.go => github.com/neilalexander/nats.go v1.11.1-0.20220104162523-f4ddebe1061c diff --git a/go.sum b/go.sum index 25697f59c..74c6278e5 100644 --- a/go.sum +++ b/go.sum @@ -480,6 +480,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/gologme/log v1.2.0/go.mod h1:gq31gQ8wEHkR+WekdWsqDuf8pXTUZA9BnnzTuPz1Y9U= github.com/gologme/log v1.3.0 h1:l781G4dE+pbigClDSDzSaaYKtiueHCILUa/qSDsmHAo= github.com/gologme/log v1.3.0/go.mod h1:yKT+DvIPdDdDoPtqFrFxheooyVmoqi0BAsw+erN3wA4= @@ -711,8 +713,8 @@ github.com/klauspost/compress v1.9.7/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0 github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= -github.com/klauspost/compress v1.14.4 h1:eijASRJcobkVtSt81Olfh7JX43osYLwy5krOJo6YEu4= -github.com/klauspost/compress v1.14.4/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= +github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -1027,8 +1029,8 @@ github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7 github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g= github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ= -github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g= -github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= +github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U= @@ -1130,8 +1132,8 @@ github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uY github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= -github.com/neilalexander/nats-server/v2 v2.7.4-0.20220302103432-6b04b9f12740 h1:RJrc+z35RHZlrjR6UBt9UmVRAlFh4SgYyEA0YpQdPHM= -github.com/neilalexander/nats-server/v2 v2.7.4-0.20220302103432-6b04b9f12740/go.mod h1:eJUrA5gm0ch6sJTEv85xmXIgQWsB0OyjkTsKXvlHbYc= +github.com/neilalexander/nats-server/v2 v2.7.2-0.20220217100407-087330ed46ad h1:Z2nWMQsXWWqzj89nW6OaLJSdkFknqhaR5whEOz4++Y8= +github.com/neilalexander/nats-server/v2 v2.7.2-0.20220217100407-087330ed46ad/go.mod h1:tckmrt0M6bVaDT3kmh9UrIq/CBOBBse+TpXQi5ldaa8= github.com/neilalexander/nats.go v1.11.1-0.20220104162523-f4ddebe1061c h1:G2qsv7D0rY94HAu8pXmElMluuMHQ85waxIDQBhIzV2Q= github.com/neilalexander/nats.go v1.11.1-0.20220104162523-f4ddebe1061c/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= github.com/neilalexander/utp v0.1.1-0.20210622132614-ee9a34a30488/go.mod h1:NPHGhPc0/wudcaCqL/H5AOddkRf8GPRhzOujuUKGQu8= From 86d4eef9f10abd944bf689298cc6f9e915b940c7 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 4 Mar 2022 16:20:23 +0000 Subject: [PATCH 6/7] Version 0.6.5 (#2254) * Version and changelog * Update changelog * Update changelog * Update readme * Update readme some more * Fix date in changelog --- CHANGES.md | 34 +++++++++++++++++++--- README.md | 69 ++++++++++++++++++++++++++------------------- internal/version.go | 2 +- 3 files changed, 71 insertions(+), 34 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ee608194d..eb365baf1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,31 @@ # Changelog +## Dendrite 0.6.5 (2022-03-04) + +### Features + +* Early support for push notifications has been added, with support for push rules, pushers, HTTP push gateways and the `/notifications` endpoint (contributions by [danpe](https://github.com/danpe), [PiotrKozimor](https://github.com/PiotrKozimor) and [tommie](https://github.com/tommie)) +* Spaces Summary (MSC2946) is now correctly supported (when `msc2946` is enabled in the config) +* All media API endpoints are now available under the `/v3` namespace +* Profile updates (display name and avatar) are now sent asynchronously so they shouldn't block the client for a very long time +* State resolution v2 has been optimised further to considerably reduce the number of memory allocations +* State resolution v2 will no longer duplicate events unnecessarily when calculating the auth difference +* The `create-account` tool now has a `-reset-password` option for resetting the passwords of existing accounts +* The `/sync` endpoint now calculates device list changes much more quickly with less RAM used +* The `/messages` endpoint now lazy-loads members correctly + +### Fixes + +* Read receipts now work correctly by correcting bugs in the stream positions and receipt coalescing +* Topological sorting of state and join responses has been corrected, which should help to reduce the number of auth problems when joining new federated rooms +* Media thumbnails should now work properly after having unnecessarily strict rate limiting removed +* The roomserver no longer holds transactions for as long when processing input events +* Uploading device keys and cross-signing keys will now correctly no-op if there were no changes +* Parameters are now remembered correctly during registration +* Devices can now only be deleted within the appropriate UIA flow +* The `/context` endpoint now returns 404 instead of 500 if the event was not found +* SQLite mode will no longer leak memory as a result of not closing prepared statements + ## Dendrite 0.6.4 (2022-02-21) ### Features @@ -210,9 +236,9 @@ ### Fixes -- **SECURITY:** A bug in SQLite mode which could cause the registration flow to complete unexpectedly for existing accounts has been fixed (PostgreSQL deployments are not affected) -- A panic in the federation sender has been fixed when shutting down destination queues -- The `/keys/upload` endpoint now correctly returns the number of one-time keys in response to an empty upload request +* **SECURITY:** A bug in SQLite mode which could cause the registration flow to complete unexpectedly for existing accounts has been fixed (PostgreSQL deployments are not affected) +* A panic in the federation sender has been fixed when shutting down destination queues +* The `/keys/upload` endpoint now correctly returns the number of one-time keys in response to an empty upload request ## Dendrite 0.3.10 (2021-02-17) @@ -534,4 +560,4 @@ First versioned release of Dendrite. * Typing: Yes. * Presence: No. * Receipts: No. -* OpenID: No. \ No newline at end of file +* OpenID: No. diff --git a/README.md b/README.md index a077788cf..d3a862587 100644 --- a/README.md +++ b/README.md @@ -2,26 +2,29 @@ Dendrite is a second-generation Matrix homeserver written in Go. It intends to provide an **efficient**, **reliable** and **scalable** alternative to [Synapse](https://github.com/matrix-org/synapse): - - Efficient: A small memory footprint with better baseline performance than an out-of-the-box Synapse. - - Reliable: Implements the Matrix specification as written, using the + +- Efficient: A small memory footprint with better baseline performance than an out-of-the-box Synapse. +- Reliable: Implements the Matrix specification as written, using the [same test suite](https://github.com/matrix-org/sytest) as Synapse as well as a [brand new Go test suite](https://github.com/matrix-org/complement). - - Scalable: can run on multiple machines and eventually scale to massive homeserver deployments. +- Scalable: can run on multiple machines and eventually scale to massive homeserver deployments. As of October 2020, Dendrite has now entered **beta** which means: + - Dendrite is ready for early adopters. We recommend running in Monolith mode with a PostgreSQL database. - Dendrite has periodic semver releases. We intend to release new versions as we land significant features. - Dendrite supports database schema upgrades between releases. This means you should never lose your messages when upgrading Dendrite. - Breaking changes will not occur on minor releases. This means you can safely upgrade Dendrite without modifying your database or config file. This does not mean: - - Dendrite is bug-free. It has not yet been battle-tested in the real world and so will be error prone initially. - - All of the CS/Federation APIs are implemented. We are tracking progress via a script called 'Are We Synapse Yet?'. In particular, + +- Dendrite is bug-free. It has not yet been battle-tested in the real world and so will be error prone initially. +- All of the CS/Federation APIs are implemented. We are tracking progress via a script called 'Are We Synapse Yet?'. In particular, presence and push notifications are entirely missing from Dendrite. See [CHANGES.md](CHANGES.md) for updates. - - Dendrite is ready for massive homeserver deployments. You cannot shard each microservice, only run each one on a different machine. +- Dendrite is ready for massive homeserver deployments. You cannot shard each microservice, only run each one on a different machine. Currently, we expect Dendrite to function well for small (10s/100s of users) homeserver deployments as well as P2P Matrix nodes in-browser or on mobile devices. -In the future, we will be able to scale up to gigantic servers (equivalent to matrix.org) via polylith mode. +In the future, we will be able to scale up to gigantic servers (equivalent to matrix.org) via polylith mode. If you have further questions, please take a look at [our FAQ](docs/FAQ.md) or join us in: @@ -31,14 +34,16 @@ If you have further questions, please take a look at [our FAQ](docs/FAQ.md) or j ## Requirements -To build Dendrite, you will need Go 1.16 or later. +To build Dendrite, you will need Go 1.16 or later. For a usable federating Dendrite deployment, you will also need: -- A domain name (or subdomain) + +- A domain name (or subdomain) - A valid TLS certificate issued by a trusted authority for that domain - SRV records or a well-known file pointing to your deployment Also recommended are: + - A PostgreSQL database engine, which will perform better than SQLite with many users and/or larger rooms - A reverse proxy server, such as nginx, configured [like this sample](https://github.com/matrix-org/dendrite/blob/master/docs/nginx/monolith-sample.conf) @@ -76,30 +81,32 @@ Then point your favourite Matrix client at `http://localhost:8008` or `https://l We use a script called Are We Synapse Yet which checks Sytest compliance rates. Sytest is a black-box homeserver test rig with around 900 tests. The script works out how many of these tests are passing on Dendrite and it -updates with CI. As of January 2022 we're at around 65% CS API coverage and 92% Federation coverage, though check +updates with CI. As of March 2022 we're at around 76% CS API coverage and 95% Federation coverage, though check CI for the latest numbers. In practice, this means you can communicate locally and via federation with Synapse servers such as matrix.org reasonably well. There's a long list of features that are not implemented, notably: - - Push - - Search and Context - - User Directory - - Presence - - Guests + +- Search +- User Directory +- Presence We are prioritising features that will benefit single-user homeservers first (e.g Receipts, E2E) rather than features that massive deployments may be interested in (User Directory, OpenID, Guests, Admin APIs, AS API). This means Dendrite supports amongst others: - - Core room functionality (creating rooms, invites, auth rules) - - Federation in rooms v1-v7 - - Backfilling locally and via federation - - Accounts, Profiles and Devices - - Published room lists - - Typing - - Media APIs - - Redaction - - Tagging - - E2E keys and device lists - - Receipts +- Core room functionality (creating rooms, invites, auth rules) +- Federation in rooms v1-v7 +- Backfilling locally and via federation +- Accounts, Profiles and Devices +- Published room lists +- Typing +- Media APIs +- Redaction +- Tagging +- Context +- E2E keys and device lists +- Receipts +- Push +- Guests ## Contributing @@ -112,6 +119,7 @@ For example, if the test `Local device key changes get to remote servers` was ma test file (e.g via `grep` or via the [CI log output](https://buildkite.com/matrix-dot-org/dendrite/builds/2826#39cff5de-e032-4ad0-ad26-f819e6919c42) it's `tests/50federation/40devicelists.pl` ) then to run Sytest: + ``` docker run --rm --name sytest -v "/Users/kegan/github/sytest:/sytest" @@ -121,10 +129,12 @@ docker run --rm --name sytest -e "POSTGRES=1" -e "DENDRITE_TRACE_HTTP=1" matrixdotorg/sytest-dendrite:latest tests/50federation/40devicelists.pl ``` + See [sytest.md](docs/sytest.md) for the full description of these flags. You can try running sytest outside of docker for faster runs, but the dependencies can be temperamental and we recommend using docker where possible. + ``` cd sytest export PERL5LIB=$HOME/lib/perl5 @@ -149,8 +159,9 @@ Dendrite in Monolith + SQLite works in a range of environments including iOS and For small homeserver installations joined on ~10s rooms on matrix.org with ~100s of users in those rooms, including some encrypted rooms: - - Memory: uses around 100MB of RAM, with peaks at around 200MB. - - Disk space: After a few months of usage, the database grew to around 2GB (in Monolith mode). - - CPU: Brief spikes when processing events, typically idles at 1% CPU. + +- Memory: uses around 100MB of RAM, with peaks at around 200MB. +- Disk space: After a few months of usage, the database grew to around 2GB (in Monolith mode). +- CPU: Brief spikes when processing events, typically idles at 1% CPU. This means Dendrite should comfortably work on things like Raspberry Pis. diff --git a/internal/version.go b/internal/version.go index 2ea1c5201..2e8de4175 100644 --- a/internal/version.go +++ b/internal/version.go @@ -17,7 +17,7 @@ var build string const ( VersionMajor = 0 VersionMinor = 6 - VersionPatch = 4 + VersionPatch = 5 VersionTag = "" // example: "rc1" ) From 9fbaa1194bb3d7e9f4dfff09461528b846d26a6e Mon Sep 17 00:00:00 2001 From: S7evinK <2353100+S7evinK@users.noreply.github.com> Date: Mon, 7 Mar 2022 10:37:04 +0100 Subject: [PATCH 7/7] Add canonical alias support (#2236) * Add canonical support * Add test * Check that the send event is actually an m.room.canonical_alias Check that we got an event from the database * Update to get correct required events * Add flakey test to blacklist --- clientapi/jsonerror/jsonerror.go | 5 +++ clientapi/routing/sendevent.go | 36 ++++++++++++++++++ roomserver/api/alias.go | 19 ++++++++++ roomserver/api/alias_test.go | 62 +++++++++++++++++++++++++++++++ roomserver/internal/alias.go | 63 ++++++++++++++++++++++++++++++-- sytest-blacklist | 3 +- sytest-whitelist | 8 +++- 7 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 roomserver/api/alias_test.go diff --git a/clientapi/jsonerror/jsonerror.go b/clientapi/jsonerror/jsonerror.go index 97c597030..1fc1c0c01 100644 --- a/clientapi/jsonerror/jsonerror.go +++ b/clientapi/jsonerror/jsonerror.go @@ -58,6 +58,11 @@ func BadJSON(msg string) *MatrixError { return &MatrixError{"M_BAD_JSON", msg} } +// BadAlias is an error when the client supplies a bad alias. +func BadAlias(msg string) *MatrixError { + return &MatrixError{"M_BAD_ALIAS", msg} +} + // NotJSON is an error when the client supplies something that is not JSON // to a JSON endpoint. func NotJSON(msg string) *MatrixError { diff --git a/clientapi/routing/sendevent.go b/clientapi/routing/sendevent.go index 23935b5d9..3d5993718 100644 --- a/clientapi/routing/sendevent.go +++ b/clientapi/routing/sendevent.go @@ -16,6 +16,8 @@ package routing import ( "context" + "encoding/json" + "fmt" "net/http" "sync" "time" @@ -120,6 +122,40 @@ func SendEvent( } timeToGenerateEvent := time.Since(startedGeneratingEvent) + // validate that the aliases exists + if eventType == gomatrixserverlib.MRoomCanonicalAlias && stateKey != nil && *stateKey == "" { + aliasReq := api.AliasEvent{} + if err = json.Unmarshal(e.Content(), &aliasReq); err != nil { + return util.ErrorResponse(fmt.Errorf("unable to parse alias event: %w", err)) + } + if !aliasReq.Valid() { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.InvalidParam("Request contains invalid aliases."), + } + } + aliasRes := &api.GetAliasesForRoomIDResponse{} + if err = rsAPI.GetAliasesForRoomID(req.Context(), &api.GetAliasesForRoomIDRequest{RoomID: roomID}, aliasRes); err != nil { + return jsonerror.InternalServerError() + } + var found int + requestAliases := append(aliasReq.AltAliases, aliasReq.Alias) + for _, alias := range aliasRes.Aliases { + for _, altAlias := range requestAliases { + if altAlias == alias { + found++ + } + } + } + // check that we found at least the same amount of existing aliases as are in the request + if aliasReq.Alias != "" && found < len(requestAliases) { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadAlias("No matching alias found."), + } + } + } + var txnAndSessionID *api.TransactionID if txnID != nil { txnAndSessionID = &api.TransactionID{ diff --git a/roomserver/api/alias.go b/roomserver/api/alias.go index df69e5b4d..be37333b6 100644 --- a/roomserver/api/alias.go +++ b/roomserver/api/alias.go @@ -14,6 +14,8 @@ package api +import "regexp" + // SetRoomAliasRequest is a request to SetRoomAlias type SetRoomAliasRequest struct { // ID of the user setting the alias @@ -84,3 +86,20 @@ type RemoveRoomAliasResponse struct { // Did we remove it? Removed bool `json:"removed"` } + +type AliasEvent struct { + Alias string `json:"alias"` + AltAliases []string `json:"alt_aliases"` +} + +var validateAliasRegex = regexp.MustCompile("^#.*:.+$") + +func (a AliasEvent) Valid() bool { + for _, alias := range a.AltAliases { + if !validateAliasRegex.MatchString(alias) { + return false + } + } + return a.Alias == "" || validateAliasRegex.MatchString(a.Alias) +} + diff --git a/roomserver/api/alias_test.go b/roomserver/api/alias_test.go new file mode 100644 index 000000000..680493b7b --- /dev/null +++ b/roomserver/api/alias_test.go @@ -0,0 +1,62 @@ +package api + +import "testing" + +func TestAliasEvent_Valid(t *testing.T) { + type fields struct { + Alias string + AltAliases []string + } + tests := []struct { + name string + fields fields + want bool + }{ + { + name: "empty alias", + fields: fields{ + Alias: "", + }, + want: true, + }, + { + name: "empty alias, invalid alt aliases", + fields: fields{ + Alias: "", + AltAliases: []string{ "%not:valid.local"}, + }, + }, + { + name: "valid alias, invalid alt aliases", + fields: fields{ + Alias: "#valid:test.local", + AltAliases: []string{ "%not:valid.local"}, + }, + }, + { + name: "empty alias, invalid alt aliases", + fields: fields{ + Alias: "", + AltAliases: []string{ "%not:valid.local"}, + }, + }, + { + name: "invalid alias", + fields: fields{ + Alias: "%not:valid.local", + AltAliases: []string{ }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := AliasEvent{ + Alias: tt.fields.Alias, + AltAliases: tt.fields.AltAliases, + } + if got := a.Valid(); got != tt.want { + t.Errorf("Valid() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/roomserver/internal/alias.go b/roomserver/internal/alias.go index 7995279d2..5c1c04f01 100644 --- a/roomserver/internal/alias.go +++ b/roomserver/internal/alias.go @@ -16,12 +16,18 @@ package internal import ( "context" + "database/sql" + "errors" "fmt" - - "github.com/matrix-org/dendrite/roomserver/api" - "github.com/matrix-org/gomatrixserverlib" + "time" asAPI "github.com/matrix-org/dendrite/appservice/api" + "github.com/matrix-org/dendrite/internal/eventutil" + "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/dendrite/roomserver/internal/helpers" + "github.com/matrix-org/gomatrixserverlib" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" ) // RoomserverInternalAPIDatabase has the storage APIs needed to implement the alias API. @@ -183,6 +189,57 @@ func (r *RoomserverInternalAPI) RemoveRoomAlias( } } + ev, err := r.DB.GetStateEvent(ctx, roomID, gomatrixserverlib.MRoomCanonicalAlias, "") + if err != nil && err != sql.ErrNoRows { + return err + } else if ev != nil { + stateAlias := gjson.GetBytes(ev.Content(), "alias").Str + // the alias to remove is currently set as the canonical alias, remove it + if stateAlias == request.Alias { + res, err := sjson.DeleteBytes(ev.Content(), "alias") + if err != nil { + return err + } + + sender := request.UserID + if request.UserID != ev.Sender() { + sender = ev.Sender() + } + + builder := &gomatrixserverlib.EventBuilder{ + Sender: sender, + RoomID: ev.RoomID(), + Type: ev.Type(), + StateKey: ev.StateKey(), + Content: res, + } + + eventsNeeded, err := gomatrixserverlib.StateNeededForEventBuilder(builder) + if err != nil { + return fmt.Errorf("gomatrixserverlib.StateNeededForEventBuilder: %w", err) + } + if len(eventsNeeded.Tuples()) == 0 { + return errors.New("expecting state tuples for event builder, got none") + } + + stateRes := &api.QueryLatestEventsAndStateResponse{} + if err := helpers.QueryLatestEventsAndState(ctx, r.DB, &api.QueryLatestEventsAndStateRequest{RoomID: roomID, StateToFetch: eventsNeeded.Tuples()}, stateRes); err != nil { + return err + } + + newEvent, err := eventutil.BuildEvent(ctx, builder, r.Cfg.Matrix, time.Now(), &eventsNeeded, stateRes) + if err != nil { + return err + } + + err = api.SendEvents(ctx, r.RSAPI, api.KindNew, []*gomatrixserverlib.HeaderedEvent{newEvent}, r.ServerName, r.ServerName, nil, false) + if err != nil { + return err + } + + } + } + // Remove the alias from the database if err := r.DB.RemoveRoomAlias(ctx, request.Alias); err != nil { return err diff --git a/sytest-blacklist b/sytest-blacklist index becc500ec..0cdfebcc0 100644 --- a/sytest-blacklist +++ b/sytest-blacklist @@ -31,8 +31,9 @@ Remove group role # Flakey AS-ghosted users can use rooms themselves +/context/ with lazy_load_members filter works # Flakey, need additional investigation Messages that notify from another user increment notification_count Messages that highlight from another user increment unread highlight count -Notifications can be viewed with GET /notifications \ No newline at end of file +Notifications can be viewed with GET /notifications diff --git a/sytest-whitelist b/sytest-whitelist index 63d779bf1..377425c9b 100644 --- a/sytest-whitelist +++ b/sytest-whitelist @@ -648,7 +648,7 @@ Device list doesn't change if remote server is down /context/ on joined room works /context/ on non world readable room does not work /context/ returns correct number of events -/context/ with lazy_load_members filter works + GET /rooms/:room_id/messages lazy loads members correctly Can query remote device keys using POST after notification Device deletion propagates over federation @@ -659,4 +659,8 @@ registration accepts non-ascii passwords registration with inhibit_login inhibits login The operation must be consistent through an interactive authentication session Multiple calls to /sync should not cause 500 errors -/context/ with lazy_load_members filter works + +Canonical alias can be set +Canonical alias can include alt_aliases +Can delete canonical alias +Multiple calls to /sync should not cause 500 errors