aws_sigv4/http_request/
uri_path_normalization.rs

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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use std::borrow::Cow;

// Normalize `uri_path` according to
// https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
pub(super) fn normalize_uri_path(uri_path: &str) -> Cow<'_, str> {
    // If the absolute path is empty, use a forward slash (/).
    if uri_path.is_empty() {
        return Cow::Borrowed("/");
    }

    // The canonical URI is the URI-encoded version of the _absolute_ path component of the URI.
    let result = if uri_path.starts_with('/') {
        Cow::Borrowed(uri_path)
    } else {
        Cow::Owned(format!("/{uri_path}"))
    };

    if !(result.contains('.') || result.contains("//")) {
        return result;
    }

    Cow::Owned(normalize_path_segment(&result))
}

// Implement 5.2.4. Remove Dot Segments in https://www.rfc-editor.org/rfc/rfc3986
//
// The function assumes that `uri_path` is an absolute path,
// starting with a forward slash.
fn normalize_path_segment(uri_path: &str) -> String {
    let number_of_slashes = uri_path.matches('/').count();
    let mut normalized: Vec<&str> = Vec::with_capacity(number_of_slashes + 1);

    for segment in uri_path.split('/') {
        match segment {
            // Segments that are empty or contain only a single period should not be preserved
            "" | "." => {}
            ".." => {
                normalized.pop();
            }
            otherwise => normalized.push(otherwise),
        }
    }

    let mut result = normalized.join("/");

    // Even though `uri_path` starts with a `/`, that may not be the case for `result`.
    // An example of this is `uri_path` being "/../foo" where the corresponding `result`
    // will be "foo".
    if !result.starts_with('/') {
        result.insert(0, '/');
    }

    // If `uri_path` is "/foo/bar/.", normalizing it should be "/foo/bar/". However,
    // the logic so far only makes `result` "/foo/bar", without the trailing slash.
    // The condition below ensures that the trailing slash is appended to `result`
    // if `uri_path` ends with a slash (per the RFC) but `result` does not.
    if ends_with_slash(uri_path) && !result.ends_with('/') {
        result.push('/');
    }

    result
}

fn ends_with_slash(uri_path: &str) -> bool {
    // These are all translated to "/" per 2.B and 2.C in section 5.2.4 in RFC 3986.
    ["/", "/.", "/./", "/..", "/../"]
        .iter()
        .any(|s| uri_path.ends_with(s))
}

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

    #[test]
    fn normalize_uri_path_should_not_modify_input_containing_just_a_forward_slash() {
        assert_eq!(normalize_uri_path("/"), Cow::<'_, str>::Borrowed("/"));
    }

    #[test]
    fn normalize_uri_path_should_add_a_forward_slash_when_input_is_empty() {
        assert_eq!(
            normalize_uri_path(""),
            Cow::<'_, str>::Owned("/".to_owned())
        );
    }

    #[test]
    fn normalize_uri_path_should_not_modify_single_non_dot_segment_starting_with_a_single_forward_slash(
    ) {
        assert_eq!(normalize_uri_path("/foo"), Cow::Borrowed("/foo"));
    }

    #[test]
    fn normalize_uri_path_should_prepend_forward_slash_when_input_is_relative() {
        assert_eq!(
            normalize_uri_path("foo"),
            Cow::<'_, str>::Owned("/foo".to_owned())
        );
    }

    #[test]
    fn normalize_uri_path_should_not_modify_multiple_non_dot_segments_starting_with_a_single_forward_slash(
    ) {
        assert_eq!(normalize_uri_path("/foo/bar"), Cow::Borrowed("/foo/bar"));
    }

    #[test]
    fn normalize_uri_path_should_not_modify_multiple_non_dot_segments_with_a_trailing_forward_slash(
    ) {
        assert_eq!(normalize_uri_path("/foo/bar/"), Cow::Borrowed("/foo/bar/"));
    }

    // 2.A in https://www.rfc-editor.org/rfc/rfc3986#section-5.2.4
    #[test]
    fn normalize_uri_path_should_remove_a_leading_dot_from_input() {
        // The expected value is "/" rather than "" because if the absolute path is empty,
        // we use a forward slash.
        assert_eq!(
            normalize_uri_path("./"),
            Cow::<'_, str>::Owned("/".to_owned())
        );

        assert_eq!(
            normalize_uri_path("./foo"),
            Cow::<'_, str>::Owned("/foo".to_owned())
        );
    }

    // 2.A in https://www.rfc-editor.org/rfc/rfc3986#section-5.2.4
    #[test]
    fn normalize_uri_path_should_remove_leading_double_dots_from_input() {
        // The expected value is "/" rather than "" because if the absolute path is empty,
        // we use a forward slash.
        assert_eq!(
            normalize_uri_path("../"),
            Cow::<'_, str>::Owned("/".to_owned())
        );

        assert_eq!(
            normalize_uri_path("../foo"),
            Cow::<'_, str>::Owned("/foo".to_owned())
        );
    }

    // 2.B in https://www.rfc-editor.org/rfc/rfc3986#section-5.2.4
    #[test]
    fn normalize_uri_path_should_remove_a_singel_dot_from_input() {
        assert_eq!(
            normalize_uri_path("/."),
            Cow::<'_, str>::Owned("/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/./"),
            Cow::<'_, str>::Owned("/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/./foo"),
            Cow::<'_, str>::Owned("/foo".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/foo/bar/."),
            Cow::<'_, str>::Owned("/foo/bar/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/foo/bar/./"),
            Cow::<'_, str>::Owned("/foo/bar/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/foo/./bar/./"),
            Cow::<'_, str>::Owned("/foo/bar/".to_owned())
        );
    }

    // 2.C in https://www.rfc-editor.org/rfc/rfc3986#section-5.2.4
    #[test]
    fn normalize_uri_path_should_remove_double_dots_from_input() {
        assert_eq!(
            normalize_uri_path("/.."),
            Cow::<'_, str>::Owned("/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/../"),
            Cow::<'_, str>::Owned("/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/../foo"),
            Cow::<'_, str>::Owned("/foo".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/foo/bar/.."),
            Cow::<'_, str>::Owned("/foo/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/foo/bar/../"),
            Cow::<'_, str>::Owned("/foo/".to_owned())
        );
        assert_eq!(
            normalize_uri_path("/foo/../bar/../"),
            Cow::<'_, str>::Owned("/".to_owned())
        );
    }

    // 2.D in https://www.rfc-editor.org/rfc/rfc3986#section-5.2.4
    #[test]
    fn normalize_uri_path_should_replace_a_dot_segment_with_a_forward_slash() {
        assert_eq!(
            normalize_uri_path("."),
            Cow::<'_, str>::Owned("/".to_owned())
        );
        assert_eq!(
            normalize_uri_path(".."),
            Cow::<'_, str>::Owned("/".to_owned())
        );
    }

    // Page 34 in https://www.rfc-editor.org/rfc/rfc3986
    #[test]
    fn normalize_uri_path_should_behave_as_expected_against_examples_in_rfc() {
        assert_eq!(
            normalize_uri_path("/a/b/c/./../../g"),
            Cow::<'_, str>::Owned("/a/g".to_owned())
        );
        // The expected value will be absolutized.
        assert_eq!(
            normalize_uri_path("mid/content=5/../6"),
            Cow::<'_, str>::Owned("/mid/6".to_owned())
        );
    }

    // The CRT does this so I figured we should too. - Zelda
    #[test]
    fn normalize_uri_path_should_merge_multiple_subsequent_slashes_into_one() {
        assert_eq!(
            normalize_uri_path("//foo//"),
            Cow::<'_, str>::Owned("/foo/".to_owned())
        );
    }

    #[test]
    fn normalize_uri_path_should_not_remove_dot_when_surrounded_by_percent_encoded_forward_slashes()
    {
        assert_eq!(
            normalize_uri_path("/foo%2F.%2Fbar"),
            Cow::<'_, str>::Borrowed("/foo%2F.%2Fbar")
        );
    }
}