guacamole-client 0.5.1

Rust client library for the Guacamole REST API
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
use crate::error::{Error, Result};

/// Maximum allowed length for a URL path segment or token.
const MAX_SEGMENT_LENGTH: usize = 512;

/// Returns `true` if `s` is safe to embed in a URL path segment.
///
/// Rejects empty strings, strings longer than [`MAX_SEGMENT_LENGTH`], and strings
/// containing `/`, `\`, `..`, `\0`, `?`, `#`, `%`, or whitespace.
fn is_safe_path_segment(s: &str) -> bool {
    !s.is_empty()
        && s.len() <= MAX_SEGMENT_LENGTH
        && !s.contains("..")
        && !s.contains(['/', '\\', '\0', '?', '#', '%'])
        && !s.chars().any(char::is_whitespace)
}

/// Returns `true` if `s` is a non-empty string of ASCII digits within [`MAX_SEGMENT_LENGTH`].
fn is_ascii_digits(s: &str) -> bool {
    !s.is_empty() && s.len() <= MAX_SEGMENT_LENGTH && s.bytes().all(|b| b.is_ascii_digit())
}

/// Validates a value as a safe path segment, returning the error produced by `make_err` on failure.
fn validate_path_segment(value: &str, make_err: impl FnOnce(String) -> Error) -> Result<()> {
    if is_safe_path_segment(value) {
        Ok(())
    } else {
        Err(make_err(value.to_owned()))
    }
}

/// Validates a value as ASCII digits, returning the error produced by `make_err` on failure.
fn validate_digits(value: &str, make_err: impl FnOnce(String) -> Error) -> Result<()> {
    if is_ascii_digits(value) {
        Ok(())
    } else {
        Err(make_err(value.to_owned()))
    }
}

/// Validates that a data source name is safe to use in a URL path.
pub(crate) fn validate_data_source(ds: &str) -> Result<()> {
    validate_path_segment(ds, Error::InvalidDataSource)
}

/// Validates that a username is safe to use in a URL path.
pub(crate) fn validate_username(username: &str) -> Result<()> {
    validate_path_segment(username, Error::InvalidUsername)
}

/// Validates that a connection ID is a non-empty string of ASCII digits.
pub(crate) fn validate_connection_id(id: &str) -> Result<()> {
    validate_digits(id, Error::InvalidConnectionId)
}

/// Validates that a sharing profile ID is a non-empty string of ASCII digits.
pub(crate) fn validate_sharing_profile_id(id: &str) -> Result<()> {
    validate_digits(id, Error::InvalidSharingProfileId)
}

/// Validates that a user group ID is safe to use in a URL path.
pub(crate) fn validate_user_group_id(id: &str) -> Result<()> {
    validate_path_segment(id, Error::InvalidUserGroupId)
}

/// Validates that a connection group ID is safe to use in a URL path.
///
/// IDs can be `"ROOT"` or numeric strings.
pub(crate) fn validate_connection_group_id(id: &str) -> Result<()> {
    validate_path_segment(id, Error::InvalidConnectionGroupId)
}

/// Validates that a tunnel ID is safe to use in a URL path.
pub(crate) fn validate_tunnel_id(id: &str) -> Result<()> {
    validate_path_segment(id, Error::InvalidTunnelId)
}

/// Validates that a query parameter value does not contain characters that could
/// alter the URL structure (`&`, `#`, `?`, `%`, `\0`).
pub(crate) fn validate_query_param(name: &str, value: &str) -> Result<()> {
    if value.is_empty()
        || value.len() > MAX_SEGMENT_LENGTH
        || value.contains(['&', '#', '?', '%', '\0'])
        || value.bytes().any(|b| b.is_ascii_control())
    {
        Err(Error::InvalidQueryParam {
            name: name.to_owned(),
            reason: if value.is_empty() {
                "must not be empty".to_owned()
            } else if value.len() > MAX_SEGMENT_LENGTH {
                "exceeds maximum length".to_owned()
            } else {
                "contains unsafe characters".to_owned()
            },
        })
    } else {
        Ok(())
    }
}

/// Validates that a token is safe for use in both URL query values and URL path segments.
///
/// Rejects empty tokens and tokens containing `/`, `\`, `..`, `&`, `?`, `#`, `%`, `\0`,
/// or whitespace.
pub(crate) fn validate_token(token: &str) -> Result<()> {
    if token.is_empty()
        || token.len() > MAX_SEGMENT_LENGTH
        || token.contains("..")
        || token.contains(['/', '\\', '\0', '?', '#', '%', '&'])
        || token.chars().any(char::is_whitespace)
    {
        Err(Error::InvalidToken(token.to_owned()))
    } else {
        Ok(())
    }
}

