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

[3.x] Fix undefined array key 1 for user agent without version in "detectEngine" method of web client #132

Draft
wants to merge 5 commits into
base: 3.x-dev
Choose a base branch
from

Conversation

richard67
Copy link

@richard67 richard67 commented Nov 17, 2024

Pull Request for Issue #112

Summary of Changes

This pull request (PR) adds a check if a version part was found in the $userAgent parameter at 2 places in the "detectEngine" method of web client in order not to run into a PHP error "Undefined array key 1" when a known user agent is used without a version part.

See the linked issue and another CMS issue joomla/joomla-cms#44469 .

In addition, it modifies the "detectBrowser" so that for the user agent "Chrome Privacy Preserving Prefetch Proxy" no version "Privacy" is detected.

The user agent "Chrome Privacy Preserving Prefetch Proxy" is used by the https://developer.chrome.com/blog/private-prefetch-proxy and does not provide a version.

Finally, this PR adds a unit test case for that user agent (and will possibly add more test cases for user agents without version).

To be done

Possibly further test cases for user agents without version.

Testing Instructions

Use a user agent equal to e.g. "Chrome Privacy Preserving Prefetch Proxy" or just "Chrome", or e.g. just "AppleWebKit" without any version part appended with "/".

Result without this PR: PHP warning Undefined array key 1 and then PHP Deprecated explode(): Passing null to parameter #2 ($string) of type string is deprecated.

Result with this PR: No such warning and deprecated message.

Review the unit test case added by this PR and check that it passes in Drone for this PR.

Documentation Changes Required

None as far as I know.

src/Web/WebClient.php Outdated Show resolved Hide resolved

if ($version[0] >= 28) {
if ($version === false || $version[0] >= 28) {
Copy link
Contributor

@Quy Quy Nov 17, 2024

Choose a reason for hiding this comment

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

Maybe it should be the same as AppleWebKit? I assume self::WEBKIT to be the fallback value.
$version !== false &&

Copy link
Author

Choose a reason for hiding this comment

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

I would assume an actual version if no version is given. "Chrome Privacy Preserving Prefetch Proxy" is a real user agent used for the https://developer.chrome.com/blog/private-prefetch-proxy feature of Google Chrome.

Copy link
Author

@richard67 richard67 Nov 17, 2024

Choose a reason for hiding this comment

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

I've added a unit test, and it fails with a reason. The detectBrowser method would need to be adjusted to not detect a version="Privacy " when the user agent is "Chrome Privacy Preserving Prefetch Proxy"

Copy link
Author

Choose a reason for hiding this comment

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

Changed to draft status until I have checked further.

Copy link
Author

Choose a reason for hiding this comment

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

I thin it is OK to use if ($version === false || $version[0] >= 28) { here as the https://developer.chrome.com/blog/private-prefetch-proxy has been added to Chrome 103 for Android, and for other cases without version we can also assume Chrome version >= 28.

Choose a reason for hiding this comment

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

Looks like this should work. (I'm currently not able to test this, sorry.)

Add unit test data for user agent "Chrome Privacy Preserving Prefetch Proxy" for web client.
@richard67 richard67 marked this pull request as draft November 17, 2024 19:57
Don't detect version='Privacy' in detectBrowser method of web client.

if ($version[0] === 537.36) {
if ($version !== false && $version[0] === 537.36) {
// AppleWebKit/537.36 is Blink engine specific, exception is Blink emulated IEMobile, Trident or Edge
$this->engine = self::BLINK;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not related to your PR, but this doesn't seem right. The assignment on line 412 is replaced on line 417.

Copy link
Author

Choose a reason for hiding this comment

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

Yes ... weird. Would have to check with Git Blame if someone has removed a "return" statement or how else that code was changed so it is that weird.

Copy link
Author

Choose a reason for hiding this comment

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

It was added with this PR in 2016: #62 . And that created that nonsense.

Copy link
Contributor

@Quy Quy Nov 18, 2024

Choose a reason for hiding this comment

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

Move line 417 above the if statement (line 406) should fix the issue.

Choose a reason for hiding this comment

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

or put it into an else clause:

} elseif (\stripos($userAgent, 'AppleWebKit') !== false || \stripos($userAgent, 'blackberry') !== false) {
  if (\stripos($userAgent, 'AppleWebKit') !== false) {
     ...
  }
  else
  {
    // Evidently blackberry uses WebKit and doesn't necessarily report it.  Bad RIM.
    $this->engine = self::WEBKIT;
  }

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