Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send m.room_key_request events when we fail to decrypt an event #448

Merged
merged 2 commits into from
Jun 6, 2017

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Jun 1, 2017

No description provided.

}

/**
* Send off a room key request, if we don't already have one
Copy link
Member

Choose a reason for hiding this comment

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

if we don't already have one

Doesn't make sense to me. Do you mean: "if we don't already have one queued."? How do we determine if one request is the same as another request?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the comment.

requestId: this._baseApis.makeTxnId(),
state: ROOM_KEY_REQUEST_STATES.UNSENT,
}).then((req) => {
if (!req.state) {
Copy link
Member

Choose a reason for hiding this comment

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

It's not obvious what this is meant to mean. Why do we only start a timer if there is no state prop?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm actually relying on the fact that ROOM_KEY_REQUEST_STATES.UNSENT is zero. I agree it's horribly unclear. Updated.


return this._cryptoStore.getOutgoingRoomKeyRequestByState([
ROOM_KEY_REQUEST_STATES.UNSENT,
]).then((req) => {
Copy link
Member

Choose a reason for hiding this comment

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

It seems like a weird design decision to have this return a single request rather than an array of requests. I'm guessing that there is some batching going on or something? Why return only 1 request if N match? Is there significance to which request is selected (FIFO/LIFO/arbitrary?).

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems like a weird design decision to have this return a single request rather than an array of requests. I'm guessing that there is some batching going on or something? Why return only 1 request if N match?

well, I have to loop anyway, even if I pull N out of the database at once, because more might turn up while I'm sending those N. So I might as well just do one at a time and save some complexity.

Is there significance to which request is selected (FIFO/LIFO/arbitrary?).

well, it's done by order of pkey - ie, request_id - so is more-or-less FIFO. But basically, I'm assuming that arbitrary order is good enough.

if (this._sendOutgoingRoomKeyRequestsRunning) {
throw new Error("RoomKeyRequestSend already in progress!");
}
this._sendOutgoingRoomKeyRequestsRunning = true;
Copy link
Member

Choose a reason for hiding this comment

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

Is this state variable strictly required? Can the presence of _sendOutgoingRoomKeyRequestsTimer be enough of an indicator? It's preferable to reduce the number of states your state machine can be in, so if we can get by without this and still have it be clear I would prefer it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not required - it is strictly a sanity check, as per https://github.com/matrix-org/matrix-js-sdk/pull/448/files#diff-d8c620291b1726321a300d1e99bb1256R53.

I see your point that it is adding complexity. I still think it is a net improvement, but will remove it if you feel strongly.

Copy link
Member

Choose a reason for hiding this comment

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

I don't feel strongly.

}
}

function stringifyRequestBody(requestBody) {
Copy link
Member

@kegsay kegsay Jun 2, 2017

Choose a reason for hiding this comment

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

If requestBody was an actual type you could leverage the built-in toString method so you don't need to explicitly stringify here. Might be nice.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, but sometimes requestBodies come out of the db (as sub-objects of requests), so making it into an actual class is non-trivial.

Copy link
Member

Choose a reason for hiding this comment

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

OK.

this._outgoingRoomKeyRequestManager.makeRoomKeyRequest(
requestBody, recipients,
).catch((e) => {
// this normally means we couldn't talk to the store */
Copy link
Member

Choose a reason for hiding this comment

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

Stray */

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed, ta

* @returns {Promise} resolves when the request has been added to the
* pending list
*/
makeRoomKeyRequest(requestBody, recipients) {
Copy link
Member

Choose a reason for hiding this comment

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

Naming: I dislike the name makeRoomKeyRequest because it sounds to me like it is constructing a request rather than sending a request. The docs are clear but at call-sites I find it confusing, see here for an example. Can we s/make/send/ please?

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed. done.

this._decryptEvent(event, true);
};

MegolmDecryption.prototype._decryptEvent = function(event, requestKeysOnFail) {
Copy link
Member

@kegsay kegsay Jun 2, 2017

Choose a reason for hiding this comment

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

I understand this is an internal method, but I'd like there to be a bit of prose by what you mean by "fail" on requestKeysOnFail. I see this exclusively means if the error message is OLM.UNKNOWN_MESSAGE_INDEX or res === null but that's as clear as mud.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -543,6 +547,9 @@ MegolmDecryption.prototype.decryptEvent = function(event) {
} catch (e) {
if (e.message === 'OLM.UNKNOWN_MESSAGE_INDEX') {
this._addEventToPendingList(event);
if (requestKeysOnFail) {
this._requestKeysForEvent(event);
}
Copy link
Member

Choose a reason for hiding this comment

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

We don't recover at this point, we just throw as if requestKeysOnFail == false so from the caller's perspective they have no idea whether they will ever be able to decrypt this message. This might be fine? I don't know how people are using MegolmDecryption.prototype.decryptEvent currently.

Copy link
Member Author

Choose a reason for hiding this comment

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

well, it's fine for now. Until the key turns up it is just an undecryptable event.

decryptEvent is called here which in turn called here.

I was toying with setting a flag on the event to show that its key had been requested or something, but I didn't need it yet.

Copy link
Member

Choose a reason for hiding this comment

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

OK.

@@ -657,7 +689,7 @@ MegolmDecryption.prototype._retryDecryption = function(senderKey, sessionId) {

for (let i = 0; i < pending.length; i++) {
try {
this.decryptEvent(pending[i]);
this.decryptEvent(pending[i], false);
Copy link
Member

Choose a reason for hiding this comment

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

Wrong method call. Should be this._decryptEvent. Also, it would be good to explain why you're passing false here, presumably because we're retrying once we've got the keys, so getting the keys again isn't going to make a difference.

Copy link
Member Author

Choose a reason for hiding this comment

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

done, thanks.

* @property {string} requestId unique id for this request
*
* @property {string?} cancellationRequestId
* transaction id for the cancellation, if any
Copy link
Member

@kegsay kegsay Jun 2, 2017

Choose a reason for hiding this comment

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

Terminology: The variable states its a request ID, but the comment says it's a transaction ID. Is there a difference? If not, can we stick to one term throughout please, or else I get confused.

Copy link
Member Author

Choose a reason for hiding this comment

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

this one is certainly a transaction id. I can change that.

The other one (requestId) is both: we use it both as a request id within the message as a unique identifier for the recipient to correlate with the cancellation when it arrives, and as the transaction_id for sending the to-device message.

Copy link
Member

Choose a reason for hiding this comment

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

It might be worth mentioning that on :18 then when describing what requestId is then, since I didn't manage to grok that from looking at the code alone.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok. Added.

*
* @typedef {Object} OutgoingRoomKeyRequest
*
* @property {string} requestId unique id for this request
Copy link
Member

Choose a reason for hiding this comment

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

The usual questions I ask whenever I see people add in IDs:

  • Is there a min length? Max length?
  • Can I use smiley poo? Spaces?

Copy link
Member Author

Choose a reason for hiding this comment

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

it's an internal method. I don't think this needs documenting.

Copy link
Member

Choose a reason for hiding this comment

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

This goes over the wire as request_id - so either link to the docs for the wire protocol or state the protocol here please. There's no other way for me to know what is valid and what is not otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

In absence of this being in the spec, please add a link to the gdoc so readers know where to go to find the wire protocol.

Copy link
Member Author

@richvdh richvdh Jun 5, 2017

Choose a reason for hiding this comment

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

Added at the top of OutgoingKeyRequestManager.js

/**
* The parameters of a room key request. The details of the request may
* vary with the crypto algorithm, but the management and storage layers for
* outgoing requests expect it to have 'room_id' and 'session_id' properties.
Copy link
Member

Choose a reason for hiding this comment

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

the management and storage layers for outgoing requests expect it to have 'room_id' and 'session_id' properties.

Is this just a long way of saying:

@typedef {Object} RoomKeyRequestBody
@prop {string} room_id
@prop {string} session_id

Other keys are determined by the precise crypto algorithm used.

right? There may be value in @proping up the mandatory fields.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe, but I think it gives more emphasis to the presence of those fields than is entirely justified. The management layer only uses them for debug, and the storage layer just uses them as keys in the indexeddb for efficiency. Both should be perfectly happy with them being absent.

Copy link
Member

Choose a reason for hiding this comment

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

Okay then.

);
deferred.resolve(existing);
},
onNotFound: () => {
Copy link
Member

Choose a reason for hiding this comment

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

API shape feels weird. Is there any reason why we don't invoke a single function and give a null parameter if it was not found?

Copy link
Member Author

Choose a reason for hiding this comment

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

good question. fixed.

*
* @return {Promise} resolves to the a
* {@link module:crypto/store/base~OutgoingRoomKeyRequest}, or null if
* there are no pending requests in those states
Copy link
Member

Choose a reason for hiding this comment

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

As per your comment on https://github.com/matrix-org/matrix-js-sdk/pull/448/files#r119830241 - I would mention here that if there are multiple matching requests, an arbitrary request is chosen.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

*/
getOutgoingRoomKeyRequestByState(wantedStates) {
if (wantedStates.length === 0) {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

The JSDoc clearly states this function will always return a Promise. Please wrap this up.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@kegsay kegsay assigned richvdh and unassigned kegsay Jun 5, 2017
@richvdh
Copy link
Member Author

richvdh commented Jun 5, 2017

@kegsay ptal?

@kegsay
Copy link
Member

kegsay commented Jun 5, 2017

I'd like for you to add a link to https://docs.google.com/document/d/1m4gQkcnJkxNuBmb5NoFCIadIY-DyqqNAS3lloE73BlQ/edit#heading=h.2rxyh346wc9c somewhere so people can see the wire protocol that this code is supposed to be implementing. I don't need to see the code afterwards.

Other than that, LGTM!

@richvdh
Copy link
Member Author

richvdh commented Jun 5, 2017

I think this is ready to land, but blocked on the un-reversion of matrix-org/matrix-react-sdk#983, which is blocked on the landing of the guest access branch

@richvdh
Copy link
Member Author

richvdh commented Jun 6, 2017

I'm going to land this and #449 on a feature branch, until we have a fix in place for matrix-org/matrix-react-sdk#983 and have the rest of this feature in place.

@richvdh richvdh changed the base branch from develop to room_key_sharing June 6, 2017 13:20
When we are missing the keys to decrypt an event, send out a request for those
keys to our other devices and to the original sender.
@richvdh richvdh force-pushed the rav/send_room_key_requests branch from c48fbaa to ea2a041 Compare June 6, 2017 13:25
@richvdh richvdh merged commit ff323d0 into room_key_sharing Jun 6, 2017
@richvdh richvdh deleted the rav/send_room_key_requests branch June 6, 2017 13:25
ylecollen pushed a commit to matrix-org/matrix-android-sdk that referenced this pull request Oct 26, 2017
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.

2 participants