Skip to content

Commit cc133a0

Browse files
committed
Add a useragent header when fetching data + print response if not JSON
This commit introduces two fixes/QoL changes. First off, it adds a useragent to every request sent by this tool. This is required iff the remote server rejects requests without any specific useragent being set (an example for this is Wikimedia Toolforge, which will drop all HTTP requests without a User-Agent header See: https://wikitech.wikimedia.org/wiki/Help:Toolforge/Banned#Banned_user_agents_and_IP_addresses Additionally the second change introduced prints the body of the response into stderr iff the tool encounters a error in parsing the JSON data into JWKS sets. This is particularly useful because it (~~allowed me to debug the error above~~) will help folks figure out exactly what data got sent back from the server and why the tool might not be able to validate the keys.
1 parent 84b73ab commit cc133a0

File tree

1 file changed

+28
-4
lines changed
  • crates/http-signature-directory/src

1 file changed

+28
-4
lines changed

crates/http-signature-directory/src/main.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use clap::Parser;
66
use log::{debug, info};
77
use reqwest::{
8-
Url,
9-
blocking::Client,
10-
header::{ACCEPT, CONTENT_TYPE},
8+
blocking::Client, header::{ACCEPT, CONTENT_TYPE, USER_AGENT}, Url
119
};
1210
use serde::{Deserialize, Serialize};
1311
use web_bot_auth::{
@@ -130,6 +128,7 @@ fn main() -> Result<(), String> {
130128
let response = match Client::new()
131129
.get(cli.url.clone())
132130
.header(ACCEPT, MIME_TYPE)
131+
.header(USER_AGENT, "http-signature-directory-test-script/0.1.0")
133132
.send()
134133
{
135134
Ok(response) => response,
@@ -189,8 +188,29 @@ fn main() -> Result<(), String> {
189188
signature_inputs
190189
);
191190

191+
let body_text = match response.text() {
192+
Ok(text) => text,
193+
Err(err) => {
194+
let error_msg = format!("Failed to read response body: {:?}", err);
195+
errors.push(error_msg.clone());
196+
let result = ValidationResult {
197+
success: false,
198+
message: error_msg.clone(),
199+
details: ValidationDetails {
200+
url: cli.url.clone(),
201+
keys_count: 0,
202+
validated_keys: vec![],
203+
errors,
204+
warnings,
205+
},
206+
};
207+
eprintln!("{}", serde_json::to_string_pretty(&result).unwrap());
208+
return Err("Body read failed".to_string());
209+
}
210+
};
211+
192212
// Parse JSON Web Key Set
193-
let json_web_key_set: JSONWebKeySet = match response.json() {
213+
let json_web_key_set: JSONWebKeySet = match serde_json::from_str(&body_text) {
194214
Ok(jwks) => jwks,
195215
Err(error) => {
196216
let error_msg = format!("Failed to parse content as JSON web key set: {:?}", error);
@@ -207,6 +227,10 @@ fn main() -> Result<(), String> {
207227
},
208228
};
209229
eprintln!("{}", serde_json::to_string_pretty(&result).unwrap());
230+
eprintln!("This was the data the server sent back:");
231+
eprintln!("--- RAW RESPONSE BODY START ---");
232+
println!("{}", body_text);
233+
eprintln!("--- RAW RESPONSE BODY END ---");
210234
return Err("JSON parsing failed".to_string());
211235
}
212236
};

0 commit comments

Comments
 (0)