playwright_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. //go:build !windows
  2. // Integration tests for Anubis, using Playwright.
  3. //
  4. // These tests require an already running Anubis and Playwright server.
  5. //
  6. // Anubis must be configured to redirect to the server started by the test suite.
  7. // The bind address and the Anubis server can be specified using the flags `-bind` and `-anubis` respectively.
  8. //
  9. // Playwright must be started in server mode using `npx playwright@1.50.1 run-server --port 3000`.
  10. // The version must match the minor used by the playwright-go package.
  11. //
  12. // On unsupported systems you may be able to use a container instead: https://playwright.dev/docs/docker#remote-connection
  13. //
  14. // In that case you may need to set the `-playwright` flag to the container's URL, and specify the `--host` the run-server command listens on.
  15. package test
  16. import (
  17. "flag"
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "net/http/httptest"
  22. "net/url"
  23. "os"
  24. "os/exec"
  25. "strconv"
  26. "testing"
  27. "time"
  28. "github.com/TecharoHQ/anubis"
  29. libanubis "github.com/TecharoHQ/anubis/lib"
  30. "github.com/playwright-community/playwright-go"
  31. )
  32. var (
  33. playwrightPort = flag.Int("playwright-port", 9001, "Playwright port")
  34. playwrightServer = flag.String("playwright", "ws://localhost:9001", "Playwright server URL")
  35. playwrightMaxTime = flag.Duration("playwright-max-time", 5*time.Second, "maximum time for Playwright requests")
  36. playwrightMaxHardTime = flag.Duration("playwright-max-hard-time", 5*time.Minute, "maximum time for hard Playwright requests")
  37. playwrightRunner = flag.String("playwright-runner", "npx", "how to start Playwright, can be: none,npx,docker,podman")
  38. testCases = []testCase{
  39. {
  40. name: "firefox",
  41. action: actionChallenge,
  42. realIP: placeholderIP,
  43. userAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0",
  44. },
  45. {
  46. name: "headlessChrome",
  47. action: actionDeny,
  48. realIP: placeholderIP,
  49. userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/120.0.6099.28 Safari/537.36",
  50. },
  51. {
  52. name: "Amazonbot",
  53. action: actionDeny,
  54. realIP: placeholderIP,
  55. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5 (Amazonbot/0.1; +https://developer.amazon.com/support/amazonbot)",
  56. },
  57. {
  58. name: "Amazonbot",
  59. action: actionDeny,
  60. realIP: placeholderIP,
  61. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/8.0.2 Safari/600.2.5 (Amazonbot/0.1; +https://developer.amazon.com/support/amazonbot)",
  62. },
  63. {
  64. name: "PerplexityAI",
  65. action: actionDeny,
  66. realIP: placeholderIP,
  67. userAgent: "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; PerplexityBot/1.0; +https://perplexity.ai/perplexitybot)",
  68. },
  69. {
  70. name: "kagiBadIP",
  71. action: actionChallenge,
  72. isHard: true,
  73. realIP: placeholderIP,
  74. userAgent: "Mozilla/5.0 (compatible; Kagibot/1.0; +https://kagi.com/bot)",
  75. },
  76. {
  77. name: "kagiGoodIP",
  78. action: actionAllow,
  79. realIP: "216.18.205.234",
  80. userAgent: "Mozilla/5.0 (compatible; Kagibot/1.0; +https://kagi.com/bot)",
  81. },
  82. {
  83. name: "unknownAgent",
  84. action: actionAllow,
  85. realIP: placeholderIP,
  86. userAgent: "AnubisTest/0",
  87. },
  88. }
  89. )
  90. const (
  91. actionAllow action = "ALLOW"
  92. actionDeny action = "DENY"
  93. actionChallenge action = "CHALLENGE"
  94. placeholderIP = "fd11:5ee:bad:c0de::"
  95. playwrightVersion = "1.52.0"
  96. )
  97. type action string
  98. type testCase struct {
  99. name string
  100. action action
  101. realIP string
  102. userAgent string
  103. isHard bool
  104. }
  105. func doesCommandExist(t *testing.T, command string) {
  106. t.Helper()
  107. if _, err := exec.LookPath(command); err != nil {
  108. t.Skipf("%s not found in PATH, skipping integration smoke testing: %v", command, err)
  109. }
  110. }
  111. func run(t *testing.T, command string) string {
  112. if testing.Short() {
  113. t.Skip("skipping integration smoke testing in short mode")
  114. }
  115. t.Helper()
  116. shPath, err := exec.LookPath("sh")
  117. if err != nil {
  118. t.Fatalf("[unexpected] %v", err)
  119. }
  120. t.Logf("running command: %s", command)
  121. cmd := exec.Command(shPath, "-c", command)
  122. cmd.Stdin = nil
  123. cmd.Stderr = os.Stderr
  124. output, err := cmd.Output()
  125. if err != nil {
  126. t.Fatalf("can't run command: %v", err)
  127. }
  128. return string(output)
  129. }
  130. func daemonize(t *testing.T, command string) {
  131. t.Helper()
  132. shPath, err := exec.LookPath("sh")
  133. if err != nil {
  134. t.Fatalf("[unexpected] %v", err)
  135. }
  136. t.Logf("daemonizing command: %s", command)
  137. cmd := exec.Command(shPath, "-c", command)
  138. cmd.Stdin = nil
  139. cmd.Stderr = os.Stderr
  140. cmd.Stdout = os.Stdout
  141. if err := cmd.Start(); err != nil {
  142. t.Fatalf("can't daemonize command: %v", err)
  143. }
  144. t.Cleanup(func() {
  145. cmd.Process.Kill()
  146. })
  147. }
  148. func startPlaywright(t *testing.T) {
  149. t.Helper()
  150. if *playwrightRunner == "npx" {
  151. doesCommandExist(t, "npx")
  152. if os.Getenv("CI") == "true" {
  153. run(t, fmt.Sprintf("npx --yes playwright@%s install --with-deps", playwrightVersion))
  154. } else {
  155. run(t, fmt.Sprintf("npx --yes playwright@%s install", playwrightVersion))
  156. }
  157. daemonize(t, fmt.Sprintf("npx --yes playwright@%s run-server --port %d", playwrightVersion, *playwrightPort))
  158. } else if *playwrightRunner == "docker" || *playwrightRunner == "podman" {
  159. doesCommandExist(t, *playwrightRunner)
  160. // docs: https://playwright.dev/docs/docker
  161. pwcmd := fmt.Sprintf("npx -y playwright@%s run-server --port %d --host 0.0.0.0", playwrightVersion, *playwrightPort)
  162. container := run(t, fmt.Sprintf("%s run -d --ipc=host --user pwuser --workdir /home/pwuser --net=host mcr.microsoft.com/playwright:v%s-noble /bin/sh -c \"%s\"", *playwrightRunner, playwrightVersion, pwcmd))
  163. t.Cleanup(func() {
  164. run(t, fmt.Sprintf("%s rm --force %s", *playwrightRunner, container))
  165. })
  166. } else if *playwrightRunner == "none" {
  167. t.Log("not starting Playwright, assuming it is already running")
  168. } else {
  169. t.Skipf("unknown runner: %s, skipping", *playwrightRunner)
  170. }
  171. for {
  172. if _, err := http.Get(fmt.Sprintf("http://localhost:%d", *playwrightPort)); err != nil {
  173. time.Sleep(500 * time.Millisecond)
  174. continue
  175. }
  176. break
  177. }
  178. //nosleep:bypass XXX(Xe): Playwright doesn't have a good way to signal readiness. This is a HACK that will just let the tests pass.
  179. time.Sleep(2 * time.Second)
  180. }
  181. func TestPlaywrightBrowser(t *testing.T) {
  182. if os.Getenv("DONT_USE_NETWORK") != "" {
  183. t.Skip("test requires network egress")
  184. return
  185. }
  186. if os.Getenv("SKIP_INTEGRATION") != "" {
  187. t.Skip("SKIP_INTEGRATION was set")
  188. return
  189. }
  190. startPlaywright(t)
  191. pw := setupPlaywright(t)
  192. anubisURL := spawnAnubis(t)
  193. browsers := []playwright.BrowserType{pw.Chromium, pw.Firefox, pw.WebKit}
  194. for _, typ := range browsers {
  195. t.Run(typ.Name()+"/warmup", func(t *testing.T) {
  196. browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
  197. ExposeNetwork: playwright.String("<loopback>"),
  198. })
  199. if err != nil {
  200. t.Fatalf("could not connect to remote browser: %v", err)
  201. }
  202. defer browser.Close()
  203. ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
  204. AcceptDownloads: playwright.Bool(false),
  205. ExtraHttpHeaders: map[string]string{
  206. "X-Real-Ip": "127.0.0.1",
  207. },
  208. UserAgent: playwright.String("Sephiroth"),
  209. })
  210. if err != nil {
  211. t.Fatalf("could not create context: %v", err)
  212. }
  213. defer ctx.Close()
  214. page, err := ctx.NewPage()
  215. if err != nil {
  216. t.Fatalf("could not create page: %v", err)
  217. }
  218. defer page.Close()
  219. timeout := 2.0
  220. page.Goto(anubisURL, playwright.PageGotoOptions{
  221. Timeout: &timeout,
  222. })
  223. })
  224. for _, tc := range testCases {
  225. name := fmt.Sprintf("%s/%s", typ.Name(), tc.name)
  226. t.Run(name, func(t *testing.T) {
  227. _, hasDeadline := t.Deadline()
  228. if tc.isHard && hasDeadline {
  229. t.Skip("skipping hard challenge with deadline")
  230. }
  231. var performedAction action
  232. var err error
  233. for i := 0; i < 5; i++ {
  234. performedAction, err = executeTestCase(t, tc, typ, anubisURL)
  235. if performedAction == tc.action {
  236. break
  237. }
  238. time.Sleep(time.Duration(i+1) * 250 * time.Millisecond)
  239. }
  240. if performedAction != tc.action {
  241. t.Errorf("unexpected test result, expected %s, got %s", tc.action, performedAction)
  242. }
  243. if err != nil {
  244. t.Fatalf("test error: %v", err)
  245. }
  246. })
  247. }
  248. }
  249. }
  250. func TestPlaywrightWithBasePrefix(t *testing.T) {
  251. if os.Getenv("DONT_USE_NETWORK") != "" {
  252. t.Skip("test requires network egress")
  253. return
  254. }
  255. if os.Getenv("SKIP_INTEGRATION") != "" {
  256. t.Skip("SKIP_INTEGRATION was set")
  257. return
  258. }
  259. t.Skip("NOTE(Xe)\\ these tests require HTTPS support in #364")
  260. startPlaywright(t)
  261. pw := setupPlaywright(t)
  262. basePrefix := "/myapp"
  263. anubisURL := spawnAnubisWithOptions(t, basePrefix)
  264. // Reset BasePrefix after test
  265. t.Cleanup(func() {
  266. anubis.BasePrefix = ""
  267. })
  268. browsers := []playwright.BrowserType{pw.Chromium}
  269. for _, typ := range browsers {
  270. t.Run(typ.Name()+"/basePrefix", func(t *testing.T) {
  271. browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
  272. ExposeNetwork: playwright.String("<loopback>"),
  273. })
  274. if err != nil {
  275. t.Fatalf("could not connect to remote browser: %v", err)
  276. }
  277. defer browser.Close()
  278. ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
  279. AcceptDownloads: playwright.Bool(false),
  280. ExtraHttpHeaders: map[string]string{
  281. "X-Real-Ip": "127.0.0.1",
  282. },
  283. UserAgent: playwright.String("Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0"),
  284. })
  285. if err != nil {
  286. t.Fatalf("could not create context: %v", err)
  287. }
  288. defer ctx.Close()
  289. page, err := ctx.NewPage()
  290. if err != nil {
  291. t.Fatalf("could not create page: %v", err)
  292. }
  293. defer page.Close()
  294. // Test accessing the base URL with prefix
  295. _, err = page.Goto(anubisURL+basePrefix, playwright.PageGotoOptions{
  296. Timeout: pwTimeout(testCases[0], time.Now().Add(5*time.Second)),
  297. })
  298. if err != nil {
  299. pwFail(t, page, "could not navigate to test server with base prefix: %v", err)
  300. }
  301. // Check if challenge page is displayed
  302. image := page.Locator("#image[src*=pensive], #image[src*=happy]")
  303. err = image.WaitFor(playwright.LocatorWaitForOptions{
  304. Timeout: pwTimeout(testCases[0], time.Now().Add(5*time.Second)),
  305. })
  306. if err != nil {
  307. pwFail(t, page, "could not wait for challenge image: %v", err)
  308. }
  309. isVisible, err := image.IsVisible()
  310. if err != nil {
  311. pwFail(t, page, "could not check if challenge image is visible: %v", err)
  312. }
  313. if !isVisible {
  314. pwFail(t, page, "challenge image not visible")
  315. }
  316. // Complete the challenge
  317. // Wait for the challenge to be solved
  318. anubisTest := page.Locator("#anubis-test")
  319. err = anubisTest.WaitFor(playwright.LocatorWaitForOptions{
  320. Timeout: pwTimeout(testCases[0], time.Now().Add(30*time.Second)),
  321. })
  322. if err != nil {
  323. pwFail(t, page, "could not wait for challenge to be solved: %v", err)
  324. }
  325. // Verify the challenge was solved
  326. content, err := anubisTest.TextContent(playwright.LocatorTextContentOptions{})
  327. if err != nil {
  328. pwFail(t, page, "could not get text content: %v", err)
  329. }
  330. var tm int64
  331. if _, err := fmt.Sscanf(content, "%d", &tm); err != nil {
  332. pwFail(t, page, "unexpected output: %s", content)
  333. }
  334. // Check if the timestamp is reasonable
  335. now := time.Now().Unix()
  336. if tm < now-60 || tm > now+60 {
  337. pwFail(t, page, "unexpected timestamp in output: %d not in range %d±60", tm, now)
  338. }
  339. // Check if cookie has the correct path
  340. cookies, err := ctx.Cookies()
  341. if err != nil {
  342. pwFail(t, page, "could not get cookies: %v", err)
  343. }
  344. var found bool
  345. for _, cookie := range cookies {
  346. if cookie.Name == anubis.CookieName {
  347. found = true
  348. if cookie.Path != basePrefix+"/" {
  349. t.Errorf("cookie path is wrong, wanted %s, got: %s", basePrefix+"/", cookie.Path)
  350. }
  351. break
  352. }
  353. }
  354. if !found {
  355. t.Errorf("Cookie %q not found", anubis.CookieName)
  356. }
  357. })
  358. }
  359. }
  360. func buildBrowserConnect(name string) string {
  361. u, _ := url.Parse(*playwrightServer)
  362. q := u.Query()
  363. q.Set("browser", name)
  364. u.RawQuery = q.Encode()
  365. return u.String()
  366. }
  367. func executeTestCase(t *testing.T, tc testCase, typ playwright.BrowserType, anubisURL string) (action, error) {
  368. deadline, _ := t.Deadline()
  369. browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
  370. ExposeNetwork: playwright.String("<loopback>"),
  371. })
  372. if err != nil {
  373. return "", fmt.Errorf("could not connect to remote browser: %w", err)
  374. }
  375. defer browser.Close()
  376. ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
  377. AcceptDownloads: playwright.Bool(false),
  378. ExtraHttpHeaders: map[string]string{
  379. "X-Real-Ip": tc.realIP,
  380. },
  381. UserAgent: playwright.String(tc.userAgent),
  382. })
  383. if err != nil {
  384. return "", fmt.Errorf("could not create context: %w", err)
  385. }
  386. defer ctx.Close()
  387. page, err := ctx.NewPage()
  388. if err != nil {
  389. return "", fmt.Errorf("could not create page: %w", err)
  390. }
  391. defer page.Close()
  392. // Attempt challenge.
  393. start := time.Now()
  394. _, err = page.Goto(anubisURL, playwright.PageGotoOptions{
  395. Timeout: pwTimeout(tc, deadline),
  396. })
  397. if err != nil {
  398. return "", pwFail(t, page, "could not navigate to test server: %v", err)
  399. }
  400. hadChallenge := false
  401. switch tc.action {
  402. case actionChallenge:
  403. // FIXME: This could race if challenge is completed too quickly.
  404. checkImage(t, tc, deadline, page, "#image[src*=pensive], #image[src*=happy]")
  405. hadChallenge = true
  406. case actionDeny:
  407. checkImage(t, tc, deadline, page, "#image[src*=sad]")
  408. return actionDeny, nil
  409. }
  410. // Ensure protected resource was provided.
  411. res, err := page.Locator("#anubis-test").TextContent(playwright.LocatorTextContentOptions{
  412. Timeout: pwTimeout(tc, deadline),
  413. })
  414. end := time.Now()
  415. if err != nil {
  416. pwFail(t, page, "could not get text content: %v", err)
  417. }
  418. var tm int64
  419. if _, err := fmt.Sscanf(res, "%d", &tm); err != nil {
  420. pwFail(t, page, "unexpected output: %s", res)
  421. }
  422. if tm < start.Unix() || end.Unix() < tm {
  423. pwFail(t, page, "unexpected timestamp in output: %d not in range %d..%d", tm, start.Unix(), end.Unix())
  424. }
  425. if hadChallenge {
  426. return actionChallenge, nil
  427. } else {
  428. return actionAllow, nil
  429. }
  430. }
  431. func checkImage(t *testing.T, tc testCase, deadline time.Time, page playwright.Page, locator string) {
  432. image := page.Locator(locator)
  433. err := image.WaitFor(playwright.LocatorWaitForOptions{
  434. Timeout: pwTimeout(tc, deadline),
  435. })
  436. if err != nil {
  437. pwFail(t, page, "could not wait for result: %v", err)
  438. }
  439. failIsVisible, err := image.IsVisible()
  440. if err != nil {
  441. pwFail(t, page, "could not check result image: %v", err)
  442. }
  443. if !failIsVisible {
  444. pwFail(t, page, "expected result image not visible")
  445. }
  446. }
  447. func pwFail(t *testing.T, page playwright.Page, format string, args ...any) error {
  448. t.Helper()
  449. saveScreenshot(t, page)
  450. return fmt.Errorf(format, args...)
  451. }
  452. func pwTimeout(tc testCase, deadline time.Time) *float64 {
  453. maxTime := *playwrightMaxTime
  454. if tc.isHard {
  455. maxTime = *playwrightMaxHardTime
  456. }
  457. d := time.Until(deadline)
  458. if d <= 0 || d > maxTime {
  459. return playwright.Float(float64(maxTime.Milliseconds()))
  460. }
  461. return playwright.Float(float64(d.Milliseconds()))
  462. }
  463. func saveScreenshot(t *testing.T, page playwright.Page) {
  464. t.Helper()
  465. data, err := page.Screenshot()
  466. if err != nil {
  467. t.Logf("could not take screenshot: %v", err)
  468. return
  469. }
  470. f, err := os.CreateTemp("", "anubis-test-fail-*.png")
  471. if err != nil {
  472. t.Logf("could not create temporary file: %v", err)
  473. return
  474. }
  475. defer f.Close()
  476. _, err = f.Write(data)
  477. if err != nil {
  478. t.Logf("could not write screenshot: %v", err)
  479. return
  480. }
  481. t.Logf("screenshot saved to %s", f.Name())
  482. }
  483. func setupPlaywright(t *testing.T) *playwright.Playwright {
  484. err := playwright.Install(&playwright.RunOptions{
  485. SkipInstallBrowsers: true,
  486. })
  487. if err != nil {
  488. t.Fatalf("could not install Playwright: %v", err)
  489. }
  490. pw, err := playwright.Run()
  491. if err != nil {
  492. t.Fatalf("could not start Playwright: %v", err)
  493. }
  494. return pw
  495. }
  496. func spawnAnubis(t *testing.T) string {
  497. return spawnAnubisWithOptions(t, "")
  498. }
  499. func spawnAnubisWithOptions(t *testing.T, basePrefix string) string {
  500. t.Helper()
  501. h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  502. w.Header().Add("Content-Type", "text/html")
  503. fmt.Fprintf(w, "<html><body><span id=anubis-test>%d</span></body></html>", time.Now().Unix())
  504. })
  505. policy, err := libanubis.LoadPoliciesOrDefault(t.Context(), "", anubis.DefaultDifficulty)
  506. if err != nil {
  507. t.Fatal(err)
  508. }
  509. listener, err := net.Listen("tcp", ":0")
  510. if err != nil {
  511. t.Fatalf("can't listen on random port: %v", err)
  512. }
  513. addr := listener.Addr().(*net.TCPAddr)
  514. host := "localhost"
  515. port := strconv.Itoa(addr.Port)
  516. s, err := libanubis.New(libanubis.Options{
  517. Next: h,
  518. Policy: policy,
  519. ServeRobotsTXT: true,
  520. Target: "http://" + host + ":" + port,
  521. BasePrefix: basePrefix,
  522. })
  523. if err != nil {
  524. t.Fatalf("can't construct libanubis.Server: %v", err)
  525. }
  526. ts := &httptest.Server{
  527. Listener: listener,
  528. Config: &http.Server{Handler: s},
  529. }
  530. ts.Start()
  531. t.Log(ts.URL)
  532. t.Cleanup(func() {
  533. ts.Close()
  534. })
  535. return ts.URL
  536. }