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

Some website return some invalid UTF16 ?? #666

Open
momala454 opened this issue Nov 20, 2024 · 12 comments
Open

Some website return some invalid UTF16 ?? #666

momala454 opened this issue Nov 20, 2024 · 12 comments

Comments

@momala454
Copy link
Contributor

Hi
I'm receiving the following error
Uncaught exception 'HeadlessChromium\Exception\CommunicationException\CannotReadResponse' with message Response from chrome remote interface is not a valid json response. JSON error: 10

JSON error 10 means

JSON_ERROR_UTF16 | Malformed UTF-16 characters, possibly incorrectly encoded

The exception is thrown on
vendor/chrome-php/chrome/src/Communication/Connection.php
Method dispatchMessage

The data is from a message Fetch.requestPaused, when it's trying to send a tracking data from https://www.smithsfoodanddrug.com.
The post data contains unreadable data like this, which prevent the library from doing json_decode over it

"postData":"\t\u0000\u0000\u0000\u0011i\u000e\u0006\n&J+\u0.....
@divinity76
Copy link
Contributor

Can you provide a small reproduce script? What OS are you on? what PHP version are you running?

I failed to reproduce the issue with:

<?php
declare(strict_types=1);
require_once('vendor/autoload.php');
$chromiumPath = '/usr/bin/chromium-browser';
$factory = new \HeadlessChromium\BrowserFactory($chromiumPath);
$browser = $factory->createBrowser();
$page = $browser->createPage();
$page->navigate('https://www.smithsfoodanddrug.com')->waitForNavigation();
var_dump($page->getHtml());

it produced

$ php repro.php 
string(244597) "<html dir="ltr" lang="en"><head>
  <meta charset="utf-8">
  <meta name="color-scheme" content="light dark">
  <meta name="theme-color" content="#fff">
(...)

@momala454
Copy link
Contributor Author

linux, php 8.1,
I think it's only on their login page https://www.smithsfoodanddrug.com/signin?redirectUrl=/, and it happens randomly I think

@divinity76
Copy link
Contributor

divinity76 commented Nov 28, 2024

@momala454 still unable to reproduce, i tried

for($i=0;$i<100;++$i) {
    $page->navigate('https://www.smithsfoodanddrug.com/signin?redirectUrl=/')->waitForNavigation();
    var_dump($page->getHtml());
}

and I think it ran like 30 times before I got IP banned,

but even with~30 iterations, I did not get the invalid UTF16 error you speak of.

Can you please write a script to reproduce the issue?

@momala454
Copy link
Contributor Author

the issue occurs with Fetch.requestPaused
so you need to enable

$page
			->getSession()
			->sendMessageSync(new \HeadlessChromium\Communication\Message('Fetch.enable', [
				"handleAuthRequests" => true, 
				"patterns" => [
					['urlPattern' => '*']
				]
			]));

i will try to make a script

@momala454
Copy link
Contributor Author

i am able to reproduce using this script

require_once('vendor/autoload.php');
$chromiumPath = 'google-chrome';
$factory = new \HeadlessChromium\BrowserFactory($chromiumPath);
$browser = $factory->createBrowser([
  'customFlags' => [
    '--excludeSwitches=["enable-automation"]', 
    '--lang=en', 
    '--no-sandbox', 
    '--disable-setuid-sandbox', 
    '--enable-features=NetworkService', 
    '--disable-namespace-sandbox', 
    '--enable-logging', 
    '--disable-features=site-per-process,ChromeWhatsNewUI,Translate,AcceptCHFrame,MediaRouter,OptimizationHints,ProcessPerSiteUpToMainFrameThreshold',
    '--disable-blink-features=AutomationControlled',
    '--enable-blink-features=WebBluetooth,WebBluetoothRemoteCharacteristicNewWriteValue',
    '--disable-infobars',
    '--start-maximized',
    '--no-default-browser-check',
    '--remote-allow-origins=*',
    '--password-store=basic',
    '--disable-save-password-bubble',
  ],
  'headless' => false,
  'envVariables'				=>	['DISPLAY' => ':99'],
  'sendSyncDefaultTimeout'	=>	40000,
  'debugLogger'     			=> 'php://stdout',
]);
$page = $browser->createPage();

