Skip to content

Commit 33fb84d

Browse files
committed
chore: grab json value from object given path
1 parent 8a0a32d commit 33fb84d

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ If you have any questions, please reach out to any of Pluto's [team members](htt
3737
### Usage
3838

3939
```
40-
cargo run -p notary -- --config ./fixture/notary-config.toml
41-
cargo run -p client -- --config ./fixture/client.proxy.json
40+
cargo run -p web-prover-notary -- --config ./fixture/notary-config.toml
41+
cargo run -p web-prover-client -- --config ./fixture/client.proxy.json
4242
```
4343

4444
## Security Status

notary/src/error.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,21 @@ pub enum NotaryServerError {
4444
#[error("Error occurred from reasing server config: {0}")]
4545
ServerConfigError(String),
4646

47-
// TODO: Update to contain feedback
48-
#[error("Manifest-request mismatch")]
47+
#[error("Manifest request mismatch")]
4948
ManifestRequestMismatch,
5049

51-
// TODO: Update to contain feedback
52-
#[error("Manifest-response mismatch")]
50+
#[error("Manifest response mismatch")]
5351
ManifestResponseMismatch,
5452

53+
#[error("Manifest missing")]
54+
ManifestMissing,
55+
56+
#[error("Missing Origo secrets")]
57+
MissingOrigoSecrets,
58+
59+
#[error("JSON path not found")]
60+
JsonPathNotFound,
61+
5562
#[error(transparent)]
5663
ProxyError(#[from] ProxyError),
5764

notary/src/proxy.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ use axum::{
77
use reqwest::{Request, Response};
88
use serde::Deserialize;
99
use serde_json::Value;
10-
use tracing::info;
10+
use tracing::{debug, info};
1111
use uuid::Uuid;
1212
use web_prover_core::{
1313
http::{
14-
ManifestRequest, ManifestResponse, ManifestResponseBody, NotaryResponse, NotaryResponseBody,
14+
JsonKey, ManifestRequest, ManifestResponse, ManifestResponseBody, NotaryResponse,
15+
NotaryResponseBody,
1516
},
1617
manifest::Manifest,
1718
proof::{TeeProof, TeeProofData},
@@ -57,7 +58,12 @@ pub async fn proxy(
5758
let response = from_reqwest_response(reqwest_response).await;
5859
// debug!("{:?}", response);
5960

61+
if !response.matches_client_manifest(&payload.manifest.response) {
62+
return Err(NotaryServerError::ManifestResponseMismatch);
63+
}
64+
6065
let tee_proof = create_tee_proof(&payload.manifest, &request, &response, State(state))?;
66+
debug!("{:?}", tee_proof);
6167

6268
Ok(Json(tee_proof))
6369
}
@@ -122,8 +128,9 @@ pub fn create_tee_proof(
122128
State(state): State<Arc<SharedState>>,
123129
) -> Result<TeeProof, NotaryServerError> {
124130
validate_notarization_legal(manifest, request, response)?;
125-
126-
let value = response.notary_response_body.clone().json.unwrap();
131+
let path = manifest.response.body.json_path.clone();
132+
let body = response.notary_response_body.clone().json.unwrap();
133+
let value = get_value_from_json_path(&body, &path);
127134
let serialized_value = serde_json::to_string(&value).unwrap();
128135
let manifest_hash = manifest.to_keccak_digest()?;
129136
let to_sign = VerifyOutput { value: serialized_value.clone(), manifest: manifest.clone() };
@@ -149,3 +156,61 @@ fn validate_notarization_legal(
149156
}
150157
Ok(())
151158
}
159+
160+
fn get_value_from_json_path(json_body: &Value, path: &[JsonKey]) -> Value {
161+
let mut current = json_body;
162+
for key in path {
163+
current = match key {
164+
JsonKey::String(s) => current.get(s),
165+
JsonKey::Num(n) => current.get(n),
166+
}
167+
.unwrap();
168+
}
169+
current.clone()
170+
}
171+
172+
#[test]
173+
fn test_get_value_from_json_path() {
174+
let json_body = json!({
175+
"foo": {
176+
"bar": "baz"
177+
}
178+
});
179+
let path = vec![JsonKey::String("foo".to_string()), JsonKey::String("bar".to_string())];
180+
let value = get_value_from_json_path(&json_body, &path).unwrap();
181+
assert_eq!(value, "baz");
182+
}
183+
184+
#[test]
185+
fn test_get_value_from_json_path_num() {
186+
let json_body = json!({
187+
"foo": [1, 2, 3]
188+
});
189+
let path = vec![JsonKey::String("foo".to_string()), JsonKey::Num(1)];
190+
let value = get_value_from_json_path(&json_body, &path).unwrap();
191+
assert_eq!(value, 2);
192+
}
193+
194+
#[test]
195+
fn test_get_value_from_json_path_bool() {
196+
let json_body = json!({
197+
"foo": {
198+
"bar": true
199+
}
200+
});
201+
let path = vec![JsonKey::String("foo".to_string()), JsonKey::String("bar".to_string())];
202+
let value = get_value_from_json_path(&json_body, &path).unwrap();
203+
assert_eq!(value, true);
204+
}
205+
206+
#[test]
207+
fn test_get_value_from_json_path_null() {
208+
let json_body = json!({
209+
"foo": {
210+
"bar": null
211+
}
212+
});
213+
let path = vec![JsonKey::String("foo".to_string()), JsonKey::String("bar".to_string())];
214+
let value = get_value_from_json_path(&json_body, &path).unwrap();
215+
assert_eq!(value, Value::Null);
216+
}

0 commit comments

Comments
 (0)