Skip to main content

libdd_trace_obfuscation/
memcached.rs

1// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4/// Obfuscates the memcached command cmd.
5#[must_use]
6pub fn obfuscate_memcached_string(cmd: &str) -> String {
7    // All memcached commands end with new lines [1]. In the case of storage
8    // commands, key values follow after. Knowing this, all we have to do
9    // to obfuscate sensitive information is to remove everything that follows
10    // a new line. For non-storage commands, this will have no effect.
11    // [1]: https://github.com/memcached/memcached/blob/master/doc/protocol.txt
12    let split: Vec<&str> = cmd.splitn(2, "\r\n").collect();
13    let res = split.first().copied().unwrap_or(cmd);
14    res.trim().to_string()
15}
16
17#[cfg(test)]
18mod tests {
19    use duplicate::duplicate_item;
20
21    use super::obfuscate_memcached_string;
22
23    #[duplicate_item(
24        test_name                       input                                       expected;
25        [test_obfuscate_memcached_1]    ["set mykey 0 60 5\r\nvalue"]               ["set mykey 0 60 5"];
26        [test_obfuscate_memcached_2]    ["get mykey"]                               ["get mykey"];
27        [test_obfuscate_memcached_3]    ["add newkey 0 60 5\r\nvalue"]              ["add newkey 0 60 5"];
28        [test_obfuscate_memcached_4]    ["add newkey 0 60 5\r\nvalue\r\nvalue1"]    ["add newkey 0 60 5"];
29        [test_obfuscate_memcached_5]    ["decr mykey 5"]                            ["decr mykey 5"];
30        [fuzzing_2126976840]            ["\t"]                                      [""];
31    )]
32    #[test]
33    fn test_name() {
34        let result = obfuscate_memcached_string(input);
35        assert_eq!(result, expected);
36    }
37}