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
use crate::args::Args;
use crate::errors::*;
use crate::security;
use crate::utils;
use actix_files::NamedFile;
use actix_web::{
    get, http::header, middleware, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
};
use libtor::TorAddress;
use std::borrow::Cow;
use std::fs;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;

const DIR_LIST_PADDING: usize = 50;

pub struct Config {
    web_root: String,
    list_directories: bool,
}

fn resolve_path_req(base: &str, req: &Path) -> Result<PathBuf> {
    let mut path = PathBuf::from(base);
    for comp in req.components() {
        match comp {
            Component::Prefix(_) => bail!("Invalid component: windows path prefix"),
            Component::RootDir => bail!("Invalid component: unix root directory"),
            Component::CurDir => bail!("Invalid component: current dir"),
            Component::ParentDir => bail!("Invalid component: parent dir"),
            Component::Normal(comp) => {
                path.push(comp);
            }
        }
    }
    Ok(path)
}

fn bad_request() -> HttpResponse {
    HttpResponse::BadRequest()
        .content_type("text/plain; charset=utf-8")
        .body("400 - bad request\n")
}

fn forbidden() -> HttpResponse {
    HttpResponse::Forbidden()
        .content_type("text/plain; charset=utf-8")
        .body("403 - forbidden\n")
}

fn not_found() -> HttpResponse {
    HttpResponse::NotFound()
        .content_type("text/plain; charset=utf-8")
        .body("404 - not found\n")
}

fn internal_error() -> HttpResponse {
    HttpResponse::InternalServerError()
        .content_type("text/plain; charset=utf-8")
        .body("500 - internal server error\n")
}

