-
Notifications
You must be signed in to change notification settings - Fork 51
/
eventV1.go
360 lines (305 loc) · 9.72 KB
/
eventV1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package gomatrixserverlib
import (
"bytes"
"encoding/json"
"fmt"
"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"golang.org/x/crypto/ed25519"
)
type eventV1 struct {
redacted bool
eventJSON []byte
roomVersion RoomVersion
eventFields
EventIDRaw string `json:"event_id,omitempty"`
PrevEvents []eventReference `json:"prev_events"`
AuthEvents []eventReference `json:"auth_events"`
}
// MarshalJSON implements json.Marshaller
func (e eventV1) MarshalJSON() ([]byte, error) {
if e.eventJSON == nil {
return nil, fmt.Errorf("gomatrixserverlib: cannot serialise uninitialised Event")
}
return e.eventJSON, nil
}
func (e *eventV1) EventID() string {
return e.EventIDRaw
}
func (e *eventV1) StateKey() *string {
return e.eventFields.StateKey
}
func (e *eventV1) StateKeyEquals(s string) bool {
if e.eventFields.StateKey == nil {
return false
}
return *e.eventFields.StateKey == s
}
func (e *eventV1) Type() string {
return e.eventFields.Type
}
func (e *eventV1) Content() []byte {
return e.eventFields.Content
}
func (e *eventV1) JoinRule() (string, error) {
if !e.StateKeyEquals("") {
return "", fmt.Errorf("gomatrixserverlib: JoinRule() event is not a m.room.join_rules event, bad state key")
}
var content JoinRuleContent
if err := json.Unmarshal(e.eventFields.Content, &content); err != nil {
return "", err
}
return content.JoinRule, nil
}
func (e *eventV1) HistoryVisibility() (HistoryVisibility, error) {
if !e.StateKeyEquals("") {
return "", fmt.Errorf("gomatrixserverlib: HistoryVisibility() event is not a m.room.history_visibility event, bad state key")
}
var content HistoryVisibilityContent
if err := json.Unmarshal(e.eventFields.Content, &content); err != nil {
return "", err
}
return content.HistoryVisibility, nil
}
func (e *eventV1) Membership() (string, error) {
var content struct {
Membership string `json:"membership"`
}
if err := json.Unmarshal(e.eventFields.Content, &content); err != nil {
return "", err
}
if e.StateKey() == nil {
return "", fmt.Errorf("gomatrixserverlib: Membersip() event is not a m.room.member event, missing state key")
}
return content.Membership, nil
}
func (e *eventV1) PowerLevels() (*PowerLevelContent, error) {
if !e.StateKeyEquals("") {
return nil, fmt.Errorf("gomatrixserverlib: PowerLevels() event is not a m.room.power_levels event, bad state key")
}
c, err := NewPowerLevelContentFromEvent(e)
if err != nil {
return nil, err
}
return &c, nil
}
func (e *eventV1) Version() RoomVersion {
return e.roomVersion
}
func (e *eventV1) RoomID() spec.RoomID {
roomID, err := spec.NewRoomID(e.eventFields.RoomID)
if err != nil {
panic(fmt.Errorf("RoomID is invalid: %w", err))
}
return *roomID
}
func (e *eventV1) Redacts() string {
return e.eventFields.Redacts
}
func (e *eventV1) Redacted() bool {
return e.redacted
}
func (e *eventV1) PrevEventIDs() []string {
result := make([]string, 0, len(e.PrevEvents))
for _, id := range e.PrevEvents {
result = append(result, id.EventID)
}
return result
}
func (e *eventV1) AuthEventIDs() []string {
result := make([]string, 0, len(e.AuthEvents))
for _, id := range e.AuthEvents {
result = append(result, id.EventID)
}
return result
}
func (e *eventV1) OriginServerTS() spec.Timestamp {
return e.eventFields.OriginServerTS
}
func (e *eventV1) Redact() {
if e.redacted {
return
}
verImpl, err := GetRoomVersion(e.roomVersion)
if err != nil {
panic(fmt.Errorf("gomatrixserverlib: invalid event %v", err))
}
eventJSON, err := verImpl.RedactEventJSON(e.eventJSON)
if err != nil {
// This is unreachable for events created with EventBuilder.Build or NewEventFromUntrustedJSON
panic(fmt.Errorf("gomatrixserverlib: invalid event %v", err))
}
if eventJSON, err = EnforcedCanonicalJSON(eventJSON, e.roomVersion); err != nil {
// This is unreachable for events created with EventBuilder.Build or NewEventFromUntrustedJSON
panic(fmt.Errorf("gomatrixserverlib: invalid event %v", err))
}
var res eventV1
err = json.Unmarshal(eventJSON, &res)
if err != nil {
panic(fmt.Errorf("gomatrixserverlib: populateFieldsFromJSON failed %v", err))
}
res.redacted = true
res.roomVersion = e.roomVersion
res.eventJSON = eventJSON
*e = res
}
func (e *eventV1) SenderID() spec.SenderID {
return spec.SenderID(e.eventFields.SenderID)
}
func (e *eventV1) Unsigned() []byte {
return e.eventFields.Unsigned
}
func (e *eventV1) SetUnsigned(unsigned interface{}) (PDU, error) {
var eventAsMap map[string]spec.RawJSON
var err error
if err = json.Unmarshal(e.eventJSON, &eventAsMap); err != nil {
return nil, err
}
unsignedJSON, err := json.Marshal(unsigned)
if err != nil {
return nil, err
}
eventAsMap["unsigned"] = unsignedJSON
eventJSON, err := json.Marshal(eventAsMap)
if err != nil {
return nil, err
}
if eventJSON, err = EnforcedCanonicalJSON(eventJSON, e.roomVersion); err != nil {
return nil, err
}
result := *e
result.eventJSON = eventJSON
result.eventFields.Unsigned = unsignedJSON
return &result, nil
}
func (e *eventV1) SetUnsignedField(path string, value interface{}) error {
// The safest way is to change the unsigned json and then reparse the
// event fully. But since we are only changing the unsigned section,
// which doesn't affect the signatures or hashes, we can cheat and
// just fiddle those bits directly.
path = "unsigned." + path
eventJSON, err := sjson.SetBytes(e.eventJSON, path, value)
if err != nil {
return err
}
eventJSON = CanonicalJSONAssumeValid(eventJSON)
res := gjson.GetBytes(eventJSON, "unsigned")
unsigned := RawJSONFromResult(res, eventJSON)
e.eventFields.Unsigned = unsigned
e.eventJSON = eventJSON
return nil
}
func (e *eventV1) Sign(signingName string, keyID KeyID, privateKey ed25519.PrivateKey) PDU {
eventJSON, err := signEvent(signingName, keyID, privateKey, e.eventJSON, e.roomVersion)
if err != nil {
// This is unreachable for events created with EventBuilder.Build or NewEventFromUntrustedJSON
panic(fmt.Errorf("gomatrixserverlib: invalid event %v (%q)", err, string(e.eventJSON)))
}
if eventJSON, err = EnforcedCanonicalJSON(eventJSON, e.roomVersion); err != nil {
// This is unreachable for events created with EventBuilder.Build or NewEventFromUntrustedJSON
panic(fmt.Errorf("gomatrixserverlib: invalid event %v (%q)", err, string(e.eventJSON)))
}
res := &e
(*res).eventJSON = eventJSON
return *res
}
func (e *eventV1) Depth() int64 {
return e.eventFields.Depth
}
func (e *eventV1) JSON() []byte {
return e.eventJSON
}
func (e *eventV1) ToHeaderedJSON() ([]byte, error) {
var err error
eventJSON := e.JSON()
eventJSON, err = sjson.SetBytes(eventJSON, "_room_version", e.Version())
if err != nil {
return []byte{}, err
}
eventJSON, err = sjson.SetBytes(eventJSON, "_event_id", e.EventID())
if err != nil {
return []byte{}, err
}
return eventJSON, nil
}
func newEventFromUntrustedJSONV1(eventJSON []byte, roomVersion IRoomVersion) (PDU, error) {
if r := gjson.GetBytes(eventJSON, "_*"); r.Exists() {
return nil, fmt.Errorf("gomatrixserverlib NewEventFromUntrustedJSON: found top-level '_' key, is this a headered event: %v", string(eventJSON))
}
if err := roomVersion.CheckCanonicalJSON(eventJSON); err != nil {
return nil, BadJSONError{err}
}
res := &eventV1{}
res.roomVersion = roomVersion.Version()
// Synapse removes these keys from events in case a server accidentally added them.
// https://github.com/matrix-org/synapse/blob/v0.18.5/synapse/crypto/event_signing.py#L57-L62
var err error
for _, key := range []string{"outlier", "destinations", "age_ts", "unsigned"} {
if eventJSON, err = sjson.DeleteBytes(eventJSON, key); err != nil {
return nil, err
}
}
if err := json.Unmarshal(eventJSON, &res); err != nil {
return nil, err
}
if err := checkID(res.eventFields.RoomID, "room", '!'); err != nil {
return nil, err
}
// We know the JSON must be valid here.
eventJSON = CanonicalJSONAssumeValid(eventJSON)
res.eventJSON = eventJSON
if err = checkEventContentHash(eventJSON); err != nil {
res.redacted = true
// If the content hash doesn't match then we have to discard all non-essential fields
// because they've been tampered with.
var redactedJSON []byte
if redactedJSON, err = roomVersion.RedactEventJSON(eventJSON); err != nil {
return nil, err
}
redactedJSON = CanonicalJSONAssumeValid(redactedJSON)
// We need to ensure that `result` is the redacted event.
// If redactedJSON is the same as eventJSON then `result` is already
// correct. If not then we need to reparse.
//
// Yes, this means that for some events we parse twice (which is slow),
// but means that parsing unredacted events is fast.
if !bytes.Equal(redactedJSON, eventJSON) {
result, err := roomVersion.NewEventFromTrustedJSON(redactedJSON, true)
if err != nil {
return nil, err
}
err = CheckFields(result)
return result, err
}
}
err = CheckFields(res)
return res, err
}
func newEventFromTrustedJSONV1(eventJSON []byte, redacted bool, roomVersion IRoomVersion) (PDU, error) {
res := &eventV1{}
if err := json.Unmarshal(eventJSON, &res); err != nil {
return nil, err
}
if err := checkID(res.eventFields.RoomID, "room", '!'); err != nil {
return nil, fmt.Errorf("RoomID is invalid: %w", err)
}
res.eventJSON = eventJSON
res.roomVersion = roomVersion.Version()
res.redacted = redacted
return res, nil
}
func newEventFromTrustedJSONWithEventIDV1(eventID string, eventJSON []byte, redacted bool, roomVersion IRoomVersion) (PDU, error) {
res := &eventV1{}
if err := json.Unmarshal(eventJSON, &res); err != nil {
return nil, err
}
if err := checkID(res.eventFields.RoomID, "room", '!'); err != nil {
return nil, err
}
res.EventIDRaw = eventID
res.eventJSON = eventJSON
res.roomVersion = roomVersion.Version()
res.redacted = redacted
return res, nil
}