Skip to content

Commit 55a90d4

Browse files
authored
Add a useragent header when fetching data + print response if not JSON (#59)
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 55a90d4

File tree

1 file changed

+32
-2
lines changed
  • crates/http-signature-directory/src

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use log::{debug, info};
77
use reqwest::{
88
Url,
99
blocking::Client,
10-
header::{ACCEPT, CONTENT_TYPE},
10+
header::{ACCEPT, CONTENT_TYPE, USER_AGENT},
1111
};
1212
use serde::{Deserialize, Serialize};
1313
use web_bot_auth::{
@@ -17,12 +17,16 @@ use web_bot_auth::{
1717
};
1818

1919
const MIME_TYPE: &str = "application/http-message-signatures-directory+json";
20+
const DEFAULT_USERAGENT: &str = "http-signature-directory-test-script/0.1.0";
2021

2122
#[derive(Parser)]
2223
#[command(version, about, long_about = None)]
2324
struct Cli {
2425
/// URL pointing to your HTTP Message Signature JSON Web Key Set e.g. `https://example.com/.well-known/http-message-signatures-directory`
2526
url: String,
27+
/// Optional useragent that can be used to customize the useragent being sent to the server.
28+
#[arg (default_value_t = String::from(DEFAULT_USERAGENT))]
29+
user_agent: String,
2630
}
2731

2832
#[derive(Serialize, Deserialize)]
@@ -130,6 +134,7 @@ fn main() -> Result<(), String> {
130134
let response = match Client::new()
131135
.get(cli.url.clone())
132136
.header(ACCEPT, MIME_TYPE)
137+
.header(USER_AGENT, cli.user_agent)
133138
.send()
134139
{
135140
Ok(response) => response,
@@ -189,8 +194,29 @@ fn main() -> Result<(), String> {
189194
signature_inputs
190195
);
191196

197+
let body_text = match response.text() {
198+
Ok(text) => text,
199+
Err(err) => {
200+
let error_msg = format!("Failed to read response body: {:?}", err);
201+
errors.push(error_msg.clone());
202+
let result = ValidationResult {
203+
success: false,
204+
message: error_msg.clone(),
205+
details: ValidationDetails {
206+
url: cli.url.clone(),
207+
keys_count: 0,
208+
validated_keys: vec![],
209+
errors,
210+
warnings,
211+
},
212+
};
213+
eprintln!("{}", serde_json::to_string_pretty(&result).unwrap());
214+
return Err("Body read failed".to_string());
215+
}
216+
};
217+
192218
// Parse JSON Web Key Set
193-
let json_web_key_set: JSONWebKeySet = match response.json() {
219+
let json_web_key_set: JSONWebKeySet = match serde_json::from_str(&body_text) {
194220
Ok(jwks) => jwks,
195221
Err(error) => {
196222
let error_msg = format!("Failed to parse content as JSON web key set: {:?}", error);
@@ -207,6 +233,10 @@ fn main() -> Result<(), String> {
207233
},
208234
};
209235
eprintln!("{}", serde_json::to_string_pretty(&result).unwrap());
236+
eprintln!("This was the data the server sent back:");
237+
eprintln!("--- RAW RESPONSE BODY START ---");
238+
println!("{}", body_text);
239+
eprintln!("--- RAW RESPONSE BODY END ---");
210240
return Err("JSON parsing failed".to_string());
211241
}
212242
};

0 commit comments

Comments
 (0)