tailfeather 0.3.0

Allocation-free, transport-agnostic building blocks for a Tailscale-compatible mesh node
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! `RegisterRequest`: how a node joins a tailnet.
//!
//! Sent as JSON over HTTP/2, inside the ts2021 Noise channel, to
//! `/machine/register`. The machine key is *not* in the body — the server knows
//! it already, because the Noise handshake authenticated it. What the body
//! carries is the node key, which is the data-plane identity being bound to that
//! machine, and the credential authorising the binding.

use crate::keys::NodePublic;

use crate::control::hostinfo::Hostinfo;
use crate::control::json::{JsonError, Writer};

/// Where the request goes.
pub const PATH: &str = "/machine/register";

/// Go renders a zero `time.Time` like this, and Headscale reads an absent or
/// zero expiry as "does not expire".
///
/// Spelled out rather than computed because there is no calendar here, and
/// because a node that accidentally sent a *past* expiry would register and then
/// immediately be logged out.
pub const NO_EXPIRY: &str = "0001-01-01T00:00:00Z";
/// Upstream Tailscale logs out by requesting an expiry far in the past.
pub const LOGOUT_EXPIRY: &str = "1970-01-01T00:02:03Z";

/// What a node sends to register.
pub struct RegisterRequest<'a> {
    /// Must match the version in the Noise prologue, or the server sees a client
    /// claiming two different capability levels.
    pub version: u16,
    pub node_key: &'a NodePublic,
    /// The key being replaced, when rotating. All zeros on a first
    /// registration, which is how the server tells the two apart.
    pub old_node_key: Option<&'a NodePublic>,
    /// A preauth key. Without one the server would answer with a URL for a
    /// human to visit, which a device with no screen cannot do anything with.
    pub auth_key: &'a str,
    pub hostinfo: Hostinfo<'a>,
    /// An ephemeral node is removed when it goes offline. A gateway is not one.
    pub ephemeral: bool,
}

impl RegisterRequest<'_> {
    /// Render the request body.
    pub fn write<'o>(&self, out: &'o mut [u8]) -> Result<&'o [u8], JsonError> {
        self.write_followup("", out)
    }

    /// Render the request body as a follow-up to an interactive login.
    ///
    /// When a registration without a usable auth key is answered with an
    /// `AuthURL`, the client repeats the request with that URL in `Followup`.
    /// The server then holds the response open until someone completes the
    /// login at that URL (or the request times out), and answers with the
    /// final `MachineAuthorized` state. An empty `followup` omits the field,
    /// which is exactly [`Self::write`].
    pub fn write_followup<'o>(
        &self,
        followup: &str,
        out: &'o mut [u8],
    ) -> Result<&'o [u8], JsonError> {
        self.write_with_expiry(followup, NO_EXPIRY, out)
    }

    /// Log out the current machine-authenticated identity without reusing its
    /// enrollment credential. An ephemeral coordinator node is removed now.
    pub fn write_logout<'o>(&self, out: &'o mut [u8]) -> Result<&'o [u8], JsonError> {
        let request = Self {
            version: self.version,
            node_key: self.node_key,
            old_node_key: None,
            auth_key: "",
            hostinfo: self.hostinfo,
            ephemeral: self.ephemeral,
        };
        request.write_with_expiry("", LOGOUT_EXPIRY, out)
    }

    fn write_with_expiry<'o>(
        &self,
        followup: &str,
        expiry: &str,
        out: &'o mut [u8],
    ) -> Result<&'o [u8], JsonError> {
        let mut key_text = [0u8; 128];
        let node_key = self
            .node_key
            .encode(&mut key_text)
            .map_err(|_| JsonError::Overflow)?;

        let mut old_key_text = [0u8; 128];
        let old_node_key = match self.old_node_key {
            Some(key) => key
                .encode(&mut old_key_text)
                .map_err(|_| JsonError::Overflow)?,
            // An all-zero key, which is what Go's zero value marshals to and
            // what the server reads as "there was no previous key".
            None => "nodekey:0000000000000000000000000000000000000000000000000000000000000000",
        };

        let mut writer = Writer::new(out);
        writer
            .begin_object()
            .field_u64("Version", self.version as u64)
            .field_str("NodeKey", node_key)
            .field_str("OldNodeKey", old_node_key)
            .field_str("Expiry", expiry)
            .field_bool("Ephemeral", self.ephemeral);
        // Headscale distinguishes an absent Auth object from an empty one.
        // Durable reconnects use the existing node identity; Auth:{AuthKey:""}
        // starts an interactive reauthentication instead of resuming that node.
        if !self.auth_key.is_empty() || !followup.is_empty() {
            writer
                .key("Auth")
                .begin_object()
                .field_str("AuthKey", self.auth_key)
                .end_object();
        }
        if !followup.is_empty() {
            writer.field_str("Followup", followup);
        }
        writer.key("Hostinfo");
        self.hostinfo.write(&mut writer);
        writer.end_object();
        writer.finish()
    }
}

/// The parts of a `RegisterResponse` a headless node can act on.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct RegisterResponse<'a> {
    /// Set once the node is a member of the tailnet.
    pub machine_authorized: bool,
    /// The control server requires the node key to be replaced before maps are requested.
    pub node_key_expired: bool,
    /// Who the node registered as. Headscale returns this as `Login.LoginName`;
    /// there is no domain field in the response, which is why this is not named
    /// after one.
    pub login_name: &'a str,
    /// Present when the server wants a human to visit a URL. For a device with
    /// no screen this is a failure, not a step — but it is reported rather than
    /// swallowed, because it is what an expired or already-used preauth key
    /// looks like.
    pub auth_url: &'a str,
    /// Set when the server refused.
    pub error: &'a str,
}

impl<'a> RegisterResponse<'a> {
    /// Pull the fields we act on out of the response body.
    ///
    /// Deliberately a scan for known keys rather than a parser: the response is
    /// a flat object of a few fields, and the real parser arrives with the
    /// netmap, which is the first document that needs one.
    pub fn parse(body: &'a str) -> Self {
        Self {
            machine_authorized: bool_field(body, "MachineAuthorized").unwrap_or(false),
            node_key_expired: bool_field(body, "NodeKeyExpired").unwrap_or(false),
            login_name: string_field(body, "LoginName").unwrap_or(""),
            auth_url: string_field(body, "AuthURL").unwrap_or(""),
            error: string_field(body, "Error").unwrap_or(""),
        }
    }

    /// Whether this response means the node is now a member.
    pub fn is_success(&self) -> bool {
        self.machine_authorized && self.error.is_empty()
    }
}

fn find_value<'a>(body: &'a str, field: &str) -> Option<&'a str> {
    let mut needle = heapless::String::<64>::new();
    needle.push('"').ok()?;
    needle.push_str(field).ok()?;
    needle.push('"').ok()?;
    let start = body.find(needle.as_str())? + needle.len();
    let rest = body[start..].trim_start().strip_prefix(':')?;
    Some(rest.trim_start())
}

fn string_field<'a>(body: &'a str, field: &str) -> Option<&'a str> {
    let rest = find_value(body, field)?.strip_prefix('"')?;
    let end = rest.find('"')?;
    Some(&rest[..end])
}

