I was looking for some examples of automatic locale detection as I needed to do the same in my PHP app. Most of the answers I found were similar, only differing in the parsing methods. An example 'pseudo-code':
$supportedLocales = ['en, 'bg', 'pl'];
$locale = 'en'; // default fallback locale
$userLangs = preg_split('/,|;/', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
foreach($userLangs as $lang)
{
if(in_array($lang, $supportedLocales))
{
$locale = $lang;
break;
}
}
But, after a while of experimenting with the Accept-Language header in my browser to see if everything works correctly, I noticed that I could actually make the header very long, causing the server to do a lot of looping because the code above loops through entire $supportedLocales array for every key in $userLangs array. I could actually easily send an forged Accept-Language header with over 25000 fake languages that were not supported by the app, therefore generating over 100000 iterations in total. Not including the iterations used to compare the string values.
Even here on Stack websites, I could send over few thousand languages before getting a '400 Bad Request' error. If this can be considered an attack, how can one protect his website from such forged headers? I know I could just limit the amount of languages, but still, what amount can be considered normal?