safe-path 0.1.0

A library to safely handle file system paths for container runtimes
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
// Copyright (c) 2022 Alibaba Cloud
//
// SPDX-License-Identifier: Apache-2.0
//

use std::io::{Error, ErrorKind, Result};
use std::path::{Component, Path, PathBuf};

// Follow the same configuration as
// [secure_join](https://github.com/cyphar/filepath-securejoin/blob/master/join.go#L51)
const MAX_SYMLINK_DEPTH: u32 = 255;

fn do_scoped_resolve<R: AsRef<Path>, U: AsRef<Path>>(
    root: R,
    unsafe_path: U,
) -> Result<(PathBuf, PathBuf)> {
    let root = root.as_ref().canonicalize()?;

    let mut nlinks = 0u32;
    let mut curr_path = unsafe_path.as_ref().to_path_buf();
    'restart: loop {
        let mut subpath = PathBuf::new();
        let mut iter = curr_path.components();

        'next_comp: while let Some(comp) = iter.next() {
            match comp {
                // Linux paths don't have prefixes.
                Component::Prefix(_) => {
                    return Err(Error::new(
                        ErrorKind::Other,
                        format!("Invalid path prefix in: {}", unsafe_path.as_ref().display()),
                    ));
                }
                // `RootDir` should always be the first component, and Path::components() ensures
                // that.
                Component::RootDir | Component::CurDir => {
                    continue 'next_comp;
                }
                Component::ParentDir => {
                    subpath.pop();
                }
                Component::Normal(n) => {
                    let path = root.join(&subpath).join(n);
                    if let Ok(v) = path.read_link() {
                        nlinks += 1;
                        if nlinks > MAX_SYMLINK_DEPTH {
                            return Err(Error::new(
                                ErrorKind::Other,
                                format!(
                                    "Too many levels of symlinks: {}",
                                    unsafe_path.as_ref().display()
                                ),
                            ));
                        }
                        curr_path = if v.is_absolute() {
                            v.join(iter.as_path())
                        } else {
                            subpath.join(v).join(iter.as_path())
                        };
                        continue 'restart;
                    } else {
                        subpath.push(n);
                    }
                }
            }
        }

        return Ok((root, subpath));
    }
}

/// Resolve `unsafe_path` to a relative path, rooted at and constrained by `root`.
///
/// The `scoped_resolve()` function assumes `root` exists and is an absolute path. It processes
/// each path component in `unsafe_path` as below:
/// - assume it's not a symlink and output if the component doesn't exist yet.
/// - ignore if it's "/" or ".".
/// - go to parent directory but constrained by `root` if it's "..".
/// - recursively resolve to the real path if it's a symlink. All symlink resolutions will be
///   constrained by `root`.
/// - otherwise output the path component.
///
/// # Arguments
/// - `root`: the absolute path to constrain the symlink resolution.
/// - `unsafe_path`: the path to resolve.
///
/// Note that the guarantees provided by this function only apply if the path components in the
/// returned PathBuf are not modified (in other words are not replaced with symlinks on the
/// filesystem) after this function has returned. You may use [crate::PinnedPathBuf] to protect
/// from such TOCTOU attacks.
pub fn scoped_resolve<R: AsRef<Path>, U: AsRef<Path>>(root: R, unsafe_path: U) -> Result<PathBuf> {
    do_scoped_resolve(root, unsafe_path).map(|(_root, path)| path)
}

/// Safely join `unsafe_path` to `root`, and ensure `unsafe_path` is scoped under `root`.
///
/// The `scoped_join()` function assumes `root` exists and is an absolute path. It safely joins the
/// two given paths and ensures:
/// - The returned path is guaranteed to be scoped inside `root`.
/// - Any symbolic links in the path are evaluated with the given `root` treated as the root of the
///   filesystem, similar to a chroot.
///
/// It's modelled after [secure_join](https://github.com/cyphar/filepath-securejoin), but only
/// for Linux systems.
///
/// # Arguments
/// - `root`: the absolute path to scope the symlink evaluation.
/// - `unsafe_path`: the path to evaluated and joint with `root`. It is unsafe since it may try to
///   escape from the `root` by using "../" or symlinks.
///
/// # Security
/// On success return, the `scoped_join()` function guarantees that:
/// - The resulting PathBuf must be a child path of `root` and will not contain any symlink path
///   components (they will all get expanded).
/// - When expanding symlinks, all symlink path components must be resolved relative to the provided
///   `root`. In particular, this can be considered a userspace implementation of how chroot(2)
///    operates on file paths.
/// - Non-existent path components are unaffected.
///
/// Note that the guarantees provided by this function only apply if the path components in the
/// returned string are not modified (in other words are not replaced with symlinks on the
/// filesystem) after this function has returned. You may use [crate::PinnedPathBuf] to protect
/// from such TOCTTOU attacks.
pub fn scoped_join<R: AsRef<Path>, U: AsRef<Path>>(root: R, unsafe_path: U) -> Result<PathBuf> {
    do_scoped_resolve(root, unsafe_path).map(|(root, path)| root.join(path))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::DirBuilder;
    use std::os::unix::fs;
    use tempfile::tempdir;

    #[allow(dead_code)]
    #[derive(Debug)]
    struct TestData<'a> {
        name: &'a str,
        rootfs: &'a Path,
        unsafe_path: &'a str,
        result: &'a str,
    }

    fn exec_tests(tests: &[TestData]) {
        for (i, t) in tests.iter().enumerate() {
            // Create a string containing details of the test
            let msg = format!("test[{}]: {:?}", i, t);
            let result = scoped_resolve(t.rootfs, t.unsafe_path).unwrap();
            let msg = format!("{}, result: {:?}", msg, result);

            // Perform the checks
            assert_eq!(&result, Path::new(t.result), "{}", msg);
        }
    }

    #[test]
    fn test_scoped_resolve() {
        // create temporary directory to emulate container rootfs with symlink
        let rootfs_dir = tempdir().expect("failed to create tmpdir");
        DirBuilder::new()
            .create(rootfs_dir.path().join("b"))
            .unwrap();
        fs::symlink(rootfs_dir.path().join("b"), rootfs_dir.path().join("a")).unwrap();
        let rootfs_path = &rootfs_dir.path().join("a");

        let tests = [
            TestData {
                name: "normal path",
                rootfs: rootfs_path,
                unsafe_path: "a/b/c",
                result: "a/b/c",
            },
            TestData {
                name: "path with .. at beginning",
                rootfs: rootfs_path,
                unsafe_path: "../../../a/b/c",
                result: "a/b/c",
            },
            TestData {
                name: "path with complex .. pattern",
                rootfs: rootfs_path,
                unsafe_path: "../../../a/../../b/../../c",
                result: "c",
            },
            TestData {
                name: "path with .. in middle",
                rootfs: rootfs_path,
                unsafe_path: "/usr/bin/../../bin/ls",
                result: "bin/ls",
            },
            TestData {
                name: "path with . and ..",
                rootfs: rootfs_path,
                unsafe_path: "/usr/./bin/../../bin/./ls",
                result: "bin/ls",
            },
            TestData {
                name: "path with . at end",
                rootfs: rootfs_path,
                unsafe_path: "/usr/./bin/../../bin/./ls/.",
                result: "bin/ls",
            },
            TestData {
                name: "path try to escape by ..",
                rootfs: rootfs_path,
                unsafe_path: "/usr/./bin/../../../../bin/./ls/../ls",
                result: "bin/ls",
            },
            TestData {
                name: "path with .. at the end",
                rootfs: rootfs_path,
                unsafe_path: "/usr/./bin/../../bin/./ls/..",
                result: "bin",
            },
            TestData {
                name: "path ..",
                rootfs: rootfs_path,
                unsafe_path: "..",
                result: "",
            },
            TestData {
                name: "path .",
                rootfs: rootfs_path,
                unsafe_path: ".",
                result: "",
            },
            TestData {
                name: "path /",
                rootfs: rootfs_path,
                unsafe_path: "/",
                result: "",
            },
            TestData {
                name: "empty path",
                rootfs: rootfs_path,
                unsafe_path: "",
                result: "",
            },
        ];

        exec_tests(&tests);
    }

    #[test]
    fn test_scoped_resolve_invalid() {
        scoped_resolve("./root_is_not_absolute_path", ".").unwrap_err();
        scoped_resolve("C:", ".").unwrap_err();
        scoped_resolve(r#"\\server\test"#, ".").unwrap_err();
        scoped_resolve(r#"http://localhost/test"#, ".").unwrap_err();
        // Chinese Unicode characters
        scoped_resolve(r#"您好"#, ".").unwrap_err();
    }

    #[test]
    fn test_scoped_resolve_symlink() {
        // create temporary directory to emulate container rootfs with symlink
        let rootfs_dir = tempdir().expect("failed to create tmpdir");
        let rootfs_path = &rootfs_dir.path();
        std::fs::create_dir(rootfs_path.join("symlink_dir")).unwrap();

        fs::symlink("../../../", rootfs_path.join("1")).unwrap();
        let tests = [TestData {
            name: "relative symlink beyond root",
            rootfs: rootfs_path,
            unsafe_path: "1",
            result: "",
        }];
        exec_tests(&tests);

        fs::symlink("/dddd", rootfs_path.join("2")).unwrap();
        let tests = [TestData {
            name: "abs symlink pointing to non-exist directory",
            rootfs: rootfs_path,
            unsafe_path: "2",
            result: "dddd",
        }];
        exec_tests(&tests);

        fs::symlink("/", rootfs_path.join("3")).unwrap();
        let tests = [TestData {
            name: "abs symlink pointing to /",
            rootfs: rootfs_path,
            unsafe_path: "3",
            result: "",
        }];
        exec_tests(&tests);

        fs::symlink("usr/bin/../bin/ls", rootfs_path.join("4")).unwrap();
        let tests = [TestData {
            name: "symlink with one ..",
            rootfs: rootfs_path,
            unsafe_path: "4",
            result: "usr/bin/ls",
        }];
        exec_tests(&tests);

        fs::symlink("usr/bin/../../bin/ls", rootfs_path.join("5")).unwrap();
        let tests = [TestData {
            name: "symlink with two ..",
            rootfs: rootfs_path,
            unsafe_path: "5",
            result: "bin/ls",
        }];
        exec_tests(&tests);

        fs::symlink(
            "../usr/bin/../../../bin/ls",
            rootfs_path.join("symlink_dir/6"),
        )
        .unwrap();
        let tests = [TestData {
            name: "symlink try to escape",
            rootfs: rootfs_path,
            unsafe_path: "symlink_dir/6",
            result: "bin/ls",
        }];
        exec_tests(&tests);

        // Detect symlink loop.
        fs::symlink("/endpoint_b", rootfs_path.join("endpoint_a")).unwrap();
        fs::symlink("/endpoint_a", rootfs_path.join("endpoint_b")).unwrap();
        scoped_resolve(rootfs_path, "endpoint_a").unwrap_err();
    }

    #[test]
    fn test_scoped_join() {
        // create temporary directory to emulate container rootfs with symlink
        let rootfs_dir = tempdir().expect("failed to create tmpdir");
        let rootfs_path = &rootfs_dir.path();

        assert_eq!(
            scoped_join(&rootfs_path, "a").unwrap(),
            rootfs_path.join("a")
        );
        assert_eq!(
            scoped_join(&rootfs_path, "./a").unwrap(),
            rootfs_path.join("a")
        );
        assert_eq!(
            scoped_join(&rootfs_path, "././a").unwrap(),
            rootfs_path.join("a")
        );
        assert_eq!(
            scoped_join(&rootfs_path, "c/d/../../a").unwrap(),
            rootfs_path.join("a")
        );
        assert_eq!(
            scoped_join(&rootfs_path, "c/d/../../../.././a").unwrap(),
            rootfs_path.join("a")
        );
        assert_eq!(
            scoped_join(&rootfs_path, "../../a").unwrap(),
            rootfs_path.join("a")
        );
        assert_eq!(
            scoped_join(&rootfs_path, "./../a").unwrap(),
            rootfs_path.join("a")
        );
    }

    #[test]
    fn test_scoped_join_symlink() {
        // create temporary directory to emulate container rootfs with symlink
        let rootfs_dir = tempdir().expect("failed to create tmpdir");
        let rootfs_path = &rootfs_dir.path();
        DirBuilder::new()
            .recursive(true)
            .create(rootfs_dir.path().join("b/c"))
            .unwrap();
        fs::symlink("b/c", rootfs_dir.path().join("a")).unwrap();

        let target = rootfs_path.join("b/c");
        assert_eq!(scoped_join(&rootfs_path, "a").unwrap(), target);
        assert_eq!(scoped_join(&rootfs_path, "./a").unwrap(), target);
        assert_eq!(scoped_join(&rootfs_path, "././a").unwrap(), target);
        assert_eq!(scoped_join(&rootfs_path, "b/c/../../a").unwrap(), target);
        assert_eq!(
            scoped_join(&rootfs_path, "b/c/../../../.././a").unwrap(),
            target
        );
        assert_eq!(scoped_join(&rootfs_path, "../../a").unwrap(), target);
        assert_eq!(scoped_join(&rootfs_path, "./../a").unwrap(), target);
        assert_eq!(scoped_join(&rootfs_path, "a/../../../a").unwrap(), target);
        assert_eq!(scoped_join(&rootfs_path, "a/../../../b/c").unwrap(), target);
    }

    #[test]
    fn test_scoped_join_symlink_loop() {
        // create temporary directory to emulate container rootfs with symlink
        let rootfs_dir = tempdir().expect("failed to create tmpdir");
        let rootfs_path = &rootfs_dir.path();
        fs::symlink("/endpoint_b", rootfs_path.join("endpoint_a")).unwrap();
        fs::symlink("/endpoint_a", rootfs_path.join("endpoint_b")).unwrap();
        scoped_join(rootfs_path, "endpoint_a").unwrap_err();
    }

    #[test]
    fn test_scoped_join_unicode_character() {
        // create temporary directory to emulate container rootfs with symlink
        let rootfs_dir = tempdir().expect("failed to create tmpdir");
        let rootfs_path = &rootfs_dir.path().canonicalize().unwrap();

        let path = scoped_join(rootfs_path, "您好").unwrap();
        assert_eq!(path, rootfs_path.join("您好"));

        let path = scoped_join(rootfs_path, "../../../您好").unwrap();
        assert_eq!(path, rootfs_path.join("您好"));

        let path = scoped_join(rootfs_path, "。。/您好").unwrap();
        assert_eq!(path, rootfs_path.join("。。/您好"));

        let path = scoped_join(rootfs_path, "您好/../../test").unwrap();
        assert_eq!(path, rootfs_path.join("test"));
    }
}