enum ResolvedPath<'a> {
    File(Cow<'a, Path>),
    ListDir(&'a Path),
    Forbidden,
    NotFound,
}

fn resolve_path_fs(path: &Path, list_directories: bool) -> ResolvedPath {
    if path.exists() {
        if path.is_dir() {
            let index_path = path.join("index.html");
            if index_path.exists() {
                ResolvedPath::File(Cow::Owned(index_path))
            } else if list_directories {
                ResolvedPath::ListDir(path)
            } else {
                ResolvedPath::Forbidden
            }
        } else {
            ResolvedPath::File(Cow::Borrowed(path))
        }
    } else {
        ResolvedPath::NotFound
    }
}

fn list_directory(full_path: &Path, req_path: &str) -> Result<String> {
    let escaped_req_path = htmlescape::encode_minimal(req_path);
    let mut buf = format!(
        r#"<html>
<head><title>Index of /{}</title></head>
<body>
<h1>Index of /{}</h1><hr><pre>
"#,
        escaped_req_path, escaped_req_path
    );

    if !req_path.is_empty() {
        buf.push_str("<a href=\"../\">../</a>\n");
    }

    let iter = fs::read_dir(&full_path).context("Failed to list directory")?;

    let mut listing = Vec::new();
    for entry in iter {
        let entry = entry.context("Failed to get directory entry")?;

        let file_name = entry.file_name();
        // skip filenames with invalid utf8
        if let Ok(file_name) = file_name.into_string() {
            let md = entry
                .metadata()
                .with_context(|| anyhow!("Failed to stat file: {:?}", entry.path()))?;
            listing.push((file_name, md.len()));
        }
    }

    listing.sort();

    for (file_name, size) in listing {
        let padding = DIR_LIST_PADDING.saturating_sub(file_name.len());
        let escaped_name = htmlescape::encode_attribute(&file_name);
        buf.push_str(&format!(
            "<a href=\"{}\">{}</a>{} {}\n",
            escaped_name,
            escaped_name,
            " ".repeat(padding),
            size
        ));
    }

    buf.push_str("</pre><hr></body>\n</html>\n");
    Ok(buf)
}

#[get("/{tail:.*}")]
async fn index(cfg: web::Data<Config>, req: HttpRequest) -> impl Responder {
    let req_path: PathBuf = req.match_info().query("tail").parse().unwrap();
    let path = match resolve_path_req(&cfg.web_root, &req_path) {
        Ok(path) => path,
        Err(err) => {
            debug!("Invalid request path: {:?} ({:#})", req_path, err);
            return bad_request();
        }
    };

    match resolve_path_fs(&path, cfg.list_directories) {
        ResolvedPath::File(path) => {
            let file = match NamedFile::open(&path) {
                Ok(file) => file,
                Err(err) => {
                    warn!("Failed to open file({:?}): {:#}", path, err);
                    return forbidden();
                }
            };

            let res = file
                .prefer_utf8(true)
                .disable_content_disposition()
                .use_etag(false)
                .use_last_modified(false)
                .into_response(&req);

            match res {
                Ok(res) => res,
                Err(err) => {
                    warn!("Failed to create http response for {:?}: {:#}", path, err);
                    internal_error()
                }
            }
        }
        ResolvedPath::ListDir(path) => {
            let req_path = match utils::path_to_string(req_path) {
                Ok(path) => path,
                Err(_) => return bad_request(),
            };

            // if req_path is not empty but doesn't end with /, redirect
            if !req_path.is_empty() && !req_path.ends_with('/') {
                return HttpResponse::Found()
                    .header(header::LOCATION, format!("/{}/", req_path))
                    .finish();
            }

            let listing = match list_directory(path, &req_path) {
                Ok(listing) => listing,
                Err(err) => {
                    warn!("Failed to list directory({:?}): {:#}", path, err);
                    return forbidden();
                }
            };
            HttpResponse::Ok()
                .header(header::CONTENT_TYPE, "text/html; charset=utf-8")
                .body(listing)
        }
        ResolvedPath::Forbidden => forbidden(),
        ResolvedPath::NotFound => not_found(),
    }
}

#[actix_web::main]
async fn runtime(args: Args) -> Result<()> {
    let config = web::Data::new(Config {
        web_root: args.web_root.clone(),
        list_directories: args.list_directories,
    });
    let server = HttpServer::new(move || {
        App::new()
            .wrap(
                middleware::DefaultHeaders::new()
                    .header(header::DATE, "Thu, 01 Jan 1970 00:00:00 GMT")
                    .header(header::X_CONTENT_TYPE_OPTIONS, "nosniff")
                    .header(header::REFERRER_POLICY, "no-referrer"),
            )
            .wrap(middleware::Compress::default())
            .app_data(config.clone())
            .service(index)
    });

    let server = match args.bind_addr()? {
        TorAddress::Address(addr) => server.bind(addr),
        TorAddress::Unix(path) => server.bind_uds(&path),
        _ => unreachable!(),
    };

    server
        .context("Failed to setup server")?
        .run()
        .await
        .context("Failed to run http server")?;

    Ok(())
}

pub fn run(args: Args) -> Result<()> {
    if let Some(chroot) = &args.chroot {
        security::chroot(chroot).with_context(|| anyhow!("Failed to chroot into: {:?}", chroot))?;
        info!("Successfully chrooted into {:?}", chroot);
    }
    runtime(args)?;
    warn!("httpd thread has terminated");
    Ok(())
}

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

    #[test_case("", "/var/www/"; "root")]
    #[test_case("index.html", "/var/www/index.html"; "index.html")]
    #[test_case("a/b/c", "/var/www/a/b/c"; "a b c file")]
    #[test_case("a/b/c/", "/var/www/a/b/c/"; "a b c dir")]
    #[test_case("a//b//c", "/var/www/a/b/c"; "multiple slash")]
    #[test_case("a/b/index.html", "/var/www/a/b/index.html"; "sub index.html")]
    #[test_case("C:\\\\", "/var/www/C:\\\\"; "windows prefix")]
    #[test_case("\\", "/var/www/\\"; "backslash")]
    fn test_valid_resolve_path_req(x: &str, y: &str) {
        let resolved = resolve_path_req("/var/www/", Path::new(x)).unwrap();
        assert_eq!(resolved, Path::new(y));
    }

    #[test_case("./index.html"; "dot slash index.html")]
    #[test_case("/"; "redundant slash")]
    #[test_case("."; "current directory")]
    #[test_case(".."; "parent")]
    #[test_case("a/b/../c"; "a b parent c")]
    fn test_invalid_resolve_path_req(x: &str) {
        let result = resolve_path_req("/var/www/", Path::new(x));
        assert!(result.is_err());
    }
}