Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Controllers/UpdateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function store(Request $request): RedirectResponse
$this->authorize('manage-updates');

$request->validate([
'description' => 'required|string',
'link' => 'nullable|url',
'description' => ['required', 'string', 'max:5000'],
'link' => ['nullable', 'url:http,https', 'max:255'],
]);

Update::create($request->only('description', 'link'));
Expand Down
13 changes: 13 additions & 0 deletions app/Models/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ class Update extends Model
'description',
'link',
];

/**
* Return only browser-safe external links, including for legacy records.
*/
public function safeExternalLink(): ?string
{
$link = trim((string) $this->link);
$scheme = strtolower((string) parse_url($link, PHP_URL_SCHEME));

return $link !== '' && in_array($scheme, ['http', 'https'], true)
? $link
: null;
}
}
7 changes: 4 additions & 3 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
@if ($updates->isNotEmpty())
<div class="dashboard-updates-list">
@foreach ($updates as $update)
@php($safeUpdateLink = $update->safeExternalLink())
<article class="dashboard-update-item">
<div class="dashboard-update-item__content">
<div class="dashboard-update-item__main">
Expand All @@ -191,8 +192,8 @@
<p class="dashboard-update-item__text mb-0">{{ $update->description }}</p>
</div>

@if ($update->link)
<a href="{{ $update->link }}" target="_blank" rel="noopener" class="dashboard-inline-link dashboard-update-item__link">
@if ($safeUpdateLink)
<a href="{{ $safeUpdateLink }}" target="_blank" rel="noopener noreferrer" class="dashboard-inline-link dashboard-update-item__link">
{{ __('dashboard.updates_open_link') }}
<i class="fas fa-up-right-from-square ms-1"></i>
</a>
Expand Down Expand Up @@ -226,7 +227,7 @@
</div>

<div class="dashboard-resource-grid">
<a href="https://labschool-gr.github.io/Exams/learn.html" class="dashboard-resource-link" target="_blank" rel="noopener">
<a href="https://labschool-gr.github.io/Exams/learn.html" class="dashboard-resource-link" target="_blank" rel="noopener noreferrer">
<span class="dashboard-resource-link__icon">
<i class="fas fa-book-open"></i>
</span>
Expand Down
5 changes: 3 additions & 2 deletions resources/views/updates/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
@else
<div class="dashboard-updates-list">
@foreach ($updates as $index => $update)
@php($safeUpdateLink = $update->safeExternalLink())
<article class="dashboard-update-item">
<div class="dashboard-update-item__content">
<div class="dashboard-update-item__main">
Expand All @@ -54,8 +55,8 @@
</div>

<div class="dashboard-form-actions dashboard-form-actions--end">
@if ($update->link)
<a href="{{ $update->link }}" target="_blank" rel="noopener" class="btn dashboard-btn dashboard-btn--ghost">
@if ($safeUpdateLink)
<a href="{{ $safeUpdateLink }}" target="_blank" rel="noopener noreferrer" class="btn dashboard-btn dashboard-btn--ghost">
<i class="fas fa-up-right-from-square me-2"></i>{{ __('dashboard.updates_open_link') }}
</a>
@endif
Expand Down
50 changes: 50 additions & 0 deletions tests/Feature/AdminModuleAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,56 @@
->assertForbidden();
});

it('accepts only http and https links for updates', function () {
$admin = User::factory()->create([
'role' => 'admin',
]);

$this->actingAs($admin)
->post(route('updates.store'), [
'description' => 'Unsafe external link',
'link' => 'file://example.com/release-notes',
])
->assertSessionHasErrors('link');

$this->assertDatabaseMissing('updates', [
'description' => 'Unsafe external link',
]);

$this->actingAs($admin)
->post(route('updates.store'), [
'description' => 'Safe external link',
'link' => 'https://example.com/release-notes',
])
->assertRedirect(route('updates.index', absolute: false));

$this->assertDatabaseHas('updates', [
'description' => 'Safe external link',
'link' => 'https://example.com/release-notes',
]);
});

it('does not render legacy update links with unsafe protocols', function () {
$teacher = User::factory()->create([
'role' => 'teacher',
]);

Update::create([
'description' => 'Legacy unsafe link',
'link' => 'file://example.com/release-notes',
]);

$this->actingAs($teacher)
->get(route('dashboard'))
->assertOk()
->assertDontSee('href="file://example.com/release-notes"', false);

$this->actingAs($teacher)
->get(route('updates.index'))
->assertOk()
->assertDontSee('href="file://example.com/release-notes"', false);
});

it('allows admin access to quiz templates management and blocks teachers', function () {
$admin = User::factory()->create([
'role' => 'admin',
Expand Down