Skip to content

Commit 68bcc3d

Browse files
authored
feat: Validate MessageId's (rpc_id) (#23)
1 parent 6483d20 commit 68bcc3d

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

relay_client/src/client/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl ClientStream {
175175
Payload::Response(response) => {
176176
let id = response.id();
177177

178-
if !id.is_valid() {
178+
if id.is_zero() {
179179
return match response {
180180
Response::Error(response) => {
181181
Some(StreamEvent::InboundError(Error::Rpc {

relay_rpc/src/domain.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,18 @@ new_type!(
143143
);
144144

145145
impl MessageId {
146-
pub fn is_valid(&self) -> bool {
146+
/// Minimum allowed value of a [`MessageId`].
147+
const MIN: Self = Self(1000000000);
148+
149+
pub(crate) fn validate(&self) -> bool {
150+
self.0 >= Self::MIN.0
151+
}
152+
153+
pub fn is_zero(&self) -> bool {
147154
// Message ID `0` is used when the client request failed to parse for whatever
148155
// reason, and the server doesn't know the message ID of that request, but still
149156
// wants to communicate the error.
150-
self.0 != 0
157+
self.0 == 0
151158
}
152159
}
153160

relay_rpc/src/rpc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub enum ValidationError {
3737
#[error("Subscription ID decoding failed: {0}")]
3838
SubscriptionIdDecoding(DecodingError),
3939

40+
#[error("Invalid request ID")]
41+
RequestId,
42+
4043
#[error("Invalid JSON RPC version")]
4144
JsonRpcVersion,
4245

@@ -701,6 +704,10 @@ impl Request {
701704

702705
/// Validates the request payload.
703706
pub fn validate(&self) -> Result<(), ValidationError> {
707+
if !self.id.validate() {
708+
return Err(ValidationError::RequestId);
709+
}
710+
704711
if self.jsonrpc.as_ref() != JSON_RPC_VERSION_STR {
705712
return Err(ValidationError::JsonRpcVersion);
706713
}

relay_rpc/src/rpc/tests.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,27 @@ fn deserialize_batch_methods() {
194194
#[test]
195195
fn validation() {
196196
// Valid data.
197-
let id = MessageId::from(1);
197+
let id = MessageId::from(1234567890);
198198
let jsonrpc: Arc<str> = "2.0".into();
199199
let message: Arc<str> = "0".repeat(512).into();
200200
let topic = Topic::from("c4163cf65859106b3f5435fc296e7765411178ed452d1c30337a6230138c9840");
201201
let subscription_id =
202202
SubscriptionId::from("c4163cf65859106b3f5435fc296e7765411178ed452d1c30337a6230138c9841");
203203

204+
// Invalid request ID.
205+
let request = Request {
206+
id: MessageId::new(1),
207+
jsonrpc: jsonrpc.clone(),
208+
params: Params::Publish(Publish {
209+
topic: topic.clone(),
210+
message: message.clone(),
211+
ttl_secs: 0,
212+
tag: 0,
213+
prompt: false,
214+
}),
215+
};
216+
assert_eq!(request.validate(), Err(ValidationError::RequestId));
217+
204218
// Invalid JSONRPC version.
205219
let request = Request {
206220
id,

0 commit comments

Comments
 (0)