grafana / xk6-browser

The browser module adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol to k6.
https://grafana.com/docs/k6/latest/javascript-api/k6-browser/
GNU Affero General Public License v3.0
342 stars 42 forks source link

Handle schemeless URLs gracefully #243

Open imiric opened 2 years ago

imiric commented 2 years ago

On current main (917b83e) on Linux and Chromium 97, running the following script:

import launcher from 'k6/x/browser';

export default function() {
  const browser = launcher.launch('chromium', {
    headless: false,
  });
  const context = browser.newContext();
  const page = context.newPage();

  // Note no http:// prefix
  page.goto('localhost:3000/');

  page.close();
  browser.close();
}

... results in a prompt to handle the URL with xdg-open:

2022-02-16-170717_799x244_scrot

Regardless if the prompt is dismissed or not, the test ends after ~30s with a NavigateFrame timeout:

DEBU[0030] fmid:1 fid:B57954A19F450613CA0161AD1050D0EB furl:about:blank url:localhost:3000/  category="FrameManager:NavigateFrame:return" elapsed="29016 ms" goroutine=76
ERRO[0030] timed out
        at reflect.methodValueCall (native)
        at file:///home/ivan/Projects/grafana/xk6-browser/examples/226-waitfornavigation-timeout.js:10:2(24)
        at native  executor=per-vu-iterations scenario=default source=stacktrace

I'm not sure what the behavior is on other OSs. @inancgumus can you confirm what happens on macOS?

Regardless, we should handle this gracefully, maybe similarly how k6 handles it for http.get('localhost:3000'):

WARN[0000] Request Failed    error="Get \"localhost:3000/\": unsupported protocol scheme \"localhost\""

Or at least to pass it directly to Chrome and bypass the prompt, since browsers will default to http:// anyway.

It also might be a security issue if someone crafts a clever URL-like string that actually opens a file or runs a binary on the system, depending on xdg-open or open default configuration I guess.

inancgumus commented 2 years ago

@imiric,

It opens up a browser that hangs on "about:blank" and sometime after results in:

ERRO[0001] Failed to launch 'localhost:3000/' because the scheme does not have a registered handler.  browser_source=other line_number=0 source=browser stacktrace="<nil>" url="about:blank"
ERRO[0031] timed out
        at reflect.methodValueCall (native)
        at file:///Users/inanc/grafana/tests/schemaless249.js:11:2(24)
        at native  executor=per-vu-iterations scenario=default source=stacktrace
Full log (related parts) ```bash DEBU[0001] tid:0AE3478A9D045308B5CE1E41B7ACCC68 bctxid:0A58C8E5A51D38423A53086F7659D107 category="Browser:newPageInContext:<-waitForPage" elapsed="0 ms" goroutine=68 DEBU[0001] bctxid:0A58C8E5A51D38423A53086F7659D107 ptid:0AE3478A9D045308B5CE1E41B7ACCC68 category="BrowserContext:NewPage:return" elapsed="0 ms" goroutine=68 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 url:"localhost:3000/" category="Page:Goto" elapsed="1 ms" goroutine=68 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 mfid:0AE3478A9D045308B5CE1E41B7ACCC68 mflid:96F69CD1A98064BA838E880A9275ADE3 mfurl:about:blank category="Page:MainFrame" elapsed="0 ms" goroutine=68 DEBU[0001] fmid:1 fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:about:blank url:localhost:3000/ category="FrameManager:NavigateFrame" elapsed="0 ms" goroutine=68 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 fid:0AE3478A9D045308B5CE1E41B7ACCC68 category="Page:getFrameSession" elapsed="0 ms" goroutine=68 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 tid:0AE3478A9D045308B5CE1E41B7ACCC68 url:"localhost:3000/" referrer:"" category="FrameSession:navigateFrame" elapsed="0 ms" goroutine=68 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 tid:0AE3478A9D045308B5CE1E41B7ACCC68 method:"Page.navigate" category="Session:Execute" elapsed="0 ms" goroutine=68 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 tid:0AE3478A9D045308B5CE1E41B7ACCC68 method:"Page.navigate" category="Session:Execute:s.conn.send" elapsed="0 ms" goroutine=68 TRAC[0001] -> {"id":20,"sessionId":"7C25323FD0D16F96B2388A2E82031581","method":"Page.navigate","params":{"url":"localhost:3000/","frameId":"0AE3478A9D045308B5CE1E41B7ACCC68"}} category="cdp:send" elapsed="0 ms" goroutine=28 TRAC[0001] <- {"method":"Network.requestWillBeSent","params":{"requestId":"2BA45486BA9168B9DA469D28D8F02589","loaderId":"2BA45486BA9168B9DA469D28D8F02589","documentURL":"localhost:3000/","request":{"url":"localhost:3000/","method":"GET","headers":{"Accept-Language":"en-US"},"mixedContentType":"none","initialPriority":"VeryHigh","referrerPolicy":"strict-origin-when-cross-origin","isSameSite":true},"timestamp":642750.32225,"wallTime":1645082371.530484,"initiator":{"type":"other"},"redirectHasExtraInfo":false,"type":"Document","frameId":"0AE3478A9D045308B5CE1E41B7ACCC68","hasUserGesture":false},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="1 ms" goroutine=27 DEBU[0001] fmid:1 rurl:localhost:3000/ category="FrameManager:requestStarted" elapsed="0 ms" goroutine=87 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" rid:2BA45486BA9168B9DA469D28D8F02589 category="Frame:addRequest" elapsed="0 ms" goroutine=87 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" category="Frame:stopNetworkIdleTimer" elapsed="0 ms" goroutine=87 DEBU[0001] fmid:1 rurl:localhost:3000/ pdoc:nil category="FrameManager:requestStarted" elapsed="0 ms" goroutine=87 TRAC[0001] <- {"method":"Network.loadingFailed","params":{"requestId":"2BA45486BA9168B9DA469D28D8F02589","timestamp":642750.322625,"type":"Document","errorText":"net::ERR_ABORTED","canceled":true},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="0 ms" goroutine=27 DEBU[0001] fmid:1 rurl:localhost:3000/ category="FrameManager:requestFailed" elapsed="0 ms" goroutine=87 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" rid:2BA45486BA9168B9DA469D28D8F02589 category="Frame:deleteRequest" elapsed="0 ms" goroutine=87 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" category="Frame:startNetworkIdleTimer" elapsed="1 ms" goroutine=87 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" category="Frame:stopNetworkIdleTimer" elapsed="0 ms" goroutine=87 DEBU[0001] fmid:1 fid:0AE3478A9D045308B5CE1E41B7ACCC68 err:net::ERR_ABORTED; maybe frame was detached? docid:2BA45486BA9168B9DA469D28D8F02589 category="FrameManager:frameAbortedNavigation" elapsed="0 ms" goroutine=87 DEBU[0001] fmid:1 fid:0AE3478A9D045308B5CE1E41B7ACCC68 err:net::ERR_ABORTED; maybe frame was detached? docid:2BA45486BA9168B9DA469D28D8F02589 fname: furl:about:blank category="FrameManager:frameAbortedNavigation:emit:EventFrameNavigation" elapsed="0 ms" goroutine=87 TRAC[0001] <- {"id":20,"result":{"frameId":"0AE3478A9D045308B5CE1E41B7ACCC68","loaderId":"2BA45486BA9168B9DA469D28D8F02589","errorText":"net::ERR_ABORTED"},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="0 ms" goroutine=27 TRAC[0001] <- {"method":"Network.loadingFailed","params":{"requestId":"2BA45486BA9168B9DA469D28D8F02589","timestamp":642750.322875,"type":"Document","errorText":"net::ERR_ABORTED","canceled":true},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="0 ms" goroutine=27 DEBU[0001] fmid:1 fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:about:blank url:localhost:3000/ newDocID:2BA45486BA9168B9DA469D28D8F02589 category="FrameManager:NavigateFrame" elapsed="0 ms" goroutine=68 TRAC[0001] <- {"method":"Log.entryAdded","params":{"entry":{"source":"other","level":"error","text":"Failed to launch 'localhost:3000/' because the scheme does not have a registered handler.","timestamp":1.645082371531539e+12,"url":"about:blank"}},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="0 ms" goroutine=27 ERRO[0001] Failed to launch 'localhost:3000/' because the scheme does not have a registered handler. browser_source=other line_number=0 source=browser stacktrace="" url="about:blank" TRAC[0001] <- {"method":"Page.lifecycleEvent","params":{"frameId":"0AE3478A9D045308B5CE1E41B7ACCC68","loaderId":"96F69CD1A98064BA838E880A9275ADE3","name":"networkAlmostIdle","timestamp":642750.27325},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="462 ms" goroutine=27 TRAC[0001] <- {"method":"Page.lifecycleEvent","params":{"frameId":"0AE3478A9D045308B5CE1E41B7ACCC68","loaderId":"96F69CD1A98064BA838E880A9275ADE3","name":"networkIdle","timestamp":642750.27325},"sessionId":"7C25323FD0D16F96B2388A2E82031581"} category="cdp:recv" elapsed="0 ms" goroutine=27 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 tid:0AE3478A9D045308B5CE1E41B7ACCC68 fid:0AE3478A9D045308B5CE1E41B7ACCC68 event:"networkAlmostIdle" category="FrameSession:onPageLifecycle" elapsed="0 ms" goroutine=90 DEBU[0001] sid:7C25323FD0D16F96B2388A2E82031581 tid:0AE3478A9D045308B5CE1E41B7ACCC68 fid:0AE3478A9D045308B5CE1E41B7ACCC68 event:"networkIdle" category="FrameSession:onPageLifecycle" elapsed="0 ms" goroutine=90 DEBU[0001] fmid:1 fid:0AE3478A9D045308B5CE1E41B7ACCC68 event:networkidle category="FrameManager:frameLifecycleEvent" elapsed="38 ms" goroutine=178 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" event:networkidle category="Frame:onLifecycleEvent" elapsed="0 ms" goroutine=178 DEBU[0001] fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:"about:blank" category="Frame:recalculateLifecycle" elapsed="0 ms" goroutine=178 DEBU[0031] fmid:1 fid:0AE3478A9D045308B5CE1E41B7ACCC68 furl:about:blank url:localhost:3000/ category="FrameManager:NavigateFrame:return" elapsed="29500 ms" goroutine=68 ERRO[0031] timed out at reflect.methodValueCall (native) at file:///Users/inanc/grafana/tests/schemaless249.js:11:2(24) at native executor=per-vu-iterations scenario=default source=stacktrace ```

Suspicious logline:

TRAC[0001] <- {"method":"Log.entryAdded",
"params":{
"entry":{
  "source":"other"
  "level":"error",
  "text":"Failed to launch 'localhost:3000/' because the scheme does not have a registered handler.",
  "timestamp":1.645082371531539e+12,
  "url":"about:blank"
}},
"sessionId":"7C25323FD0D16F96B2388A2E82031581"}  
category="cdp:recv" elapsed="0 ms" goroutine=27