File size: 2,861 Bytes
27cd9bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
diff --git a/src/Http/Response.php b/src/Http/Response.php
index 984aed4a56..9146832729 100644
--- a/src/Http/Response.php
+++ b/src/Http/Response.php
@@ -176,6 +176,18 @@ public static function file(string $file, array $props = []): static
 			'type' => F::extensionToMime(F::extension($file))
 		], $props);
 
+		// if we couldn't serve a correct MIME type, force
+		// the browser to display the file as plain text to
+		// harden against attacks from malicious file uploads
+		if ($props['type'] === null) {
+			if (isset($props['headers']) !== true) {
+				$props['headers'] = [];
+			}
+
+			$props['type'] = 'text/plain';
+			$props['headers']['X-Content-Type-Options'] = 'nosniff';
+		}
+
 		return new static($props);
 	}
 
diff --git a/tests/Http/ResponseTest.php b/tests/Http/ResponseTest.php
index 0ba52fa787..efbd6e359e 100644
--- a/tests/Http/ResponseTest.php
+++ b/tests/Http/ResponseTest.php
@@ -188,13 +188,41 @@ public function testJsonWithPrettyArray()
 
 	public function testFile()
 	{
-		$file = __DIR__ . '/fixtures/download.txt';
+		$file = __DIR__ . '/fixtures/download.json';
+
+		$response = Response::file($file);
+
+		$this->assertSame('application/json', $response->type());
+		$this->assertSame(200, $response->code());
+		$this->assertSame('{"foo": "bar"}', $response->body());
+
+		$response = Response::file($file, [
+			'code'    => '201',
+			'headers' => [
+				'Pragma' => 'no-cache'
+			]
+		]);
+
+		$this->assertSame('application/json', $response->type());
+		$this->assertSame(201, $response->code());
+		$this->assertSame('{"foo": "bar"}', $response->body());
+		$this->assertSame([
+			'Pragma' => 'no-cache'
+		], $response->headers());
+	}
+
+	public function testFileInvalid()
+	{
+		$file = __DIR__ . '/fixtures/download.xyz';
 
 		$response = Response::file($file);
 
 		$this->assertSame('text/plain', $response->type());
 		$this->assertSame(200, $response->code());
 		$this->assertSame('test', $response->body());
+		$this->assertSame([
+			'X-Content-Type-Options' => 'nosniff',
+		], $response->headers());
 
 		$response = Response::file($file, [
 			'code'    => '201',
@@ -207,7 +235,8 @@ public function testFile()
 		$this->assertSame(201, $response->code());
 		$this->assertSame('test', $response->body());
 		$this->assertSame([
-			'Pragma' => 'no-cache'
+			'Pragma' => 'no-cache',
+			'X-Content-Type-Options' => 'nosniff',
 		], $response->headers());
 	}
 
diff --git a/tests/Http/fixtures/download.json b/tests/Http/fixtures/download.json
new file mode 100644
index 0000000000..18d7acf586
--- /dev/null
+++ b/tests/Http/fixtures/download.json
@@ -0,0 +1 @@
+{"foo": "bar"}
\ No newline at end of file
diff --git a/tests/Http/fixtures/download.txt b/tests/Http/fixtures/download.xyz
similarity index 100%
rename from tests/Http/fixtures/download.txt
rename to tests/Http/fixtures/download.xyz