playwright_test.go 16 KB

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