Skip to content
Closed
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
33 changes: 31 additions & 2 deletions backend/src/ApiControllers/CommentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function create(array $data): array
'post' => $data['post'],
'author' => $data['author'],
'email' => $data['email'] ?? '',
'website' => $data['website'] ?? '',
'website' => $this->sanitizeWebsite($data['website'] ?? ''),
'text' => $data['text'],
'html' => $html,
'ip' => $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'] ?? '',
Expand Down Expand Up @@ -125,8 +125,16 @@ public function edit(array $data): array

// make sure $data only contains fields that can be updated
$data = array_intersect_key($data, array_flip(['author', 'email', 'website', 'text']));
if (isset($data['website'])) {
$data['website'] = $this->sanitizeWebsite($data['website']);
}
if (isset($data['text'])) {
$data['html'] = $data['text']; // Simple text for now, could add Markdown processing
// Render through the same safe-mode Markdown pipeline as create(); do not
// copy the raw text into the html column (it is rendered via innerHTML).
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
$parsedown->setBreaksEnabled(true);
$data['html'] = $parsedown->text($data['text']);
}

// merge with existing record
Expand All @@ -135,6 +143,27 @@ public function edit(array $data): array
return $this->app->commentUtils()->process($new);
}

/**
* Only allow http(s) website URLs to be stored; the frontend renders the
* value directly into an anchor href, so a javascript:/data: scheme would be
* stored XSS. Empty and schemeless (relative) values are left as-is.
*/
private function sanitizeWebsite(string $url): string
{
$url = trim($url);
if ($url === '') {
return '';
}
// Collapse control chars/whitespace the browser strips when resolving the
// scheme, so "java script:..." cannot smuggle a dangerous scheme past this.
$probe = preg_replace('/[\x00-\x20]+/', '', $url);
$scheme = parse_url((string) $probe, PHP_URL_SCHEME);
if ($scheme !== null && !in_array(strtolower($scheme), ['http', 'https'], true)) {
return '';
}
return $url;
}

/**
* Update a comment's status
*
Expand Down