Skip to content
Merged
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
16 changes: 10 additions & 6 deletions lib/Providers/HeaderLanguageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ class HeaderLanguageProvider
*/
public function getLanguage(): ?string
{
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) || !is_string($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return null;
}

// The header is attacker-controlled: validate the primary entry
// against the language-tag shape instead of trusting raw bytes.
$primary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];

if (!empty($languages[0])) {
return strtolower(substr($languages[0], 0, 2));
}
if (!preg_match('/^\s*([a-zA-Z]{2})(?:[-_][a-zA-Z0-9]{2,8})*\s*(?:;.*)?$/', $primary, $matches)) {
return null;
}

return null;
return strtolower($matches[1]);
}
}
Loading