File tree Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 3
3
namespace App \Http \Middleware ;
4
4
5
5
use Illuminate \Foundation \Http \Middleware \VerifyCsrfToken as Middleware ;
6
+ use Override ;
7
+ use Symfony \Component \HttpFoundation \Cookie ;
6
8
7
9
class VerifyCsrfToken extends Middleware
8
10
{
@@ -14,4 +16,21 @@ class VerifyCsrfToken extends Middleware
14
16
protected $ except = [
15
17
//
16
18
];
19
+
20
+ #[Override]
21
+ protected function newCookie ($ request , $ config )
22
+ {
23
+ return new Cookie (
24
+ 'XSRF-TOKEN ' ,
25
+ $ request ->session ()->token (),
26
+ $ this ->availableAt (60 * $ config ['lifetime ' ]),
27
+ $ config ['path ' ],
28
+ $ config ['domain ' ],
29
+ $ config ['secure ' ],
30
+ true ,
31
+ false ,
32
+ $ config ['same_site ' ] ?? null ,
33
+ $ config ['partitioned ' ] ?? false
34
+ );
35
+ }
17
36
}
Original file line number Diff line number Diff line change @@ -54,6 +54,20 @@ function replace_links(string $markdown): string
54
54
}
55
55
}
56
56
57
+ if (! function_exists ('md_to_safe_html ' )) {
58
+ /**
59
+ * Converts Markdown to a safe HTML string.
60
+ */
61
+ function md_to_safe_html (string $ markdown ): string
62
+ {
63
+ return str ($ markdown )->markdown ([
64
+ 'html_input ' => 'escape ' ,
65
+ 'max_nesting_level ' => 10 ,
66
+ 'allow_unsafe_links ' => false ,
67
+ ])->toString ();
68
+ }
69
+ }
70
+
57
71
if (! function_exists ('canonical ' )) {
58
72
/**
59
73
* Generate a canonical URL to the given route and allowed list of query params.
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ class="w-full bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray
106
106
x-init =" $nextTick(function () { highlightCode($el); })"
107
107
class =" prose prose-lg text-gray-800 prose-lio"
108
108
>
109
- <x-buk-markdown >{!! $article -> body ( ) ! !} </x-buk-markdown >
109
+ <x-buk-markdown >{!! md_to_safe_html ( $article -> body ) ! !} </x-buk-markdown >
110
110
</div >
111
111
112
112
@if ($article -> isUpdated () )
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ test ('converts markdown to safe html ' , function () {
4
+ $ body = 'Hello, World! . ' ;
5
+
6
+ expect (md_to_safe_html ($ body ))->toBe ('<p>Hello, World! <img src="image.png" alt="" />.</p> ' . "\n" );
7
+ });
8
+
9
+ test ('prevents unsafe links ' , function () {
10
+ $ body = "[Unsafe Link](javascript:alert('Hello')) " ;
11
+
12
+ expect (md_to_safe_html ($ body ))->toBe ('<p><a>Unsafe Link</a></p> ' . "\n" );
13
+ });
You can’t perform that action at this time.
0 commit comments