Skip to content

Commit 534937f

Browse files
Patches XSS & CSRF (#1078)
* Patched XSS & CSRF * Improve markdown rendering * wip --------- Co-authored-by: Dries Vints <[email protected]>
1 parent 5929a60 commit 534937f

File tree

16 files changed

+64
-93
lines changed

16 files changed

+64
-93
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ APP_NAME="Laravel.io"
22
APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true
5-
APP_URL=http://laravel.io.test
5+
APP_HOST=laravel.io.test
6+
APP_URL=http://${APP_HOST}
67

78
DB_DATABASE=laravel
89
DB_USERNAME=root

app/Http/Middleware/VerifyCsrfToken.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Http\Middleware;
44

55
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
6+
use Override;
7+
use Symfony\Component\HttpFoundation\Cookie;
68

79
class VerifyCsrfToken extends Middleware
810
{
@@ -14,4 +16,21 @@ class VerifyCsrfToken extends Middleware
1416
protected $except = [
1517
//
1618
];
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+
}
1736
}

app/Livewire/Editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getUsers($query): array
7373

7474
public function getPreviewProperty(): string
7575
{
76-
return replace_links(md_to_html($this->body ?: ''));
76+
return md_to_html($this->body ?: '');
7777
}
7878

7979
public function preview(): void

app/Markdown/LeagueConverter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
final class LeagueConverter implements Converter
88
{
9-
public function __construct(
10-
private MarkdownConverter $converter
11-
) {
9+
public function __construct(private MarkdownConverter $converter)
10+
{
1211
}
1312

1413
public function toHtml(string $markdown): string

app/Markdown/MarkdownServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\ServiceProvider;
66
use League\CommonMark\Environment\Environment;
77
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
8+
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
9+
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
810
use League\CommonMark\Extension\Mention\MentionExtension;
911
use League\CommonMark\MarkdownConverter;
1012

@@ -15,17 +17,26 @@ public function register(): void
1517
$this->app->singleton(Converter::class, function () {
1618
$environment = new Environment([
1719
'html_input' => 'escape',
20+
'max_nesting_level' => 10,
21+
'allow_unsafe_links' => false,
1822
'mentions' => [
1923
'username' => [
2024
'prefix' => '@',
2125
'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
2226
'generator' => config('app.url').'/user/%s',
2327
],
2428
],
29+
'external_link' => [
30+
'internal_hosts' => config('app.host'),
31+
'open_in_new_window' => true,
32+
'nofollow' => 'external',
33+
],
2534
]);
2635

2736
$environment->addExtension(new CommonMarkCoreExtension);
37+
$environment->addExtension(new GithubFlavoredMarkdownExtension);
2838
$environment->addExtension(new MentionExtension);
39+
$environment->addExtension(new ExternalLinkExtension);
2940

3041
return new LeagueConverter(new MarkdownConverter($environment));
3142
});

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
"spatie/laravel-schedule-monitor": "^3.1",
3737
"spatie/laravel-sitemap": "^7.1",
3838
"symfony/http-client": "^7.0",
39-
"symfony/mailgun-mailer": "^7.0",
40-
"yarri/link-finder": "^2.5"
39+
"symfony/mailgun-mailer": "^7.0"
4140
},
4241
"require-dev": {
4342
"fakerphp/faker": "^1.10",

composer.lock

Lines changed: 1 addition & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
|
5656
*/
5757

58+
'host' => env('APP_HOST', 'localhost'),
59+
5860
'url' => env('APP_URL', 'http://localhost'),
5961

6062
'asset_url' => env('ASSET_URL', null),

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<php>
1919
<env name="APP_ENV" value="testing"/>
2020
<env name="APP_KEY" value="base64:NXoQgjw2ZlOxnGbo5ZRhYgTdM6xLYsgYElNAgcTQJkE="/>
21+
<env name="APP_HOST" value="localhost"/>
2122
<env name="APP_URL" value="http://localhost"/>
2223
<env name="CACHE_DRIVER" value="array"/>
2324
<env name="DB_CONNECTION" value="sqlite"/>

resources/helpers.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function is_active(mixed $routes): bool
2222

2323
if (! function_exists('md_to_html')) {
2424
/**
25-
* Convert Markdown to HTML.
25+
* Converts Markdown to a safe HTML string.
2626
*/
2727
function md_to_html(string $markdown): string
2828
{
@@ -42,18 +42,6 @@ function route_to_reply_able(mixed $replyAble): string
4242
}
4343
}
4444

45-
if (! function_exists('replace_links')) {
46-
/**
47-
* Convert Standalone Urls to HTML.
48-
*/
49-
function replace_links(string $markdown): string
50-
{
51-
return (new LinkFinder([
52-
'attrs' => ['target' => '_blank', 'rel' => 'nofollow'],
53-
]))->processHtml($markdown);
54-
}
55-
}
56-
5745
if (! function_exists('canonical')) {
5846
/**
5947
* Generate a canonical URL to the given route and allowed list of query params.

0 commit comments

Comments
 (0)