forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.rs
More file actions
152 lines (140 loc) · 4.29 KB
/
Copy pathrpc.rs
File metadata and controls
152 lines (140 loc) · 4.29 KB
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
use chrono::{DateTime, Utc};
use mongodb::bson::oid::ObjectId;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RpcLog {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub user_id: ObjectId,
pub method: String,
pub params: Value,
pub timestamp: DateTime<Utc>,
pub success: bool,
pub error: Option<String>,
}
impl RpcLog {
pub fn new(
user_id: ObjectId,
method: String,
params: Value,
success: bool,
error: Option<String>,
) -> Self {
Self {
id: None,
user_id,
method,
params: Self::sanitize_params(¶ms),
timestamp: Utc::now(),
success,
error,
}
}
fn sanitize_params(params: &Value) -> Value {
match params {
Value::Object(map) => {
let mut sanitized = Map::new();
for (key, value) in map {
if Self::is_sensitive_key(key) {
sanitized.insert(key.clone(), Value::String("[REDACTED]".to_string()));
} else {
sanitized.insert(key.clone(), Self::sanitize_params(value));
}
}
Value::Object(sanitized)
}
Value::Array(values) => {
Value::Array(values.iter().map(Self::sanitize_params).collect())
}
Value::String(s) => {
if Self::looks_like_sensitive_data(s) {
Value::String("[REDACTED]".to_string())
} else {
Value::String(s.clone())
}
}
other => other.clone(),
}
}
fn is_sensitive_key(key: &str) -> bool {
let normalized = key.to_ascii_lowercase();
normalized.contains("private")
|| normalized.contains("secret")
|| normalized.contains("token")
|| normalized.contains("key")
|| normalized.contains("password")
|| normalized.contains("signature")
|| normalized.contains("tx")
|| normalized.contains("transaction")
|| normalized.contains("seed")
}
fn looks_like_sensitive_data(value: &str) -> bool {
let trimmed = value.trim();
if trimmed.is_empty() {
return false;
}
let lower = trimmed.to_ascii_lowercase();
lower.starts_with("0x") && trimmed.len() > 64
|| lower.contains("private")
|| lower.contains("secret")
|| lower.contains("password")
|| lower.contains("bearer")
|| lower.contains("authorization")
|| lower.contains("eyj")
|| lower.contains("-----begin")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redacts_sensitive_rpc_params_before_storage() {
let params = serde_json::json!({
"method": "eth_sendRawTransaction",
"params": [{
"rawTransaction": "0xabc1234567890abcdef",
"password": "supersecret"
}],
"network": "mainnet"
});
let log = RpcLog::new(
ObjectId::new(),
"eth_sendRawTransaction".to_string(),
params,
true,
None,
);
let params = log.params;
assert_eq!(
params["params"][0]["rawTransaction"],
Value::String("[REDACTED]".to_string())
);
assert_eq!(
params["params"][0]["password"],
Value::String("[REDACTED]".to_string())
);
assert_eq!(params["network"], Value::String("mainnet".to_string()));
}
#[test]
fn preserves_non_sensitive_values() {
let params = serde_json::json!({
"method": "eth_blockNumber",
"params": []
});
let log = RpcLog::new(
ObjectId::new(),
"eth_blockNumber".to_string(),
params,
true,
None,
);
assert_eq!(
log.params,
serde_json::json!({
"method": "eth_blockNumber",
"params": []
})
);
}
}