Skip to content

feat(28022): Implement Moodle TUS upload with SSO authentication#67

Merged
Yurujai merged 12 commits into
5.1.xfrom
feature_28022_moodle_tus_sso_integration
Mar 10, 2026
Merged

feat(28022): Implement Moodle TUS upload with SSO authentication#67
Yurujai merged 12 commits into
5.1.xfrom
feature_28022_moodle_tus_sso_integration

Conversation

@albacodina

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements TUS (Tus Upload Protocol) support for Moodle integration with SSO authentication. It enables chunked file uploads from Moodle instances while validating users through the existing SSO service.

Changes:

  • Added SSO parameter extraction and session storage in the upload controller to support TUS authentication flow
  • Introduced a new MoodleServerController implementing TUS protocol endpoints with SSO validation, file extension checks, and upload dispatch functionality

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
Controller/UploadController.php Extracts SSO credentials from request and stores them in session for TUS upload authentication
Controller/MoodleServerController.php Implements TUS server endpoints with SSO authentication, file validation, and upload management for Moodle integration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Controller/MoodleServerController.php Outdated

$this->validateFileExtension($request);

$cleanSeries = preg_replace('/[^a-zA-Z0-9_\-]/', '', (string) $series);

Copilot AI Feb 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The series sanitization logic is duplicated on line 146. Consider extracting this into a private method to improve maintainability and ensure consistency.

Suggested change
$cleanSeries = preg_replace('/[^a-zA-Z0-9_\-]/', '', (string) $series);
$sanitizeSeries = static function (string $series): string {
$cleanSeries = preg_replace('/[^a-zA-Z0-9_\-]/', '', $series);
return $cleanSeries ?? '';
};
$cleanSeries = $sanitizeSeries((string) $series);

Copilot uses AI. Check for mistakes.
Comment thread Controller/MoodleServerController.php Outdated

$metadata = $request->headers->get('Upload-Metadata');
if ($metadata && preg_match('/filename (?P<name>[^\s,]+)/', $metadata, $matches)) {
$filename = base64_decode($matches['name']);

Copilot AI Feb 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base64-decoded filename is used directly in file path construction without sanitization. Malicious filenames could potentially contain directory traversal sequences (e.g., '../'). Sanitize the decoded filename before using it in file operations.

Suggested change
$filename = base64_decode($matches['name']);
$decodedName = base64_decode($matches['name'], true);
if (false === $decodedName) {
return new Response('Invalid filename encoding', 400);
}
// Sanitize filename to prevent directory traversal and unsafe characters
$filename = basename($decodedName);
$filename = str_replace(['/', '\\'], '_', $filename);
$filename = preg_replace('/[^a-zA-Z0-9._\- ]/', '_', $filename);
$filename = trim((string) $filename);
if ('' === $filename) {
return new Response('Invalid filename', 400);
}

Copilot uses AI. Check for mistakes.
Comment thread Controller/MoodleServerController.php Outdated
$declaredMimeType = '';

if (preg_match('/filename (?P<name>[^\s,]+)/', $metadata, $matches)) {
$filename = base64_decode($matches['name']);

Copilot AI Feb 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base64-decoded filename should be sanitized before use in logging and error messages to prevent log injection attacks or path traversal issues. Apply path sanitization similar to the series sanitization.

Copilot uses AI. Check for mistakes.
$filename = base64_decode($matches['name']);
}
if (preg_match('/filetype (?P<type>[^\s,]+)/', $metadata, $matches)) {
$declaredMimeType = base64_decode($matches['type']);

Copilot AI Feb 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decoded MIME type is not validated before being passed to isAllowed(). Ensure the MIME type string is properly validated to prevent injection of malicious content into the validation logic.

Copilot uses AI. Check for mistakes.
Comment thread Controller/MoodleServerController.php Outdated
Comment on lines +204 to +206
$this->uploadDispatcherService->dispatchUploadFromInbox(
$user,
$request->get('fileName'),

Copilot AI Feb 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fileName parameter is retrieved directly from the request without validation and passed to dispatchUploadFromInbox. This could allow directory traversal or arbitrary file access. Validate and sanitize the fileName parameter.

Suggested change
$this->uploadDispatcherService->dispatchUploadFromInbox(
$user,
$request->get('fileName'),
$fileName = $request->get('fileName');
if (!is_string($fileName) || $fileName === '') {
return new JsonResponse(['success' => false, 'error' => 'Invalid file name'], 400);
}
// Prevent directory traversal and enforce simple file names
if (strpos($fileName, '..') !== false || strpbrk($fileName, "/\\") !== false) {
return new JsonResponse(['success' => false, 'error' => 'Invalid file name'], 400);
}
$fileName = basename($fileName);
$this->uploadDispatcherService->dispatchUploadFromInbox(
$user,
$fileName,

Copilot uses AI. Check for mistakes.
@albacodina albacodina changed the base branch from 5.0.x to 5.1.x March 10, 2026 09:12
@Yurujai Yurujai merged commit 68b501c into 5.1.x Mar 10, 2026
2 checks passed
@Yurujai Yurujai deleted the feature_28022_moodle_tus_sso_integration branch March 10, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants