rustf 1.0.0

A simple MVC web framework inspired by Total.js
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
//! Secure static file serving with path traversal protection
//!
//! This module provides secure static file serving functionality that prevents
//! path traversal attacks, validates file types, and implements content security measures.

use super::{PathValidator, SecurityConfig};
use crate::error::{Error, Result};
use crate::http::{Request, Response};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

/// Secure static file server
pub struct SecureStaticFileServer {
    validator: PathValidator,
    _enable_directory_listing: bool,
    cache_max_age: u32,
}

impl SecureStaticFileServer {
    /// Create a new secure static file server
    pub fn new(base_directory: impl AsRef<Path>, config: &SecurityConfig) -> Result<Self> {
        let validator = PathValidator::new(base_directory, config)?;

        Ok(Self {
            validator,
            _enable_directory_listing: false, // Disabled by default for security
            cache_max_age: 3600,              // 1 hour default cache
        })
    }

    /// Serve a static file securely
    pub async fn serve_file(&self, request_path: &str) -> Result<Response> {
        // Validate and resolve the path
        let file_path = self.validator.validate_path(request_path)?;

        // Check if file is safe to serve
        if !self.validator.is_safe_file(&file_path)? {
            return Err(Error::template("File cannot be served safely".to_string()));
        }

        // Read file content
        let content = tokio::fs::read(&file_path)
            .await
            .map_err(|e| Error::template(format!("Failed to read file: {}", e)))?;

        // Determine MIME type
        let mime_type = self.detect_mime_type(&file_path);

        // Get file metadata for headers
        let metadata = std::fs::metadata(&file_path)
            .map_err(|e| Error::template(format!("Failed to read file metadata: {}", e)))?;

        let last_modified = metadata
            .modified()
            .unwrap_or(SystemTime::UNIX_EPOCH)
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        // Build secure response
        let mut response = Response::ok();

        // Set content type
        response = response.with_header("Content-Type", mime_type);

        // Set security headers
        response = response.with_header("X-Content-Type-Options", "nosniff");
        response = response.with_header("X-Frame-Options", "DENY");

        // Set cache headers
        response = response.with_header(
            "Cache-Control",
            &format!("public, max-age={}", self.cache_max_age),
        );
        response = response.with_header("Last-Modified", &Self::format_http_date(last_modified));

        // Set content length
        response = response.with_header("Content-Length", &content.len().to_string());

        // For potentially dangerous file types, force download
        if self.should_force_download(&file_path) {
            let filename = file_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("download");

            response = response.with_header(
                "Content-Disposition",
                &format!("attachment; filename=\"{}\"", filename),
            );
        }

        Ok(response.with_body(content))
    }

    /// Check if file should be forced as download
    fn should_force_download(&self, path: &Path) -> bool {
        if let Some(extension) = path.extension().and_then(|s| s.to_str()) {
            let ext = extension.to_lowercase();
            matches!(
                ext.as_str(),
                "pdf"
                    | "doc"
                    | "docx"
                    | "xls"
                    | "xlsx"
                    | "ppt"
                    | "pptx"
                    | "zip"
                    | "rar"
                    | "7z"
                    | "tar"
                    | "gz"
                    | "exe"
                    | "dmg"
            )
        } else {
            true // Force download for files without extension
        }
    }

    /// Detect MIME type based on file extension
    fn detect_mime_type(&self, path: &Path) -> &'static str {
        if let Some(extension) = path.extension().and_then(|s| s.to_str()) {
            match extension.to_lowercase().as_str() {
                // Text files
                "html" | "htm" => "text/html; charset=utf-8",
                "css" => "text/css; charset=utf-8",
                "js" => "application/javascript; charset=utf-8",
                "json" => "application/json; charset=utf-8",
                "txt" => "text/plain; charset=utf-8",
                "xml" => "application/xml; charset=utf-8",
                "md" => "text/markdown; charset=utf-8",

                // Images
                "png" => "image/png",
                "jpg" | "jpeg" => "image/jpeg",
                "gif" => "image/gif",
                "svg" => "image/svg+xml",
                "ico" => "image/x-icon",
                "webp" => "image/webp",

                // Fonts
                "woff" => "font/woff",
                "woff2" => "font/woff2",
                "ttf" => "font/ttf",
                "eot" => "application/vnd.ms-fontobject",

                // Documents
                "pdf" => "application/pdf",
                "doc" => "application/msword",
                "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "xls" => "application/vnd.ms-excel",
                "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",

                // Archives
                "zip" => "application/zip",
                "rar" => "application/x-rar-compressed",
                "7z" => "application/x-7z-compressed",
                "tar" => "application/x-tar",
                "gz" => "application/gzip",

                _ => "application/octet-stream",
            }
        } else {
            "application/octet-stream"
        }
    }

    /// Format timestamp as an RFC 7231 HTTP date string.
    fn format_http_date(timestamp: u64) -> String {
        crate::utils::http_date::format_http_date(timestamp)
    }

    /// Handle conditional requests (If-Modified-Since, ETag)
    pub fn handle_conditional_request(
        &self,
        request: &Request,
        file_path: &Path,
    ) -> Result<Option<Response>> {
        // Check If-Modified-Since header
        if let Some(if_modified_since_header) = request.headers.get("if-modified-since") {
            let metadata = std::fs::metadata(file_path)
                .map_err(|e| Error::template(format!("Failed to read file metadata: {}", e)))?;

            let last_modified = metadata
                .modified()
                .unwrap_or(SystemTime::UNIX_EPOCH)
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();

            // Parse the If-Modified-Since header (basic HTTP date parsing)
            if let Some(if_modified_timestamp) = Self::parse_http_date(if_modified_since_header) {
                // If file hasn't been modified since the client's cache date, return 304
                if last_modified <= if_modified_timestamp {
                    return Ok(Some(Response::not_modified()));
                }
            }
        }

        Ok(None)
    }

    /// Parse HTTP date string to timestamp (RFC 7231/2616 compliant)
    fn parse_http_date(date_str: &str) -> Option<u64> {
        crate::utils::http_date::parse_http_date(date_str)
    }
}

/// Static file middleware for automatic static file serving
pub struct StaticFileMiddleware {
    server: SecureStaticFileServer,
    url_prefix: String,
}

impl StaticFileMiddleware {
    /// Create new static file middleware
    pub fn new(
        base_directory: impl AsRef<Path>,
        url_prefix: &str,
        config: &SecurityConfig,
    ) -> Result<Self> {
        let server = SecureStaticFileServer::new(base_directory, config)?;

        Ok(Self {
            server,
            url_prefix: url_prefix.trim_end_matches('/').to_string(),
        })
    }

    /// Check if request should be handled by static file middleware
    pub fn should_handle(&self, request_path: &str) -> bool {
        if self.url_prefix.is_empty() {
            return true;
        }

        // Must start with prefix and have content after it
        request_path.starts_with(&self.url_prefix)
            && request_path.len() > self.url_prefix.len()
            && request_path.chars().nth(self.url_prefix.len()) == Some('/')
    }

    /// Handle static file request
    pub async fn handle_request(&self, request_path: &str) -> Result<Response> {
        // Remove prefix from path
        let file_path = if self.url_prefix.is_empty() {
            request_path.to_string()
        } else {
            request_path
                .strip_prefix(&self.url_prefix)
                .unwrap_or(request_path)
                .to_string()
        };

        self.server.serve_file(&file_path).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_secure_static_file_server() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        // Create test files
        fs::write(
            base_path.join("test.html"),
            "<html><body>Test</body></html>",
        )
        .unwrap();
        fs::write(base_path.join("style.css"), "body { color: red; }").unwrap();

        let config = SecurityConfig::default();
        let server = SecureStaticFileServer::new(base_path, &config).unwrap();

        // Test MIME type detection
        assert_eq!(
            server.detect_mime_type(&base_path.join("test.html")),
            "text/html; charset=utf-8"
        );
        assert_eq!(
            server.detect_mime_type(&base_path.join("style.css")),
            "text/css; charset=utf-8"
        );
        assert_eq!(
            server.detect_mime_type(&base_path.join("unknown.xyz")),
            "application/octet-stream"
        );
    }

    #[test]
    fn test_static_file_middleware() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        fs::write(base_path.join("app.js"), "console.log('test');").unwrap();

        let config = SecurityConfig::default();
        let middleware = StaticFileMiddleware::new(base_path, "/static", &config).unwrap();

        // Test path matching
        assert!(middleware.should_handle("/static/app.js"));
        assert!(!middleware.should_handle("/api/data"));
        assert!(!middleware.should_handle("/static"));
    }

    #[tokio::test]
    async fn test_path_traversal_prevention() {
        let temp_dir = TempDir::new().unwrap();
        let base_path = temp_dir.path();

        fs::write(base_path.join("safe.txt"), "safe content").unwrap();

        let config = SecurityConfig::default();
        let server = SecureStaticFileServer::new(base_path, &config).unwrap();

        // Test that path traversal attempts are blocked
        assert!(server.serve_file("../../../etc/passwd").await.is_err());
        assert!(server
            .serve_file("..\\..\\windows\\system32\\config\\sam")
            .await
            .is_err());
        assert!(server
            .serve_file("safe.txt/../../../etc/passwd")
            .await
            .is_err());

        // Test that safe files can be served
        assert!(server.serve_file("safe.txt").await.is_ok());
    }

    #[test]
    fn test_http_date_formatting() {
        // Test with known timestamp: 1994-11-06 08:49:37 GMT
        let dt_1994 = chrono::Utc
            .with_ymd_and_hms(1994, 11, 6, 8, 49, 37)
            .unwrap();
        let timestamp_1994 = dt_1994.timestamp() as u64;
        let formatted = SecureStaticFileServer::format_http_date(timestamp_1994);
        assert_eq!(formatted, "Sun, 06 Nov 1994 08:49:37 GMT");

        // Test with Unix epoch
        let formatted_epoch = SecureStaticFileServer::format_http_date(0);
        assert_eq!(formatted_epoch, "Thu, 01 Jan 1970 00:00:00 GMT");

        // Test with leap year date: 2000-02-29 12:00:00 GMT
        let dt_leap = chrono::Utc.with_ymd_and_hms(2000, 2, 29, 12, 0, 0).unwrap();
        let leap_year_timestamp = dt_leap.timestamp() as u64;
        let formatted_leap = SecureStaticFileServer::format_http_date(leap_year_timestamp);
        assert_eq!(formatted_leap, "Tue, 29 Feb 2000 12:00:00 GMT");

        // Test with recent date: 2023-12-25 15:30:45 GMT
        let dt_recent = chrono::Utc
            .with_ymd_and_hms(2023, 12, 25, 15, 30, 45)
            .unwrap();
        let recent_timestamp = dt_recent.timestamp() as u64;
        let formatted_recent = SecureStaticFileServer::format_http_date(recent_timestamp);
        assert_eq!(formatted_recent, "Mon, 25 Dec 2023 15:30:45 GMT");
    }

    #[test]
    fn test_http_date_parsing() {
        // Calculate correct expected timestamp
        let expected_dt = chrono::Utc
            .with_ymd_and_hms(1994, 11, 6, 8, 49, 37)
            .unwrap();
        let expected_timestamp = expected_dt.timestamp() as u64;

        // Test RFC 7231 format: "Sun, 06 Nov 1994 08:49:37 GMT"
        let rfc7231_date = "Sun, 06 Nov 1994 08:49:37 GMT";
        let parsed_rfc7231 = SecureStaticFileServer::parse_http_date(rfc7231_date);
        assert_eq!(parsed_rfc7231, Some(expected_timestamp));

        // Test RFC 850 format: "Sunday, 06-Nov-94 08:49:37 GMT"
        let rfc850_date = "Sunday, 06-Nov-94 08:49:37 GMT";
        let parsed_rfc850 = SecureStaticFileServer::parse_http_date(rfc850_date);
        assert_eq!(parsed_rfc850, Some(expected_timestamp));

        // Test asctime() format: "Sun Nov  6 08:49:37 1994"
        let asctime_date = "Sun Nov  6 08:49:37 1994";
        let parsed_asctime = SecureStaticFileServer::parse_http_date(asctime_date);
        assert_eq!(parsed_asctime, Some(expected_timestamp));

        // Test Unix epoch
        let epoch_date = "Thu, 01 Jan 1970 00:00:00 GMT";
        let parsed_epoch = SecureStaticFileServer::parse_http_date(epoch_date);
        assert_eq!(parsed_epoch, Some(0));

        // Test invalid date formats
        assert_eq!(
            SecureStaticFileServer::parse_http_date("invalid date"),
            None
        );
        assert_eq!(SecureStaticFileServer::parse_http_date(""), None);
        assert_eq!(
            SecureStaticFileServer::parse_http_date("Not a date at all"),
            None
        );

        // Test date before Unix epoch (should return None)
        let pre_epoch_date = "Wed, 31 Dec 1969 23:59:59 GMT";
        assert_eq!(
            SecureStaticFileServer::parse_http_date(pre_epoch_date),
            None
        );
    }

    #[test]
    fn test_http_date_roundtrip() {
        // Test that formatting and parsing are consistent
        let test_datetimes = [
            chrono::Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap(), // Unix epoch
            chrono::Utc
                .with_ymd_and_hms(1994, 11, 6, 8, 49, 37)
                .unwrap(), // 1994-11-06 08:49:37 GMT
            chrono::Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(), // 2000-01-01 00:00:00 GMT
            chrono::Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap(), // 2020-01-01 00:00:00 GMT
            chrono::Utc
                .with_ymd_and_hms(2023, 12, 25, 15, 30, 45)
                .unwrap(), // 2023-12-25 15:30:45 GMT
        ];

        for dt in &test_datetimes {
            let timestamp = dt.timestamp() as u64;
            let formatted = SecureStaticFileServer::format_http_date(timestamp);
            let parsed = SecureStaticFileServer::parse_http_date(&formatted);
            assert_eq!(
                parsed,
                Some(timestamp),
                "Roundtrip failed for timestamp {}: formatted='{}', parsed={:?}",
                timestamp,
                formatted,
                parsed
            );
        }
    }

    #[test]
    fn test_http_date_edge_cases() {
        // Test leap year dates with correct timestamps
        let leap_years = [
            (
                "Sat, 29 Feb 1992 12:00:00 GMT",
                chrono::Utc.with_ymd_and_hms(1992, 2, 29, 12, 0, 0).unwrap(),
            ),
            (
                "Tue, 29 Feb 2000 12:00:00 GMT",
                chrono::Utc.with_ymd_and_hms(2000, 2, 29, 12, 0, 0).unwrap(),
            ),
            (
                "Mon, 29 Feb 2016 12:00:00 GMT",
                chrono::Utc.with_ymd_and_hms(2016, 2, 29, 12, 0, 0).unwrap(),
            ),
        ];

        for (date_str, expected_dt) in &leap_years {
            let expected_timestamp = expected_dt.timestamp() as u64;
            let parsed = SecureStaticFileServer::parse_http_date(date_str);
            assert_eq!(
                parsed,
                Some(expected_timestamp),
                "Failed to parse leap year date: {}",
                date_str
            );

            let formatted = SecureStaticFileServer::format_http_date(expected_timestamp);
            assert_eq!(
                &formatted, date_str,
                "Formatting mismatch for timestamp {}",
                expected_timestamp
            );
        }

        // Test century boundary dates
        let dt_1999 = chrono::Utc
            .with_ymd_and_hms(1999, 12, 31, 23, 59, 59)
            .unwrap();
        let formatted_1999 = SecureStaticFileServer::format_http_date(dt_1999.timestamp() as u64);
        assert_eq!(formatted_1999, "Fri, 31 Dec 1999 23:59:59 GMT");

        let dt_2000 = chrono::Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap();
        let formatted_2000 = SecureStaticFileServer::format_http_date(dt_2000.timestamp() as u64);
        assert_eq!(formatted_2000, "Sat, 01 Jan 2000 00:00:00 GMT");
    }
}