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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::io;
use clap::{Args, Parser};
use clap_cargo::style::CLAP_STYLING;
use clap_verbosity_flag::{InfoLevel, Verbosity};
use color_eyre::Result;
use color_eyre::eyre::{Context, bail};
use colored_json::ToColoredJson;
use tracing::{debug, warn};
use webfinger_rs::{Rel, Resource, WebFingerRequest};
/// A simple CLI for fetching webfinger resources
#[derive(Debug, Parser)]
#[clap(styles = CLAP_STYLING)]
struct Cli {
#[command(flatten)]
fetch_command: FetchCommand,
#[command(flatten)]
verbosity: Verbosity<InfoLevel>,
}
#[derive(Debug, Args, PartialEq, Eq)]
#[command(next_line_help = false)]
struct FetchCommand {
/// The resource to fetch
///
/// E.g. `acct:user@example.com"
resource: String,
/// The host to fetch the webfinger resource from
///
/// This defaults to the host part of the resource
host: Option<String>,
/// The link relation types to fetch
///
/// E.g. `http://webfinger.net/rel/profile-page`
///
/// This can be specified multiple times
#[arg(short, long)]
rel: Vec<String>,
/// Ignore TLS certificate verification errors
#[arg(long)]
insecure: bool,
}
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
let args = Cli::parse();
tracing_subscriber::fmt()
.with_max_level(args.verbosity)
.with_writer(io::stderr)
.init();
args.fetch_command.execute().await?;
Ok(())
}
impl FetchCommand {
async fn execute(&self) -> Result<()> {
let resource = self.resource()?;
let request = WebFingerRequest {
host: self.host(&resource)?,
resource,
rels: self.link_relations()?,
};
debug!("fetching webfinger resource: {:?}", request);
if self.insecure {
warn!("ignoring TLS certificate verification errors");
}
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
let client = reqwest::Client::builder()
.danger_accept_invalid_certs(self.insecure)
.build()?;
let response = request.execute_reqwest_with_client(&client).await?;
let json = response.to_string().to_colored_json_auto()?;
println!("{json}");
Ok(())
}
fn host(&self, resource: &Resource) -> Result<String> {
if let Some(host) = self.host.as_deref() {
Ok(host.to_string())
} else {
if let Some(host) = resource.host() {
debug!("extracted host from resource URI: {}", host);
Ok(host.to_string())
} else if let Some((_, host)) = self.resource.split_once('@') {
// TODO normalize account-address hosts before constructing the request URI.
debug!("extracted host from acct resource: {}", host);
Ok(host.to_string())
} else {
bail!("no host provided")
}
}
}
fn resource(&self) -> Result<Resource> {
self.resource.parse().wrap_err("invalid resource")
}
fn link_relations(&self) -> Result<Vec<Rel>> {
self.rel
.iter()
.map(Rel::try_new)
.collect::<std::result::Result<Vec<_>, _>>()
.wrap_err("invalid relation type")
}
}
#[cfg(test)]
mod tests {
use super::*;
fn command(resource: &str) -> FetchCommand {
FetchCommand {
resource: resource.to_string(),
host: None,
rel: Vec::new(),
insecure: false,
}
}
/// Uses the URI authority when the resource has one.
///
/// WebFinger resources are not always `acct:` URIs. Parsing the URI before falling back to
/// `acct:` splitting prevents `@` inside an HTTPS path from being mistaken for the request host.
///
/// See <https://www.rfc-editor.org/rfc/rfc7033.html#section-4.1>.
#[test]
fn host_uses_resource_uri_authority() {
let command = command("https://example.org/users/@carol");
let resource = command.resource().unwrap();
let host = command.host(&resource).unwrap();
assert_eq!(host, "example.org");
}
/// Falls back to the account authority for `acct:` resources.
///
/// `acct:` URIs do not expose a URI host through the `http::Uri` API, so the CLI keeps the
/// WebFinger account-address fallback for the common `acct:user@example.org` case.
///
/// See <https://www.rfc-editor.org/rfc/rfc7565.html#section-3>.
#[test]
fn host_falls_back_to_acct_authority() {
let command = command("acct:carol@example.org");
let resource = command.resource().unwrap();
let host = command.host(&resource).unwrap();
assert_eq!(host, "example.org");
}
/// Honors an explicit CLI host over any host that could be inferred from the resource.
///
/// WebFinger deployments can serve an account domain from a different endpoint host, so the
/// caller-provided host must remain authoritative when present.
#[test]
fn host_uses_explicit_host() {
let mut command = command("acct:carol@example.org");
command.host = Some("webfinger.example.net".to_string());
let resource = command.resource().unwrap();
let host = command.host(&resource).unwrap();
assert_eq!(host, "webfinger.example.net");
}
/// Rejects host inference when neither an HTTP(S) authority nor an `acct:` authority is
/// available.
///
/// RFC 7033 section 4.1 requires a concrete WebFinger endpoint host for the outgoing query.
/// Without an explicit CLI host, only hierarchical resource URIs and `acct:` account addresses
/// provide enough information to infer that endpoint.
///
/// See <https://www.rfc-editor.org/rfc/rfc7033.html#section-4.1>.
/// See <https://www.rfc-editor.org/rfc/rfc7565.html#section-3>.
#[test]
fn host_rejects_resource_without_inferable_host() {
let command = command("mailto:carol");
let resource = command.resource().unwrap();
let error = command.host(&resource).expect_err("missing host");
assert_eq!(error.to_string(), "no host provided");
}
/// Parses all relation filters through the same validated `Rel` boundary used by the library.
///
/// Repeated `--rel` options become repeated WebFinger `rel` query parameters, so their order is
/// observable in the generated request URI.
#[test]
fn link_relations_parse_in_order() {
let mut command = command("acct:carol@example.org");
command.rel = vec![
"http://webfinger.net/rel/profile-page".to_string(),
"avatar".to_string(),
];
let rels = command.link_relations().unwrap();
assert_eq!(
rels.iter().map(Rel::as_ref).collect::<Vec<_>>(),
["http://webfinger.net/rel/profile-page", "avatar"]
);
}
/// Rejects invalid CLI relation filters before a network request can be built.
///
/// This keeps CLI validation consistent with response and request relation validation instead
/// of letting malformed relation strings reach the transport layer.
#[test]
fn link_relations_reject_invalid_rel() {
let mut command = command("acct:carol@example.org");
command.rel = vec!["profile page".to_string()];
let error = command.link_relations().expect_err("invalid rel");
assert!(error.to_string().contains("invalid relation type"));
}
/// Stops invalid resource text before the CLI constructs a WebFinger request.
///
/// The command path should fail with local validation context, not with a later Reqwest URL or
/// transport error that hides the malformed resource.
#[tokio::test]
async fn execute_rejects_invalid_resource_before_network() {
let command = command("http:foo");
let error = command.execute().await.expect_err("invalid resource");
assert!(error.to_string().contains("invalid resource"));
}
/// Stops execution when the endpoint host cannot be explicit or inferred.
///
/// This covers the full async command path for the same missing-host boundary tested directly
/// by `host_rejects_resource_without_inferable_host`.
#[tokio::test]
async fn execute_rejects_missing_host_before_network() {
let command = command("mailto:carol");
let error = command.execute().await.expect_err("missing host");
assert_eq!(error.to_string(), "no host provided");
}
/// Validates relation filters before initializing the HTTP client.
///
/// A bad `--rel` value should be reported as command input failure even when the resource and
/// host are otherwise usable.
#[tokio::test]
async fn execute_rejects_invalid_relation_before_network() {
let mut command = command("acct:carol@example.org");
command.host = Some("example.org".to_string());
command.rel = vec!["profile page".to_string()];
let error = command.execute().await.expect_err("invalid rel");
assert!(error.to_string().contains("invalid relation type"));
}
/// Locks the positional CLI shape for resource, optional host, repeated rels, and TLS override.
///
/// Clap derives this parser from struct field order and attributes, so a small parser refactor
/// can unintentionally change the public command line.
#[test]
fn cli_parses_resource_host_rel_and_insecure_flag() {
let cli = Cli::try_parse_from([
"webfinger",
"acct:carol@example.org",
"example.org",
"--rel",
"avatar",
"--insecure",
])
.unwrap();
assert_eq!(
cli.fetch_command,
FetchCommand {
resource: "acct:carol@example.org".to_string(),
host: Some("example.org".to_string()),
rel: vec!["avatar".to_string()],
insecure: true,
}
);
}
/// Rejects non-hierarchical HTTP resource text before host inference.
#[test]
fn resource_rejects_http_uri_without_authority() {
let command = command("http:foo");
let error = command
.resource()
.expect_err("HTTP resource without authority");
assert!(error.to_string().contains("invalid resource"));
}
}