/// Validates that a sort order is one of the allowed values (`asc` or `desc`).
pub(crate) fn validate_sort_order(order: &str) -> Result<()> {
    match order {
        "asc" | "desc" => Ok(()),
        _ => Err(Error::InvalidQueryParam {
            name: "order".to_owned(),
            reason: format!("must be \"asc\" or \"desc\", got \"{order}\""),
        }),
    }
}

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

    // --- data source ---

    #[test]
    fn valid_data_source() {
        assert!(validate_data_source("mysql").is_ok());
        assert!(validate_data_source("postgresql").is_ok());
        assert!(validate_data_source("my-data-source").is_ok());
    }

    #[test]
    fn invalid_data_source_empty() {
        assert!(matches!(
            validate_data_source(""),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_slash() {
        assert!(matches!(
            validate_data_source("a/b"),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_backslash() {
        assert!(matches!(
            validate_data_source("a\\b"),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_dot_dot() {
        assert!(matches!(
            validate_data_source(".."),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_null() {
        assert!(matches!(
            validate_data_source("a\0b"),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_query() {
        assert!(matches!(
            validate_data_source("a?b"),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_hash() {
        assert!(matches!(
            validate_data_source("a#b"),
            Err(Error::InvalidDataSource(_))
        ));
    }

    #[test]
    fn invalid_data_source_percent() {
        assert!(matches!(
            validate_data_source("a%2Fb"),
            Err(Error::InvalidDataSource(_))
        ));
    }

    // --- username ---

    #[test]
    fn valid_username() {
        assert!(validate_username("guacadmin").is_ok());
        assert!(validate_username("john.doe").is_ok());
        assert!(validate_username("user-1").is_ok());
    }

    #[test]
    fn invalid_username_empty() {
        assert!(matches!(
            validate_username(""),
            Err(Error::InvalidUsername(_))
        ));
    }

    #[test]
    fn invalid_username_slash() {
        assert!(matches!(
            validate_username("../admin"),
            Err(Error::InvalidUsername(_))
        ));
    }

    #[test]
    fn invalid_username_null() {
        assert!(matches!(
            validate_username("user\0"),
            Err(Error::InvalidUsername(_))
        ));
    }

    // --- connection ID ---

    #[test]
    fn valid_connection_id() {
        assert!(validate_connection_id("1").is_ok());
        assert!(validate_connection_id("42").is_ok());
        assert!(validate_connection_id("12345").is_ok());
    }

    #[test]
    fn invalid_connection_id_empty() {
        assert!(matches!(
            validate_connection_id(""),
            Err(Error::InvalidConnectionId(_))
        ));
    }

    #[test]
    fn invalid_connection_id_non_digit() {
        assert!(matches!(
            validate_connection_id("abc"),
            Err(Error::InvalidConnectionId(_))
        ));
    }

    #[test]
    fn invalid_connection_id_mixed() {
        assert!(matches!(
            validate_connection_id("12abc"),
            Err(Error::InvalidConnectionId(_))
        ));
    }

    #[test]
    fn invalid_connection_id_path_traversal() {
        assert!(matches!(
            validate_connection_id("../../etc"),
            Err(Error::InvalidConnectionId(_))
        ));
    }

    // --- sharing profile ID ---

    #[test]
    fn valid_sharing_profile_id() {
        assert!(validate_sharing_profile_id("1").is_ok());
        assert!(validate_sharing_profile_id("99").is_ok());
    }

    #[test]
    fn invalid_sharing_profile_id_empty() {
        assert!(matches!(
            validate_sharing_profile_id(""),
            Err(Error::InvalidSharingProfileId(_))
        ));
    }

    #[test]
    fn invalid_sharing_profile_id_non_digit() {
        assert!(matches!(
            validate_sharing_profile_id("abc"),
            Err(Error::InvalidSharingProfileId(_))
        ));
    }

    // --- user group ID ---

    #[test]
    fn valid_user_group_id() {
        assert!(validate_user_group_id("admins").is_ok());
        assert!(validate_user_group_id("my-group").is_ok());
    }

    #[test]
    fn invalid_user_group_id_empty() {
        assert!(matches!(
            validate_user_group_id(""),
            Err(Error::InvalidUserGroupId(_))
        ));
    }

    #[test]
    fn invalid_user_group_id_slash() {
        assert!(matches!(
            validate_user_group_id("a/b"),
            Err(Error::InvalidUserGroupId(_))
        ));
    }

    #[test]
    fn invalid_user_group_id_dot_dot() {
        assert!(matches!(
            validate_user_group_id(".."),
            Err(Error::InvalidUserGroupId(_))
        ));
    }

    // --- connection group ID ---

    #[test]
    fn valid_connection_group_id() {
        assert!(validate_connection_group_id("ROOT").is_ok());
        assert!(validate_connection_group_id("1").is_ok());
        assert!(validate_connection_group_id("42").is_ok());
    }

    #[test]
    fn invalid_connection_group_id_empty() {
        assert!(matches!(
            validate_connection_group_id(""),
            Err(Error::InvalidConnectionGroupId(_))
        ));
    }

    #[test]
    fn invalid_connection_group_id_slash() {
        assert!(matches!(
            validate_connection_group_id("a/b"),
            Err(Error::InvalidConnectionGroupId(_))
        ));
    }

    #[test]
    fn invalid_connection_group_id_dot_dot() {
        assert!(matches!(
            validate_connection_group_id(".."),
            Err(Error::InvalidConnectionGroupId(_))
        ));
    }

    // --- tunnel ID ---

    #[test]
    fn valid_tunnel_id() {
        assert!(validate_tunnel_id("abc-123").is_ok());
        assert!(validate_tunnel_id("tunnel-1").is_ok());
    }

    #[test]
    fn invalid_tunnel_id_empty() {
        assert!(matches!(
            validate_tunnel_id(""),
            Err(Error::InvalidTunnelId(_))
        ));
    }

    #[test]
    fn invalid_tunnel_id_slash() {
        assert!(matches!(
            validate_tunnel_id("a/b"),
            Err(Error::InvalidTunnelId(_))
        ));
    }

    #[test]
    fn invalid_tunnel_id_dot_dot() {
        assert!(matches!(
            validate_tunnel_id(".."),
            Err(Error::InvalidTunnelId(_))
        ));
    }

    // --- query param ---

    #[test]
    fn valid_query_param() {
        assert!(validate_query_param("contains", "my-server").is_ok());
        assert!(validate_query_param("contains", "test value").is_ok());
    }

    #[test]
    fn invalid_query_param_empty() {
        assert!(matches!(
            validate_query_param("contains", ""),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    #[test]
    fn invalid_query_param_ampersand() {
        assert!(matches!(
            validate_query_param("contains", "x&limit=0"),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    #[test]
    fn invalid_query_param_hash() {
        assert!(matches!(
            validate_query_param("contains", "x#fragment"),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    #[test]
    fn invalid_query_param_question_mark() {
        assert!(matches!(
            validate_query_param("contains", "x?extra=1"),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    #[test]
    fn invalid_query_param_percent() {
        assert!(matches!(
            validate_query_param("contains", "x%00"),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    #[test]
    fn invalid_query_param_null() {
        assert!(matches!(
            validate_query_param("contains", "x\0y"),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    #[test]
    fn invalid_query_param_control_chars() {
        assert!(matches!(
            validate_query_param("contains", "x\ty"),
            Err(Error::InvalidQueryParam { .. })
        ));
        assert!(matches!(
            validate_query_param("contains", "x\ny"),
            Err(Error::InvalidQueryParam { .. })
        ));
        assert!(matches!(
            validate_query_param("contains", "x\ry"),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    // --- token ---

    #[test]
    fn valid_token() {
        assert!(validate_token("ABCDEF1234567890").is_ok());
        assert!(validate_token("168F8D0A2D68247F30B7E2E01187AEE2CF82186D").is_ok());
        assert!(validate_token("simple-token").is_ok());
    }

    #[test]
    fn invalid_token_empty() {
        assert!(matches!(validate_token(""), Err(Error::InvalidToken(_))));
    }

    #[test]
    fn invalid_token_ampersand() {
        assert!(matches!(
            validate_token("a&b=c"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_slash() {
        assert!(matches!(
            validate_token("a/b"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_backslash() {
        assert!(matches!(
            validate_token("a\\b"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_dot_dot() {
        assert!(matches!(
            validate_token(".."),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_hash() {
        assert!(matches!(
            validate_token("tok#frag"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_question_mark() {
        assert!(matches!(
            validate_token("tok?extra"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_percent() {
        assert!(matches!(
            validate_token("tok%00"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_null() {
        assert!(matches!(
            validate_token("tok\0"),
            Err(Error::InvalidToken(_))
        ));
    }

    #[test]
    fn invalid_token_whitespace() {
        assert!(matches!(
            validate_token("tok en"),
            Err(Error::InvalidToken(_))
        ));
        assert!(matches!(
            validate_token("\t"),
            Err(Error::InvalidToken(_))
        ));
    }

    // --- sort order ---

    #[test]
    fn valid_sort_order() {
        assert!(validate_sort_order("asc").is_ok());
        assert!(validate_sort_order("desc").is_ok());
    }

    #[test]
    fn invalid_sort_order() {
        assert!(matches!(
            validate_sort_order("ASC"),
            Err(Error::InvalidQueryParam { .. })
        ));
        assert!(matches!(
            validate_sort_order("invalid"),
            Err(Error::InvalidQueryParam { .. })
        ));
        assert!(matches!(
            validate_sort_order(""),
            Err(Error::InvalidQueryParam { .. })
        ));
    }

    // --- unicode and edge cases ---

    #[test]
    fn validation_unicode_inputs() {
        // Unicode in data_source/username should pass (safe path segment)
        assert!(validate_data_source("źródło").is_ok());
        assert!(validate_username("użytkownik").is_ok());

        // Non-ASCII digits should fail connection_id validation (requires ASCII digits)
        assert!(validate_connection_id("٤٢").is_err()); // Arabic-Indic digits
        assert!(validate_connection_id("12").is_err()); // Fullwidth digits
    }

    #[test]
    fn validation_whitespace_only() {
        // Whitespace-only strings are rejected by is_safe_path_segment
        assert!(validate_data_source(" ").is_err());
        assert!(validate_username("\t").is_err());

        // Whitespace also fails is_ascii_digits
        assert!(validate_connection_id(" ").is_err());
    }

    #[test]
    fn validation_very_long_strings() {
        let long = "a".repeat(10_000);
        // Strings exceeding MAX_SEGMENT_LENGTH are rejected
        assert!(validate_data_source(&long).is_err());
        assert!(validate_username(&long).is_err());
        assert!(validate_token(&long).is_err());
        assert!(validate_query_param("k", &long).is_err());

        // Digit-only validators also enforce MAX_SEGMENT_LENGTH
        let long_digits = "1".repeat(10_000);
        assert!(validate_connection_id(&long_digits).is_err());
        assert!(validate_sharing_profile_id(&long_digits).is_err());
    }

    #[test]
    fn validation_max_length() {
        let at_limit = "a".repeat(MAX_SEGMENT_LENGTH);
        assert!(validate_data_source(&at_limit).is_ok());
        assert!(validate_username(&at_limit).is_ok());
        assert!(validate_token(&at_limit).is_ok());
        assert!(validate_query_param("k", &at_limit).is_ok());

        let digits_at_limit = "1".repeat(MAX_SEGMENT_LENGTH);
        assert!(validate_connection_id(&digits_at_limit).is_ok());
        assert!(validate_sharing_profile_id(&digits_at_limit).is_ok());

        let over_limit = "a".repeat(MAX_SEGMENT_LENGTH + 1);
        assert!(validate_data_source(&over_limit).is_err());
        assert!(validate_username(&over_limit).is_err());
        assert!(validate_token(&over_limit).is_err());
        assert!(validate_query_param("k", &over_limit).is_err());

        let digits_over_limit = "1".repeat(MAX_SEGMENT_LENGTH + 1);
        assert!(validate_connection_id(&digits_over_limit).is_err());
        assert!(validate_sharing_profile_id(&digits_over_limit).is_err());
    }

    #[test]
    fn validation_dot_variants() {
        // Single dot is fine (no ".." substring)
        assert!(validate_data_source(".").is_ok());

        // Double dot and beyond are rejected
        assert!(validate_data_source("..").is_err());
        assert!(validate_data_source("...").is_err());
        assert!(validate_data_source("a..b").is_err());
    }

    #[test]
    fn connection_id_leading_zeros() {
        assert!(validate_connection_id("007").is_ok());
        assert!(validate_connection_id("0").is_ok());
    }

    #[test]
    fn connection_id_negative_decimal_plus() {
        assert!(validate_connection_id("-1").is_err());
        assert!(validate_connection_id("1.5").is_err());
        assert!(validate_connection_id("+1").is_err());
    }
}