fn bool_field(body: &str, field: &str) -> Option<bool> {
    let rest = find_value(body, field)?;
    if rest.starts_with("true") {
        Some(true)
    } else if rest.starts_with("false") {
        Some(false)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::keys::NodePrivate;

    fn node_key() -> NodePublic {
        NodePrivate::from_bytes([0x11; 32]).public()
    }

    fn body<'a>(buffer: &'a mut [u8], request: &RegisterRequest<'_>) -> &'a str {
        core::str::from_utf8(request.write(buffer).unwrap()).unwrap()
    }

    #[test]
    fn durable_reconnect_omits_empty_auth_for_headscale() {
        let key = node_key();
        let request = RegisterRequest {
            version: 131,
            node_key: &key,
            old_node_key: None,
            auth_key: "",
            hostinfo: Hostinfo::default(),
            ephemeral: false,
        };
        let mut buffer = [0; 512];
        let text = body(&mut buffer, &request);
        assert!(!text.contains("\"Auth\""));
        assert!(!text.contains("AuthKey"));
        assert!(text.contains("NodeKey"));
    }
    #[test]
    fn logout_expires_the_owned_identity_without_an_enrollment_credential() {
        let key = node_key();
        let request = RegisterRequest {
            version: 131,
            node_key: &key,
            old_node_key: Some(&key),
            auth_key: "must-not-be-sent-on-logout",
            hostinfo: Hostinfo::default(),
            ephemeral: true,
        };
        let mut out = [0; 1024];
        let text = core::str::from_utf8(request.write_logout(&mut out).unwrap()).unwrap();
        assert!(text.contains(r#""Expiry":"1970-01-01T00:02:03Z""#));
        assert!(text.contains(r#""Ephemeral":true"#));
        assert!(!text.contains("Auth"));
        assert!(!text.contains("must-not-be-sent"));
        assert!(text.contains("NodeKey"));
    }

    #[test]
    fn a_first_registration_carries_the_node_key_and_the_preauth_key() {
        let key = node_key();
        let mut buffer = [0u8; 512];
        let text = body(
            &mut buffer,
            &RegisterRequest {
                version: 131,
                node_key: &key,
                old_node_key: None,
                auth_key: "hskey-auth-abc",
                hostinfo: Hostinfo::default(),
                ephemeral: false,
            },
        );

        assert!(text.contains(r#""Version":131"#));
        assert!(text.contains(r#""NodeKey":"nodekey:"#));
        assert!(text.contains(r#""AuthKey":"hskey-auth-abc""#));
        assert!(text.contains(r#""Ephemeral":false"#));
        assert!(text.contains(r#""Hostinfo":{"#));

        // The machine key must not appear: the Noise handshake already proved
        // it, and putting it in the body would invite trusting the body instead.
        assert!(!text.contains("mkey:"), "{text}");

        // A first registration says so with an all-zero previous key.
        assert!(text.contains(
            r#""OldNodeKey":"nodekey:0000000000000000000000000000000000000000000000000000000000000000""#
        ));
    }

    #[test]
    fn a_rotation_names_the_key_being_replaced() {
        let old = NodePrivate::from_bytes([0x22; 32]).public();
        let new = node_key();
        let mut buffer = [0u8; 512];
        let text = body(
            &mut buffer,
            &RegisterRequest {
                version: 131,
                node_key: &new,
                old_node_key: Some(&old),
                auth_key: "hskey-auth-abc",
                hostinfo: Hostinfo::default(),
                ephemeral: false,
            },
        );

        let mut expected = [0u8; 128];
        assert!(text.contains(old.encode(&mut expected).unwrap()));
        assert!(!text.contains("nodekey:00000000"));
    }

    #[test]
    fn an_exit_node_advertises_its_routes_in_the_registration() {
        let key = node_key();
        let mut buffer = [0u8; 512];
        let text = body(
            &mut buffer,
            &RegisterRequest {
                version: 131,
                node_key: &key,
                old_node_key: None,
                auth_key: "k",
                hostinfo: Hostinfo {
                    routable_ips: &crate::control::hostinfo::EXIT_NODE_ROUTES,
                    ..Default::default()
                },
                ephemeral: false,
            },
        );
        assert!(text.contains(r#""RoutableIPs":["0.0.0.0/0"]"#), "{text}");
    }

    #[test]
    fn a_followup_names_the_login_url_and_is_otherwise_unchanged() {
        let key = node_key();
        let request = RegisterRequest {
            version: 131,
            node_key: &key,
            old_node_key: None,
            auth_key: "",
            hostinfo: Hostinfo::default(),
            ephemeral: true,
        };
        let mut plain = [0u8; 768];
        let plain = core::str::from_utf8(request.write(&mut plain).unwrap()).unwrap();
        assert!(!plain.contains("Followup"), "{plain}");

        let mut buffer = [0u8; 768];
        let url = "https://login.tailscale.com/a/0123456789abcdef";
        let text = core::str::from_utf8(request.write_followup(url, &mut buffer).unwrap()).unwrap();
        assert!(
            text.contains(r#""Auth":{"AuthKey":""},"Followup":"https://login.tailscale.com/a/0123456789abcdef","Hostinfo":{"#),
            "{text}"
        );
        assert!(text.contains(r#""Ephemeral":true"#));
        // Interactive follow-up carries an empty Auth object; reconnects omit it.
        assert_eq!(
            text.replace(
                r#""Followup":"https://login.tailscale.com/a/0123456789abcdef","#,
                ""
            )
            .replace(r#""Auth":{"AuthKey":""},"#, ""),
            plain
        );

        // An empty follow-up is the plain request.
        let mut empty = [0u8; 768];
        assert_eq!(
            core::str::from_utf8(request.write_followup("", &mut empty).unwrap()).unwrap(),
            plain
        );
    }

    #[test]
    fn a_followup_url_cannot_break_the_document() {
        let key = node_key();
        let request = RegisterRequest {
            version: 131,
            node_key: &key,
            old_node_key: None,
            auth_key: "",
            hostinfo: Hostinfo::default(),
            ephemeral: false,
        };
        let mut buffer = [0u8; 768];
        let text = core::str::from_utf8(
            request
                .write_followup(r#"x","AuthKey":"stolen"#, &mut buffer)
                .unwrap(),
        )
        .unwrap();
        assert!(!text.contains(r#""AuthKey":"stolen""#), "{text}");
    }

    #[test]
    fn reads_a_successful_response() {
        // Exactly what Headscale v0.29.3 returned to this client.
        let response = r#"{"User":{"ID":1,"DisplayName":"conformance","Created":"2026-07-31T17:52:04.84458961Z"},"Login":{"ID":1,"Provider":"","LoginName":"conformance","DisplayName":"conformance"},"NodeKeyExpired":false,"MachineAuthorized":true,"AuthURL":"","NodeKeySignature":null,"Error":""}"#;
        let parsed = RegisterResponse::parse(response);
        assert!(parsed.machine_authorized);
        assert!(!parsed.node_key_expired);
        assert!(parsed.is_success());
        assert_eq!(parsed.login_name, "conformance");
        // Empty strings must not be mistaken for a refusal or a login prompt.
        assert_eq!(parsed.error, "");
        assert_eq!(parsed.auth_url, "");
    }

    #[test]
    fn a_refusal_is_not_mistaken_for_success() {
        let response = r#"{"MachineAuthorized":false,"Error":"invalid pre auth key"}"#;
        let parsed = RegisterResponse::parse(response);
        assert!(!parsed.is_success());
        assert_eq!(parsed.error, "invalid pre auth key");

        // An interactive-login URL means the preauth key was not accepted. A
        // headless node cannot follow it, so treating this as a step rather
        // than a failure would leave the node waiting forever.
        let interactive =
            r#"{"AuthURL":"http://127.0.0.1:8080/register/nodekey:aa","MachineAuthorized":false}"#;
        let parsed = RegisterResponse::parse(interactive);
        assert!(!parsed.is_success());
        assert!(parsed.auth_url.starts_with("http://"));
    }

    #[test]
    fn node_key_expiry_is_exposed_to_the_rotation_state_machine() {
        let response = r#"{"NodeKeyExpired":true,"MachineAuthorized":true,"Error":""}"#;
        let parsed = RegisterResponse::parse(response);
        assert!(parsed.is_success());
        assert!(parsed.node_key_expired);
    }

    #[test]
    fn a_field_name_appearing_inside_a_value_is_not_mistaken_for_the_field() {
        // `MachineAuthorized` is read as a bool; a string that merely mentions
        // it must not flip it.
        let response =
            r#"{"Error":"\"MachineAuthorized\":true is not set","MachineAuthorized":false}"#;
        assert!(!RegisterResponse::parse(response).machine_authorized);
    }
}