$page
			->getSession()
			->on('method:Fetch.requestPaused', function ($params) use ($page) {
				echo date('r'). ' received Fetch.requestPaused '/*.print_r($params,true)*/."\n";
					$page
						->getSession()
						->sendMessageSync(new \HeadlessChromium\Communication\Message('Fetch.continueRequest', [
						'requestId' => $params["requestId"]
						]));
		});

$page
->getSession()
->sendMessageSync(new \HeadlessChromium\Communication\Message('Fetch.enable', [
  "handleAuthRequests" => true, 
  "patterns" => [
    ['urlPattern' => '*']
  ]
]));
try {
  $navigation = $page->navigate('https://www.smithsfoodanddrug.com/');
  $navigation->waitForNavigation(\HeadlessChromium\Page::LOAD, 50000);
  $navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);

  $navigation = $page->navigate('https://www.smithsfoodanddrug.com/signin?redirectUrl=/');
  $navigation->waitForNavigation(\HeadlessChromium\Page::LOAD, 50000);
  $navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);
} catch (\Throwable $e) {
  echo $e->__toString();
  sleep(4);
  throw $e;
}
sleep(2);
echo 'end';
var_dump($page->getHtml());

@divinity76
Copy link
Contributor

divinity76 commented Nov 28, 2024

for me it crashed after 2 minutes in

  $navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);

with

HeadlessChromium\Exception\OperationTimedOut: Operation timed out after 50s. in /home/hans/projects/misc/vendor/chrome-php/chrome/src/Exception/OperationTimedOut.php:18

which i suspect is valid, NETWORK_IDLE probably fired so fast after LOAD that the php NETWORK_IDLE listener had not been initialized yet, thus by the time it is listening, it is too late..
there's constantly some javascript doing requests in the background, so the network never gets idle for me, it seems.

if you remove

$navigation->waitForNavigation(\HeadlessChromium\Page::LOAD, 50000);

and keep

  $navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);

does it still happen?

I suspect some kind of localization issue where it sends people from your region different responses from people in my region (Norway), and your region response triggers the issue, but not my region's response 🤔

does this look like the page you are getting?
image

@divinity76
Copy link
Contributor

fwiw when I remove the

$navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);

(because some javascript is constantly keeping my network un-idle)
the script you provided exits normally here, ending with

=!0),c&&(s?(t.call(n,f),t=null):(c=t,t=func</script></head></html>"
[2024-11-28T14:54:28.400980+00:00] chrome.DEBUG: process: killing chrome [] []
[2024-11-28T14:54:28.401014+00:00] chrome.DEBUG: process: trying to close chrome gracefully [] []
[2024-11-28T14:54:28.401036+00:00] chrome.DEBUG: socket(1): → sending data:{"id":253,"method":"Browser.close","params":{}} [] []
[2024-11-28T14:54:28.401145+00:00] chrome.DEBUG: session(345BEE05E636FE5C76A6CF75471B724E): ⇶ dispatching method:Network.dataReceived [] []
[2024-11-28T14:54:28.401334+00:00] chrome.DEBUG: socket(1): ← receiving data:{"id":253,"result":{}} [] []
[2024-11-28T14:54:28.401368+00:00] chrome.DEBUG: socket(1): disconnecting [] []
[2024-11-28T14:54:28.401503+00:00] chrome.DEBUG: socket(1): ✓ disconnected [] []
[2024-11-28T14:54:28.401529+00:00] chrome.DEBUG: socket(1): disconnecting [] []
[2024-11-28T14:54:28.401544+00:00] chrome.DEBUG: socket(1): ✗ could not disconnect [] []
[2024-11-28T14:54:28.401561+00:00] chrome.DEBUG: process: waiting for process to close [] []
[2024-11-28T14:54:28.508148+00:00] chrome.DEBUG: process: cleaning temporary resources:/tmp/chromium-php-fGyWC0 [] []

real	0m8.604s
user	0m1.806s
sys	0m0.682s

@momala454
Copy link
Contributor Author

without this $navigation->waitForNavigation(\HeadlessChromium\Page::LOAD, 50000); but with wait IDLE, it still occurs, but I need to retry a few times

@divinity76
Copy link
Contributor

divinity76 commented Nov 28, 2024

nice, it took 5 minutes and multiple retries, but I managed to reproduce it!
attached the full log (5MB!), but here is the most interesting part:

[2024-11-28T15:19:50.427148+00:00] chrome.DEBUG: socket(1): ← receiving data:{"method":"Fetch.requestPaused","params":{"requestId":"interception-job-188.0","request":{"url":"https://www.smithsfoodanddrug.com/public/3eba5ca6f9e0a36475065ee8d0c2be4f278e07179439/collect?t=1732807190424&st=1630&s=drsqHVp54mSfgMyT&ss=3&c=5ee8d0c2be4f278e07179439&r=ic3u6RGuyDdUw1VZ&d=1&u=https%3A%2F%2Fwww.smithsfoodanddrug.com%2Fcdn%2FSyncinjector22Oct2024.html&v=1701637501&p=1&bv=9&rh=33aece61cfa15f545970aa99c4eecf4b&pi=433&pl=456&pwl=456&ple=456&psd=68&ppu=0&psl=25&pfu=421&phe=4&pue=-1&pbc=3&pnu=-1&pnc=-1&pnr=-1&fsp=0&sp=0&pp=0&ah=0&sm=1&tr=1","method":"POST","headers":{"Accept":"*/*","Content-Type":"text/plain","Cookie":"origin=lvdc; abTest=3f_8aae36_B|c6_10b77e_B|d1_a044c0_A; dtCookie=v_4_srv_42_sn_3F58D84D814B0DDFED5788203A9D2428_perc_100000_ol_0_mul_1_app-3A81222ad3b2deb1ef_1_rcs-3Acss_0; sid=29638732-c872-b1a2-2bcb-e3376b20d1a1; pid=2bcbe337-6b20-2963-d1a1-8732c872b1a2; AKA_A2=A; bm_ss=ab8e18ef4e; bm_so=8D7C2613408357484372F4A2492F3D63B0877029CDF021FDA9F657A0E093E1A2~YAAQKzIQYO+WYTCTAQAAlVhbcwFA/5FEgIGaK9dG7OB01lzRu5yTeb7abh5MtfNiXRx2zwkflUyv0CS/zIwtSD+Z9pfkeJoIdV3uzSJZlKnKpgGrh+MMkb2ydwY6vhsguSI4FtIsmbHeJ6NUfovshaKeDUJbD0CWyo14YXgvXAhDadVIPm0HFklNsu8vTIDDQ6Ol03MEnrsjZbi451u7qZYsv5E+MLFReooxCTtNS3v3hD1Dx/QRjt6OHD3NOB4YeOOD1xW3QbqeGjFBuTT07cU/WR1qogRdWM21IuyIfNxD9M0J+n1+CdFmLYy1k5YW+Trf5RwMzNzEL2QdoK7uXshzXrocyAto21A7GuZIyLGLirx6DtfrM1o2WSmfjzOJZtXYqjtwvQZNOa1sUYSLOUK9sTNCPf/f/xzNx5f7/ovlNHX2ck0u+WFjZ505R7Eggu9oGjSL+WH6doQwFrRv0R/1GhnIeNxHY+8=; rxVisitor=17328071867948CBUJ01RGS9LS94RHVUAO31LKLRHQRG9; bm_lso=8D7C2613408357484372F4A2492F3D63B0877029CDF021FDA9F657A0E093E1A2~YAAQKzIQYO+WYTCTAQAAlVhbcwFA/5FEgIGaK9dG7OB01lzRu5yTeb7abh5MtfNiXRx2zwkflUyv0CS/zIwtSD+Z9pfkeJoIdV3uzSJZlKnKpgGrh+MMkb2ydwY6vhsguSI4FtIsmbHeJ6NUfovshaKeDUJbD0CWyo14YXgvXAhDadVIPm0HFklNsu8vTIDDQ6Ol03MEnrsjZbi451u7qZYsv5E+MLFReooxCTtNS3v3hD1Dx/QRjt6OHD3NOB4YeOOD1xW3QbqeGjFBuTT07cU/WR1qogRdWM21IuyIfNxD9M0J+n1+CdFmLYy1k5YW+Trf5RwMzNzEL2QdoK7uXshzXrocyAto21A7GuZIyLGLirx6DtfrM1o2WSmfjzOJZtXYqjtwvQZNOa1sUYSLOUK9sTNCPf/f/xzNx5f7/ovlNHX2ck0u+WFjZ505R7Eggu9oGjSL+WH6doQwFrRv0R/1GhnIeNxHY+8=^1732807188410; DD_guid=null; DD_modStore=70600426; firstPageViewTriggered=false; bm_mi=7990AB6A428EA82B2178B7709EAE1DF5~YAAQKzIQYDeXYTCTAQAACWBbcxl9pMpbqI1a0IF36WXOx8a0ybGGMUB6WScHyxXmFtBstUIgRodaPb8r7Zzq52odwQ3bcNdQNAVtpYgyctT7l3kXsrdqyAQduymHVZjlQ6f5Sjawyo07js4ZcAgsocsbenSnbWEvBsQ1mltGd/dt/yneOG5RlADqku61ZmJ5lHLoxU1Nx/ACDC144bAYoL8f5KBWKF2MOUcWJ6XWN1fd3WmsrOKB6etuRGoPGLsHa+o2LdKBYPc11hcIveMY6/dXn+TwD9+FL5fH/n8TfffzsOAC/0gHCEaf8ftr2h/a/EStPUu+lfyWwBNP7NZNENml54XzZWxgLokM4vfLbOVdubNzqMo+~1; RT=\"z=1&dm=smithsfoodanddrug.com&si=zr0vmqazfi&ss=m41gnqo8&sl=0&tt=0\"; _fbp=fb.1.1732807188889.1419794732; AMCVS_371C27E253DB0F910A490D4E%40AdobeOrg=1; AMCV_371C27E253DB0F910A490D4E%40AdobeOrg=179643557%7CMCIDTS%7C20056%7CMCMID%7C15867644538846775853080427846149490492%7CMCAID%7CNONE%7CMCOPTOUT-1732814388s%7CNONE%7CvVersion%7C5.5.0; akaalb_KT_Digital_BannerSites=~op=KT_Digital_BannerSites_KCVG:cdc|KT_Digital_BannerSites_Legacy:kcvg|~rv=89~m=cdc:0|kcvg:0|~os=49d9e32c4b6129ccff2e66f9d0390271~id=8cf8a724acb4f51e0cda2089fc69017f; _ga_8WFSX616HR=GS1.1.1732807188.1.0.1732807189.0.0.0; _ga=GA1.1.1468082208.1732807188; ak_bmsc=C8CEDC8FE7BD6746DE79003C17C2529C~000000000000000000000000000000~YAAQKzIQYHeXYTCTAQAAe2NbcxmzlII2CHh+lHBEcm+iLWFEQmBVmR6lncc++uxOvP4oDEnpOvIt9veRh+ivlF+iYOOE10udXgX0lQAS8YJnQv+m8BSwMqGnhh++YvnF1zqJpIDh3/45lZQ6PmYTDS2Ob9JsSk7x+WZrZQI3jjBkuX7VMFLkwXlIfHVCVw3S/+/falqb2/h+NkobiCFCHOjbH0xNyod7476q/LmL1eeBHDZxunh/icWGaskQz89eAsUIa9jj6Tu0nXytNmzP8ylzLs0nvileZJnnQ+sk/qsKwc2EcsHWn1UKmRLxgpAjvm/753tL9dUEOwUU5eXzjXaG07l2OmaVURBmlpcQNEw5IujqE5didHEQbA5GNcBRVxWqzNtuMDqzTY9Z6R5DWAsG+PFd/SqS3VZ2HULmvGVRrsuvR3YuGGY989JECMoWketGXVlCjqvS4aO5scuSt/3X4R/aYt98Kvl8tSzUv5nu7WJxrqXqt/RUWJiL1SVzX2NUhVXz81bF+sPwtw==; rxvt=1732808989917|1732807186795; dtSa=false%7Cxhr%7C29%7Cx%7Cx%7C1732807189691%7C7186793_903%7Chttps%3A%2F%2Fwww.smithsfoodanddrug.com%2F%7C%7C%7C%7C; _abck=709359924A89ACA37187ABE7FA62FB0E~-1~YAAQKzIQYI2XYTCTAQAA4GRbcwyOyug17e+2XFQpkzatsh3D9FfYMvFY2sAGFNZC8BI3BDLupdvG6rNxBGLz3EK1cljVzIXIpD3Zmk/a+qelw8s4nvFG6kr1l42jrkg6eHQyFi+dwAV2JdrV8F620wclLgUPvGn2JihDV5/CK7uWvo5Vr4eUxeib/Hgvpfxtho46TpzPp+GPVZml3wJB1seyjd2f0b13P36SnEOR9evw7BYxFrc9nSwMuAgCiTj/rpH2fk1kz4u7vU5px+KUlwHatA09NPKDTarERcgpSh1UqQ+bIIH3r9xnR79HZbYLK5u9aKUm7bqI1WKf8WLHsRY2m2waYGEtWIl3fWr7TxCeo1cefU47xvl6f+qVhaJdY8Dp9G4s3jCtS5UeixSHzYrzoarrz1EzvR76YM3UHkFfriohJXiKlsqHaG6gG0Ifg/O0NW2HJnDeR7ff/cfAwhPyLKDDPleoVRkSyutO3O5nJnbReCgPYS5N1hi5RxM0N3yxwNVOsxrU7IWe9Gt+7URhJYXCEHlg3DeHz6YsYCg4iqul/ygqYgvZ8JtfdT6y4LpoOsdPXRBBXe2M30pw/NrOIg==~-1~-1~1732810788; OptanonConsent=isGpcEnabled=0&datestamp=Thu+Nov+28+2024+16%3A19%3A50+GMT%2B0100+(Central+European+Standard+Time)&version=202405.2.0&browserGpcFlag=0&isIABGlobal=false&hosts=&consentId=0aa52865-1063-490c-a68c-f131d333d020&interactionCount=1&isAnonUser=1&landingPath=https%3A%2F%2Fwww.smithsfoodanddrug.com%2F&groups=BG1216%3A1%2CC0001%3A1%2CC0003%3A1%2CC0002%3A1%2CC0004%3A1%2CC0009%3A1%2CC0008%3A1; bm_s=YAAQKzIQYJyXYTCTAQAAlWVbcwKSIL8eYhdaAl+I347YGLdXuwkdIqVMFUW+8+r4d6qjHnXm54JHVSTnNbnEDFhEtUakr3hHdlSUdhyvrD4bPvUErURd53f9vSBEsL6/iZkuvNsX4f705TN+Gd+w1rfUVOg3N5T9/9dagGmLLSe66aLQ4N2+ISz548jExExNNUukwQwz4ugmHx6ivJUTKCuhTXDzW546u11XL69FxOgvzbpctasLBrxE23CCBEzRdZ+gpaJqmogDQByC29pM3LO2N3OsuMpDIJ1tfTi3V9ncirlEW4zXff/mZsj0xeh5WVgB0jcjSkIB/2mk58BDYFLyNtt06YFkmldOjg==; x-active-modality={\"type\":\"PICKUP\",\"locationId\":\"70600426\",\"source\":\"FALLBACK_ACTIVE_MODALITY_COOKIE\",\"createdDate\":1732807190005}; bm_sv=9E7F6687443ED942A30E056B90BF603F~YAAQKzIQYLKXYTCTAQAAwWZbcxlo22j+tOrg6f0bgATVwB/9rhFBCVSvtp2cCeQkaP0Mg7eVmAcatx3Quv29I723r5TKzbkTMctpvAO85D70Lvt1DjtRVwePh9z1r0q55oHU0HohOTJ0NKgmcYiDJOM+cnbm6swcRzxK3AlpSeFIE8EyLOUbHqhTp06jpy/v7zAIaUJxcEB3RIk/faPIwB3vLwoBHSSHWeAVEB/tmP3FGjKiRZG3hvqoQ6pShEFx4pUBhG+Kx8sqd4g=~1; bm_sz=D5D8734F851C9C9235F6BBE452A1273E~YAAQKzIQYLOXYTCTAQAAwWZbcxkh51EJoTJXndKzXT03VWf85c8UwGcgkucBJvyR0wIoBoXrC2e50JeHtLi9wkD2FgUb9EPDO3KpbNNtBq6RUhnuP8R6X5JS8h6RhahSaJTokSGSRqg04mVyl6zXsvL+iSuuNn+okojj/aTz3AkjWqGtYfLx4J7iOz+25B8Au1lx6kX5xtSZd0SJvca2DTB5Y9Wf9fPF8dWtCBDAUxjK09k3JPZx9hGiH1yu+bfIU5XbSaY7uxlqcABZFnJk0PyUHD1du8y2Sor0prNyujMZ2usi1+BFvrXguZAq5PT2NLVN/XxkuDE2HVr7qj2uyMAfjnN/pWA4NrgvZhSkiYFp+DqDt6+5j/fdQDe8vDdj/QE8Ppmd5pSBLrbs1h0lyAkIoUh1J3TqOacYAF7e+vFfS8C5qYBAvLtH7TiS5Q==~3490114~4274501; dtPC=42$7186793_903h-vHKRRPKWAVTMDMUKJJSAJMIAOGICRPTMR-0e0","Origin":"https://www.smithsfoodanddrug.com","Referer":"https://www.smithsfoodanddrug.com/cdn/Syncinjector22Oct2024.html","User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36","device-memory":"8","sec-ch-ua":"\"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"","sec-ch-ua-mobile":"?0","sec-ch-ua-platform":"\"Linux\""},"postData":"\t\u0000\u0000\u0000\u0016<\u000b\u0000\u0000=m?3�7tlq4%\u0621e~~ELCc{8\u001c{T'Vcj!\u000b!ry?c&\u001a\u0016^<j)?^l\b$\u0019bC-B4m\tp6::\u000eGg7'd>:Kf7^\u001e^\u0015\u031b\u0019}Ng?g!<X\u001e\u001coY\f{00*\u0010@J!\u029a+Z6%{9Ut(\b\u001e7iWA&\u06a2*)\u001d�x@nB\u0000\u000e\u0011_Y\ub001L@W\u000fe\u000f\u01c02*t\u0000\u001bS#|\ubcfdU\u000f')\u00037F 7MpJyg=\u0013Bt&\u034as\u0578ZM\u01a5\u0007\t5^lz^V#1\u03ba\u0006\rJ>H;\u0011lus\u0007e\u0006Y.ufY\u05b1\u001bY\u001e\u0019hg:\u001a}NVv\u0003\ude73ol]Tn=w'%3]\\n\u0003\fxkus  \u5b82*\f'h[7Sg\u0006\u0014AY\u0003\u00169\u07f6ic\b^\u001f@?xi^\u0000\u91ac\u0018F\u001f\u0005(\f8sD1G\u0011a\t_\u0016\t\"W(^\u001f/]\"a\u001aQHbM[\u0010\u0000\u0000\r\u0018@$\f2\u0018Xy\u0010S yf(*\r-P(\\#\u0003T!2\u0017\u0000\u0000","hasPostData":true,"postDataEntries":[{"bytes":"CQAAABY8CwAA1FKtPW3DUJQ/M3+Y1PqRN9JUwQrbYHRsjPL4sMhxnDQliaPYoWW/fn60RUxDY5r4knv9OOeeexx7oVQn9yFcrVZj2VRqIQshctrmeT+UYyYayPIWXjy0rGqXnCnRE/KFKYKIP16opt5bbAgkGZdi1HRDLfm45QpmQjS8p20Jr84+3x6P5tPjcDY6Oo2vDkdnNydkPjpLZjfnO4LnFVCa5hcd7IspXsPq4nbxHl6dpBW86XFRzJuoiKYZ/PAd0Wz1lXxbfYLn15fw6BatTmfX7D/4ZyE8WB4cb1nnHw/gybS5DPOfQryE9o42EHswjDAqEI6eQEohyporWja0pSXvLaLUY7iUezlVdJ/gxEMoCN0o8HGQ+B7xdyzsN79pV0Em2qIqx0sp2skdf3j39gdJQG5CAJQOEbi3X1nrgIGsTIhAVw9l1UqzD8eAMip0igC9G1OzI3yivKCM67O9s4pVDyeVKYADwA03RiA35JEPhvxNcMjXivctrdNKj6aGzAd5Z/g9E5mwQnQmlc2KrHOqlZGDvcd21birWk3GpbKlB8n7CTWbXmzsRirZAXpeVqLFG9BII+oHVTHpQNrOutb//MQ2BtQGDUrVGI0+SDurEZRss9J21PXT0JR10nqtcwenZcrXBuEngFkugsFgzAqA7HVIwECpdWagmVnWsbi3G8xUk1kendjTGWhnoToafbFOVnYD7bmzsm9s7CTLXZVUbo49dyclmTNd8tLJ3pxcbqYDDHhrpnWfhXMglY4g5a6CKgyR93f/6dZCJ2hbvzez/lNnBrMUgkFZAxb2uzOcOd+2/mnHbWMI/8N17Ko1r9M/XsrnH0A/ePxpXgDphqyuGPR4RgNGwyLhiHqhHwUoDDiPc8RIxv2CRDFHEY70xU4gh2Hg4STFMQnM8+ohfY9fsRYJIlfLIyiJXrWWH8fBti9d75X7ImEa+VFIYoJNW78QwAAAAMxQx0DHEIoNGIxAJAwyGKPwTFB4plh5EFMg5hlCeWYoKg0tULmWKFwjA1SuITIXAAA="}],"initialPriority":"VeryLow","referrerPolicy":"strict-origin-when-cross-origin"},"frameId":"5372566976D77A0E07FC342B8BF0568D","resourceType":"Ping","networkId":"2997375.351"},"sessionId":"C2CE68ADFDF2F665AEA687D4CDE8BB65"} [] []
HeadlessChromium\Exception\CommunicationException\CannotReadResponse: Response from chrome remote interface is not a valid json response. JSON error: 10 in /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php:367
Stack trace:
#0 /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php(343): HeadlessChromium\Communication\Connection->dispatchMessage()
#1 /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php(326): HeadlessChromium\Communication\Connection->readLine()
#2 /home/hans/projects/misc/vendor/chrome-php/chrome/src/PageUtils/PageNavigation.php(193): HeadlessChromium\Communication\Connection->readData()
#3 /home/hans/projects/misc/vendor/chrome-php/chrome/src/Utils.php(60): HeadlessChromium\PageUtils\PageNavigation->navigationComplete()
#4 /home/hans/projects/misc/vendor/chrome-php/chrome/src/PageUtils/PageNavigation.php(132): HeadlessChromium\Utils::tryWithTimeout()
#5 /home/hans/projects/misc/repro.php(59): HeadlessChromium\PageUtils\PageNavigation->waitForNavigation()
#6 {main}PHP Fatal error:  Uncaught HeadlessChromium\Exception\CommunicationException\CannotReadResponse: Response from chrome remote interface is not a valid json response. JSON error: 10 in /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php:367
Stack trace:
#0 /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php(343): HeadlessChromium\Communication\Connection->dispatchMessage()
#1 /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php(326): HeadlessChromium\Communication\Connection->readLine()
#2 /home/hans/projects/misc/vendor/chrome-php/chrome/src/PageUtils/PageNavigation.php(193): HeadlessChromium\Communication\Connection->readData()
#3 /home/hans/projects/misc/vendor/chrome-php/chrome/src/Utils.php(60): HeadlessChromium\PageUtils\PageNavigation->navigationComplete()
#4 /home/hans/projects/misc/vendor/chrome-php/chrome/src/PageUtils/PageNavigation.php(132): HeadlessChromium\Utils::tryWithTimeout()
#5 /home/hans/projects/misc/repro.php(59): HeadlessChromium\PageUtils\PageNavigation->waitForNavigation()
#6 {main}
  thrown in /home/hans/projects/misc/vendor/chrome-php/chrome/src/Communication/Connection.php on line 367

Seems to me like there is a bug in the chromium dev protocol where it does not properly encode json! very interesting 🤔 multiple json validators agree that it's not valid json

Now the question is how to easily reproduce it.. it took 5 minutes and many retries for me to reproduce it just once.
log.log

@divinity76
Copy link
Contributor

divinity76 commented Nov 28, 2024

Found a way to easily reproduce it:

$factory = new \HeadlessChromium\BrowserFactory($chromiumPath);
$browser = $factory->createBrowser([
    'customFlags' => [
        '--excludeSwitches=["enable-automation"]',
        '--lang=en',
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--enable-features=NetworkService',
        '--disable-namespace-sandbox',
        '--enable-logging',
        '--disable-features=site-per-process,ChromeWhatsNewUI,Translate,AcceptCHFrame,MediaRouter,OptimizationHints,ProcessPerSiteUpToMainFrameThreshold',
        '--disable-blink-features=AutomationControlled',
        '--enable-blink-features=WebBluetooth,WebBluetoothRemoteCharacteristicNewWriteValue',
        '--disable-infobars',
        '--start-maximized',
        '--no-default-browser-check',
        '--remote-allow-origins=*',
        '--password-store=basic',
        '--disable-save-password-bubble',
    ],
    'headless' => false,
    //'envVariables'                =>    ['DISPLAY' => ':99'],
    'sendSyncDefaultTimeout'    =>    40000,
    'debugLogger'                 => 'php://stdout',
]);
$page = $browser->createPage();

$page
    ->getSession()
    ->on('method:Fetch.requestPaused', function ($params) use ($page) {
        echo date('r') . ' received Fetch.requestPaused '/*.print_r($params,true)*/ . "\n";
        $page
            ->getSession()
            ->sendMessageSync(new \HeadlessChromium\Communication\Message('Fetch.continueRequest', [
                'requestId' => $params["requestId"]
            ]));
    });

$page
    ->getSession()
    ->sendMessageSync(new \HeadlessChromium\Communication\Message('Fetch.enable', [
        "handleAuthRequests" => true,
        "patterns" => [
            ['urlPattern' => '*']
        ]
    ]));
$attempts = 0;
for (;;) {
    ++$attempts;
    try {
        $navigation = $page->navigate('https://www.smithsfoodanddrug.com/');
        $navigation->waitForNavigation(\HeadlessChromium\Page::LOAD, 50000);
        //$navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);

        $navigation = $page->navigate('https://www.smithsfoodanddrug.com/signin?redirectUrl=/');
        $navigation->waitForNavigation(\HeadlessChromium\Page::LOAD, 50000);
        //$navigation->waitForNavigation(\HeadlessChromium\Page::NETWORK_IDLE, 50000);
    } catch (\Throwable $e) {
        echo "Attempt $attempts reproduces the issue\n";
        throw $e;
    }
    echo "Attempt $attempts completed\n";
    sleep(10);
}

will reproduce it every time, but it may take a few minutes.

@divinity76
Copy link
Contributor

divinity76 commented Nov 28, 2024

warning, when trying to copy out just the invalid json, it seems VSCode actually converted it to valid json (maybe stripped some invalid utf8?), which means the json in my post may be valid json, and you'll need to check the full 5MB log to actually see the invalid json...

which will not make it any easier to debug this 🤔

sorry, this is too much for me to look into for free, hope someone else can investigate it.

@momala454
Copy link
Contributor Author

thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants