Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale matcher can fail when client provided locale identifier has incorrect casing #1087

Closed
Silic0nS0ldier opened this issue Apr 23, 2020 · 3 comments · Fixed by #1088
Closed
Labels
confirmed bug Something isn't working internationalization Related to the localization feature
Milestone

Comments

@Silic0nS0ldier
Copy link
Member

if (in_array(strtolower($identifier), array_map('strtolower', $availableLocales))) {
// Determine preference level (q=0.x), and add to $foundLocales
// If no preference level, set as 1
if (array_key_exists(1, $parts)) {
$preference = str_replace('q=', '', $parts[1]);
$preference = (float) $preference; // Sanitize with int cast (bad values go to 0)
} else {
$preference = 1;
}
// Add to list, and format for UF's i18n.
$foundLocales[$identifier] = $preference;
}
}

We need to update how the identifier to handled to ensure issues don't occur on case sensitive file systems. Ideally the value used would come from the locale matcher array to avoid excessive value processing.

@Silic0nS0ldier Silic0nS0ldier added confirmed bug Something isn't working internationalization Related to the localization feature labels Apr 23, 2020
@Silic0nS0ldier
Copy link
Member Author

Entirely untested prototype fix

                    // Ensure locale available
                    $localeIndex = array_search(strtolower($identifier), array_map('strtolower', $availableLocales));
                    if ($localeIndex !== -1) {
                        $matchedLocale = $availableLocales[$localeIndex];
                        // Determine preference level (q=0.x), and add to $foundLocales
                        // If no preference level, set as 1
                        if (array_key_exists(1, $parts)) {
                            $preference = str_replace('q=', '', $parts[1]);
                            $preference = (float) $preference; // Sanitize with int cast (bad values go to 0)
                        } else {
                            $preference = 1;
                        }

                        // Add to list, and format for UF's i18n.
                        $foundLocales[$matchedLocale] = $preference;
                    }

@lcharette
Copy link
Member

lcharette commented Apr 23, 2020

Isn't that handled by the strtolower ? Can you give an example of a input value where it wouldn't work ?

@lcharette lcharette added this to the 4.4.x milestone Apr 23, 2020
@lcharette
Copy link
Member

lcharette commented Apr 23, 2020

Ok I see. A test with en-us instead of en-US would be required here :

$request->shouldReceive('getHeaderLine')->with('Accept-Language')->once()->andReturn('en-US, en;q=0.9, fr;q=0.8, de;q=0.7, *;q=0.5');

lcharette added a commit to lcharette/UserFrosting that referenced this issue Apr 23, 2020
@lcharette lcharette linked a pull request Apr 23, 2020 that will close this issue
@lcharette lcharette modified the milestones: 4.4.x, 4.4.2 Apr 23, 2020
lcharette added a commit that referenced this issue Apr 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed bug Something isn't working internationalization Related to the localization feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants