Skip to content

Timeout issues when setting cookies #704

@ianef

Description

@ianef

Hi, I am trying to use chrome-php to create PDFs from protected pages. The PDF pages are under the same security restrictions as the page that is running chrome-php.

My goal is to fetch the current PHP session ID, set that into the headers of the chrome-php request, and make the call to the PDF page using the security of the current user.

If I call $page->setCookies(...)->await(), the subsequent $page->navigate(....)->waitForNavigation() hangs the initial request and times out, but after that the call is made to the PDF page with the cookies set.

If I don't call ->await() after $page->setCookies() then $page->navigate(....)->waitForNavigation() runs as expected but the cookies are not set.

Here's the chunk of code

        $browserFactory = new BrowserFactory($this->chromeBinary);
        $browser = $browserFactory->createBrowser([
            'noSandbox' => true,
            'customFlags' => ['--remote-allow-origins=*']
        ]);

        try {
            $page = $browser->createPage();
            // If we have a session, then I want to use the session cookie to authenticate chrome-php
            if ($request !== null && $request->getSession() !== null) {
                $session = $request->getSession();
                // According to some Puppeteer docs, we need to navigate to a page first, this works.
                $page->navigate($request->getSchemeAndHttpHost() . '/')->waitForNavigation();
                // Now we can set the session cookie
                $page->setCookies([
                    Cookie::create($session->getName(), $session->getId(), [
                        'domain' => $request->getHost(),
                        'expires' => time() + 3600, // 1 hour
                    ]),
                ])->await();
            }
            // But then this hangs
            $page->navigate($pdfUrl)->waitForNavigation();

            // Save to a pdf
            $page->pdf()->saveToFile($pdfFile);
        } finally {
            // bye
            $browser->close();
        }

Any help in working out why waitForNavigation() hangs after calling setCookies()->await() would be immensely helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions