spikes 0.4.0

Drop-in feedback collection for static HTML mockups
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
//! Security tests for the serve command
//!
//! Tests path traversal protection and CORS defaults

mod common;

use std::io::{Read, Write};
use std::net::TcpStream;
use std::process::{Child, Command, Stdio};
use std::time::Duration;
use std::sync::atomic::{AtomicU16, Ordering};

use reqwest::blocking::Client;
use reqwest::StatusCode;
use tempfile::TempDir;

static PORT_COUNTER: AtomicU16 = AtomicU16::new(3900u16);

/// Get a unique port for each test to avoid conflicts
fn get_unique_port() -> u16 {
    PORT_COUNTER.fetch_add(1, Ordering::SeqCst)
}

/// Helper to create a test directory with files
struct ServeTest {
    dir: TempDir,
    port: u16,
    server: Option<Child>,
    client: Client,
}

impl ServeTest {
    fn new() -> Self {
        let dir = tempfile::tempdir().expect("Failed to create temp dir");
        let port = get_unique_port();
        let client = Client::builder()
            .timeout(Duration::from_secs(5))
            .danger_accept_invalid_certs(true)
            .build()
            .expect("Failed to create client");
        
        Self {
            dir,
            port,
            server: None,
            client,
        }
    }

    fn add_file(&self, name: &str, content: &str) -> std::path::PathBuf {
        let path = self.dir.path().join(name);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).ok();
        }
        std::fs::write(&path, content).expect("Failed to write file");
        path
    }

    #[allow(dead_code)]
    fn add_secret_file(&self, content: &str) -> std::path::PathBuf {
        // Add a file outside the serve directory (in temp dir parent)
        let secret_path = self.dir.path().parent().unwrap().join("secret.txt");
        std::fs::write(&secret_path, content).expect("Failed to write secret file");
        secret_path
    }

    fn start_server(&mut self, cors_origin: Option<&str>) {
        let mut cmd = Command::new(env!("CARGO_BIN_EXE_spikes"));
        cmd.arg("serve")
            .arg("--port").arg(self.port.to_string())
            .arg("--dir").arg(self.dir.path().to_string_lossy().to_string());
        
        if let Some(origin) = cors_origin {
            cmd.arg("--cors-allow-origin").arg(origin);
        }
        
        // Capture stderr for debugging, but stdout can be null
        cmd.stdout(Stdio::null())
            .stderr(Stdio::piped());
        
        self.server = Some(cmd.spawn().expect("Failed to start server"));
        
        // Wait for server to be ready
        let url = format!("http://localhost:{}", self.port);
        for _ in 0..50 {
            std::thread::sleep(Duration::from_millis(100));
            if self.client.get(format!("{}/index.html", url)).send().is_ok() {
                return;
            }
        }
        panic!("Server failed to start within timeout");
    }

    fn url(&self) -> String {
        format!("http://localhost:{}", self.port)
    }

    fn get(&self, path: &str) -> reqwest::blocking::Response {
        self.client
            .get(format!("{}{}", self.url(), path))
            .send()
            .expect("Request failed")
    }

    fn get_with_origin(&self, path: &str, origin: &str) -> reqwest::blocking::Response {
        self.client
            .get(format!("{}{}", self.url(), path))
            .header("Origin", origin)
            .send()
            .expect("Request failed")
    }

    /// Send a raw HTTP request without path normalization
    fn raw_get(&self, path: &str) -> (StatusCode, String, std::collections::HashMap<String, String>) {
        let mut stream = TcpStream::connect(format!("127.0.0.1:{}", self.port))
            .expect("Failed to connect");
        stream.set_read_timeout(Some(Duration::from_secs(5))).ok();
        stream.set_write_timeout(Some(Duration::from_secs(5))).ok();
        
        let request = format!("GET {} HTTP/1.1\r\nHost: localhost:{}\r\nConnection: close\r\n\r\n", path, self.port);
        stream.write_all(request.as_bytes()).expect("Failed to write request");
        
        let mut response = String::new();
        stream.read_to_string(&mut response).expect("Failed to read response");
        
        // Parse response
        let mut headers = std::collections::HashMap::new();
        let mut body = String::new();
        let mut status = StatusCode::OK;
        
        let mut lines = response.lines();
        if let Some(status_line) = lines.next() {
            if let Some(code) = status_line.split_whitespace().nth(1) {
                if let Ok(code) = code.parse::<u16>() {
                    status = StatusCode::from_u16(code).unwrap_or(StatusCode::OK);
                }
            }
        }
        
        let mut in_body = false;
        for line in lines {
            if in_body {
                body.push_str(line);
            } else if line.is_empty() {
                in_body = true;
            } else if let Some((key, value)) = line.split_once(':') {
                headers.insert(key.trim().to_lowercase(), value.trim().to_string());
            }
        }
        
        (status, body, headers)
    }
}

impl Drop for ServeTest {
    fn drop(&mut self) {
        if let Some(mut child) = self.server.take() {
            let _ = child.kill();
            let _ = child.wait();
        }
    }
}

#[test]
fn test_serve_legitimate_file_succeeds() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Hello</body></html>");
    test.start_server(None);

    let resp = test.get("/index.html");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = resp.text().unwrap();
    assert!(body.contains("Hello"));
}

#[test]
fn test_serve_index_html_as_default() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Index</body></html>");
    test.start_server(None);

    let resp = test.get("/");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = resp.text().unwrap();
    assert!(body.contains("Index"));
}

#[test]
fn test_path_traversal_blocked_dotdot_slash() {
    let mut test = ServeTest::new();
    test.add_file("public/index.html", "<html><body>Public</body></html>");
    
    // Create a secret file outside the public directory
    let secret_path = test.dir.path().join("secret.txt");
    std::fs::write(&secret_path, "SECRET CONTENT").unwrap();
    
    // Also create a config file in .spikes that shouldn't be accessible via traversal
    let spikes_dir = test.dir.path().join("public/.spikes");
    std::fs::create_dir_all(&spikes_dir).unwrap();
    std::fs::write(spikes_dir.join("config.toml"), "secret=config").unwrap();
    
    // Start server serving the public directory
    let public_dir = test.dir.path().join("public");
    let port = test.port;
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_spikes"));
    cmd.arg("serve")
        .arg("--port").arg(port.to_string())
        .arg("--dir").arg(public_dir.to_string_lossy().to_string())
        .stdout(Stdio::null())
        .stderr(Stdio::piped());
    test.server = Some(cmd.spawn().expect("Failed to start server"));
    
    // Wait for server
    for _ in 0..50 {
        std::thread::sleep(Duration::from_millis(100));
        if test.client.get(format!("http://localhost:{}/index.html", port)).send().is_ok() {
            break;
        }
    }
    
    // Use raw TCP to send path without normalization
    let (status, body, _headers) = test.raw_get("/../secret.txt");
    
    assert!(
        status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
        "Path traversal should be blocked, got status: {}",
        status
    );
    
    // Verify the secret content was NOT returned
    assert!(!body.contains("SECRET"), "Secret content should not be exposed");
}

#[test]
fn test_path_traversal_blocked_multiple_dotdot() {
    let mut test = ServeTest::new();
    test.add_file("public/index.html", "<html><body>Public</body></html>");
    
    let public_dir = test.dir.path().join("public");
    let port = test.port;
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_spikes"));
    cmd.arg("serve")
        .arg("--port").arg(port.to_string())
        .arg("--dir").arg(public_dir.to_string_lossy().to_string())
        .stdout(Stdio::null())
        .stderr(Stdio::piped());
    test.server = Some(cmd.spawn().expect("Failed to start server"));
    
    for _ in 0..50 {
        std::thread::sleep(Duration::from_millis(100));
        if test.client.get(format!("http://localhost:{}/index.html", port)).send().is_ok() {
            break;
        }
    }
    
    // Use raw TCP to send deep path traversal
    let (status, _body, _headers) = test.raw_get("/../../Cargo.toml");

    assert!(
        status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
        "Deep path traversal should be blocked, got status: {}",
        status
    );
}

#[test]
fn test_path_traversal_blocked_backslash() {
    let mut test = ServeTest::new();
    test.add_file("public/index.html", "<html><body>Public</body></html>");
    
    let public_dir = test.dir.path().join("public");
    let port = test.port;
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_spikes"));
    cmd.arg("serve")
        .arg("--port").arg(port.to_string())
        .arg("--dir").arg(public_dir.to_string_lossy().to_string())
        .stdout(Stdio::null())
        .stderr(Stdio::piped());
    test.server = Some(cmd.spawn().expect("Failed to start server"));
    
    for _ in 0..50 {
        std::thread::sleep(Duration::from_millis(100));
        if test.client.get(format!("http://localhost:{}/index.html", port)).send().is_ok() {
            break;
        }
    }
    
    // Use raw TCP to send backslash path traversal
    let (status, _body, _headers) = test.raw_get("/..\\..\\Cargo.toml");

    assert!(
        status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
        "Backslash path traversal should be blocked, got status: {}",
        status
    );
}

#[test]
fn test_path_traversal_blocked_spikes_config() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Public</body></html>");
    
    // Create a .spikes directory with config
    let spikes_dir = test.dir.path().join(".spikes");
    std::fs::create_dir_all(&spikes_dir).unwrap();
    std::fs::write(spikes_dir.join("config.toml"), "[project]\nkey = \"secret-project\"").unwrap();
    
    test.start_server(None);
    
    // Use raw TCP to send path without normalization
    // HTTP clients often normalize paths before sending
    let (status, body, _headers) = test.raw_get("/../.spikes/config.toml");
    
    eprintln!("Raw request to /../.spikes/config.toml");
    eprintln!("Status: {}", status);
    eprintln!("Body: {}", body);
    
    assert!(
        status == StatusCode::FORBIDDEN || status == StatusCode::NOT_FOUND,
        "Access to .spikes via traversal should be blocked, got status: {}",
        status
    );
    
    // Verify the secret content was NOT returned
    assert!(!body.contains("secret-project"), "Secret content should not be exposed");
}

#[test]
fn test_cors_no_headers_without_flag() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Hello</body></html>");
    test.start_server(None); // No CORS flag

    let resp = test.get_with_origin("/index.html", "http://evil.com");
    
    // Should NOT have Access-Control-Allow-Origin header
    assert!(
        resp.headers().get("Access-Control-Allow-Origin").is_none(),
        "CORS header should not be present without flag"
    );
}

#[test]
fn test_cors_no_wildcard_without_flag() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Hello</body></html>");
    test.start_server(None);

    let resp = test.get("/index.html");
    
    // Should NOT have wildcard CORS
    let cors_header = resp.headers().get("Access-Control-Allow-Origin");
    assert!(
        cors_header.is_none() || cors_header.unwrap() != "*",
        "Wildcard CORS should not be present without flag"
    );
}

#[test]
fn test_cors_allowed_with_flag() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Hello</body></html>");
    test.start_server(Some("https://spikes.sh"));

    let resp = test.get_with_origin("/index.html", "https://spikes.sh");
    
    // Should have the matching CORS header
    let cors_header = resp.headers().get("Access-Control-Allow-Origin");
    assert!(
        cors_header.is_some(),
        "CORS header should be present with flag"
    );
    assert_eq!(
        cors_header.unwrap().to_str().unwrap(),
        "https://spikes.sh",
        "CORS header should match allowed origin"
    );
}

#[test]
fn test_cors_rejects_other_origin() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Hello</body></html>");
    test.start_server(Some("https://spikes.sh"));

    // Request with different origin
    let resp = test.get_with_origin("/index.html", "http://evil.com");
    
    // Should NOT echo the evil.com origin
    if let Some(cors_header) = resp.headers().get("Access-Control-Allow-Origin") {
        assert_ne!(
            cors_header.to_str().unwrap(),
            "http://evil.com",
            "CORS header should not echo untrusted origin"
        );
    }
    // The response may still succeed (file is returned), but CORS headers shouldn't expose
}

#[test]
fn test_nested_file_served_correctly() {
    let mut test = ServeTest::new();
    test.add_file("assets/css/style.css", "body { color: red; }");
    test.start_server(None);

    let resp = test.get("/assets/css/style.css");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = resp.text().unwrap();
    assert!(body.contains("color: red"));
}

#[test]
fn test_nonexistent_file_returns_404() {
    let mut test = ServeTest::new();
    test.add_file("index.html", "<html><body>Hello</body></html>");
    test.start_server(None);

    let resp = test.get("/nonexistent.html");
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}