Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Add functions in sanitization to handler ProtocolRPCRequest and ProtocolMessage - Closes#1017 #1020

Merged

Conversation

ishantiw
Copy link
Contributor

Description

Add sanitization functions to handle protocol RPC requests and messages

Review checklist

@@ -92,3 +97,32 @@ export const sanitizePeerInfoList = (
throw new InvalidRPCResponse('Invalid response type');
}
};

export const sanitizeRPCRequest = (request: unknown): ProtocolRPCRequest => {
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of sanitize, can we use validate?

@@ -69,3 +69,17 @@ export class RequestFailError extends VError {
this.name = 'RequestFailError';
}
}

export class InvalidRPCRequest extends VError {
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer InvalidRPCRequestError

}
}

export class InvalidProtocolMessage extends VError {
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer InvalidProtocolMessageError

@ishantiw ishantiw force-pushed the 1017-add_more_funcs_in_sanitization_to_handler branch 2 times, most recently from a92cadf to e69a997 Compare January 21, 2019 13:46
@ishantiw ishantiw force-pushed the 1017-add_more_funcs_in_sanitization_to_handler branch from 94a7c5e to 463eb33 Compare January 21, 2019 13:58
@ishantiw ishantiw force-pushed the 1017-add_more_funcs_in_sanitization_to_handler branch from 615b99b to 4bd298d Compare January 21, 2019 14:20
@ishantiw
Copy link
Contributor Author

ishantiw commented Jan 21, 2019

Addressed all the comments and rebased. Also, I have included the changes related to socketcluster-server library in #1021, where I forgot to upgrade the version of the library

@ishantiw ishantiw force-pushed the 1017-add_more_funcs_in_sanitization_to_handler branch from 4bd298d to c3883cb Compare January 21, 2019 14:24
@ishantiw ishantiw force-pushed the 1017-add_more_funcs_in_sanitization_to_handler branch from 8d748e1 to ef16b96 Compare January 21, 2019 14:39

return;
this.emit(EVENT_REQUEST_RECEIVED, request);

Choose a reason for hiding this comment

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

It's dangerous to emit within a try block because event listeners which are declared outside this class will be called synchronously; so if a listener is attached to the Peer from outside; for example using peer.on(EVENT_REQUEST_RECEIVED, myListenerFunction); it means that if myListenerFunction throws an error or has a bug then it will cause the catch block below to execute and this.emit(EVENT_INVALID_REQUEST_RECEIVED, packet); will be called even though the request is fine; makes it very hard to debug.

An alternative approach might be to return from inside the catch block just after calling this.emit(EVENT_INVALID_REQUEST_RECEIVED, packet); and on the next line outside the catch block (which will only run if request is valid), we can emit the request with this.emit(EVENT_REQUEST_RECEIVED, request);.

It's easy to do with let but may be kind of tricky to do with only const. Maybe it's worth separating the validation logic from the sanitization logic and only have the try-catch block around the validation part?

Another option would be to emit asynchronously (e.g. inside a setTimeout/setImmediate) but that is more ugly and computationally expensive.

return;
try {
const protocolMessage = sanitizeProtocolMessage(packet);
this.emit(EVENT_MESSAGE_RECEIVED, protocolMessage);

Choose a reason for hiding this comment

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

Same problem as above.

throw new InvalidRPCRequestError('Invalid request');
}

const rpcRequest = request as P2PRequest;
Copy link

@jondubois jondubois Jan 21, 2019

Choose a reason for hiding this comment

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

We can't cast the request object into a P2PRequest because P2PRequest is a usable request object which can be responded to using its request.end(...) or request.error(...) method. Maybe this validation/sanitization function should just return a validated/sanitized ProtocolRPCRequestPacket; that's also more consistent with the sanitizeProtocolMessage function below. We will have to instantiate the P2PRequest as a separate step (not part of validation/sanitization); it needs the SocketCluster respond callback as third argument to its constructor.

return rpcRequest;
};

export const sanitizeProtocolMessage = (message: unknown): ProtocolMessagePacket => {
Copy link
Contributor

Choose a reason for hiding this comment

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

name of this should also be validate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually before, all the functions were prefixed as process or validate. But later @jondubois renamed all of them to sanitize including the file name because these functions are not only validating but type casting or sanitizing it to some protocol type or preferred data structure.

Copy link
Contributor

Choose a reason for hiding this comment

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

Mmm we have similar functionality in transaction with validating and the checking so maybe it’s better to standardize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay then I think it makes sense to just use validate here too 👍

@@ -14,8 +14,14 @@
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

also, filename should be consistent, and all the function in this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

same here, should we rename it back to validate prefix? what do you think @jondubois ?

@ishantiw ishantiw force-pushed the 1017-add_more_funcs_in_sanitization_to_handler branch from 3c419c0 to 9461ea0 Compare January 22, 2019 15:01
@ishantiw
Copy link
Contributor Author

Addressed @jondubois @shuse2

@shuse2 shuse2 merged commit 5a82de1 into add_p2p_experiment Jan 22, 2019
@shuse2 shuse2 deleted the 1017-add_more_funcs_in_sanitization_to_handler branch January 22, 2019 22:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants