playwright_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 buildBrowserConnect(name string) string {
  231. u, _ := url.Parse(*playwrightServer)
  232. q := u.Query()
  233. q.Set("browser", name)
  234. u.RawQuery = q.Encode()
  235. return u.String()
  236. }
  237. func executeTestCase(t *testing.T, tc testCase, typ playwright.BrowserType, anubisURL string) (action, error) {
  238. deadline, _ := t.Deadline()
  239. browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
  240. ExposeNetwork: playwright.String("<loopback>"),
  241. })
  242. if err != nil {
  243. return "", fmt.Errorf("could not connect to remote browser: %w", err)
  244. }
  245. defer browser.Close()
  246. ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
  247. AcceptDownloads: playwright.Bool(false),
  248. ExtraHttpHeaders: map[string]string{
  249. "X-Real-Ip": tc.realIP,
  250. },
  251. UserAgent: playwright.String(tc.userAgent),
  252. })
  253. if err != nil {
  254. return "", fmt.Errorf("could not create context: %w", err)
  255. }
  256. defer ctx.Close()
  257. page, err := ctx.NewPage()
  258. if err != nil {
  259. return "", fmt.Errorf("could not create page: %w", err)
  260. }
  261. defer page.Close()
  262. // Attempt challenge.
  263. start := time.Now()
  264. _, err = page.Goto(anubisURL, playwright.PageGotoOptions{
  265. Timeout: pwTimeout(tc, deadline),
  266. })
  267. if err != nil {
  268. return "", pwFail(t, page, "could not navigate to test server: %v", err)
  269. }
  270. hadChallenge := false
  271. switch tc.action {
  272. case actionChallenge:
  273. // FIXME: This could race if challenge is completed too quickly.
  274. checkImage(t, tc, deadline, page, "#image[src*=pensive], #image[src*=happy]")
  275. hadChallenge = true
  276. case actionDeny:
  277. checkImage(t, tc, deadline, page, "#image[src*=sad]")
  278. return actionDeny, nil
  279. }
  280. // Ensure protected resource was provided.
  281. res, err := page.Locator("#anubis-test").TextContent(playwright.LocatorTextContentOptions{
  282. Timeout: pwTimeout(tc, deadline),
  283. })
  284. end := time.Now()
  285. if err != nil {
  286. pwFail(t, page, "could not get text content: %v", err)
  287. }
  288. var tm int64
  289. if _, err := fmt.Sscanf(res, "%d", &tm); err != nil {
  290. pwFail(t, page, "unexpected output: %s", res)
  291. }
  292. if tm < start.Unix() || end.Unix() < tm {
  293. pwFail(t, page, "unexpected timestamp in output: %d not in range %d..%d", tm, start.Unix(), end.Unix())
  294. }
  295. if hadChallenge {
  296. return actionChallenge, nil
  297. } else {
  298. return actionAllow, nil
  299. }
  300. }
  301. func checkImage(t *testing.T, tc testCase, deadline time.Time, page playwright.Page, locator string) {
  302. image := page.Locator(locator)
  303. err := image.WaitFor(playwright.LocatorWaitForOptions{
  304. Timeout: pwTimeout(tc, deadline),
  305. })
  306. if err != nil {
  307. pwFail(t, page, "could not wait for result: %v", err)
  308. }
  309. failIsVisible, err := image.IsVisible()
  310. if err != nil {
  311. pwFail(t, page, "could not check result image: %v", err)
  312. }
  313. if !failIsVisible {
  314. pwFail(t, page, "expected result image not visible")
  315. }
  316. }
  317. func pwFail(t *testing.T, page playwright.Page, format string, args ...any) error {
  318. t.Helper()
  319. saveScreenshot(t, page)
  320. return fmt.Errorf(format, args...)
  321. }
  322. func pwTimeout(tc testCase, deadline time.Time) *float64 {
  323. maxTime := *playwrightMaxTime
  324. if tc.isHard {
  325. maxTime = *playwrightMaxHardTime
  326. }
  327. d := time.Until(deadline)
  328. if d <= 0 || d > maxTime {
  329. return playwright.Float(float64(maxTime.Milliseconds()))
  330. }
  331. return playwright.Float(float64(d.Milliseconds()))
  332. }
  333. func saveScreenshot(t *testing.T, page playwright.Page) {
  334. t.Helper()
  335. data, err := page.Screenshot()
  336. if err != nil {
  337. t.Logf("could not take screenshot: %v", err)
  338. return
  339. }
  340. f, err := os.CreateTemp("", "anubis-test-fail-*.png")
  341. if err != nil {
  342. t.Logf("could not create temporary file: %v", err)
  343. return
  344. }
  345. defer f.Close()
  346. _, err = f.Write(data)
  347. if err != nil {
  348. t.Logf("could not write screenshot: %v", err)
  349. return
  350. }
  351. t.Logf("screenshot saved to %s", f.Name())
  352. }
  353. func setupPlaywright(t *testing.T) *playwright.Playwright {
  354. err := playwright.Install(&playwright.RunOptions{
  355. SkipInstallBrowsers: true,
  356. })
  357. if err != nil {
  358. t.Fatalf("could not install Playwright: %v", err)
  359. }
  360. pw, err := playwright.Run()
  361. if err != nil {
  362. t.Fatalf("could not start Playwright: %v", err)
  363. }
  364. return pw
  365. }
  366. func spawnAnubis(t *testing.T) string {
  367. t.Helper()
  368. h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  369. w.Header().Add("Content-Type", "text/html")
  370. fmt.Fprintf(w, "<html><body><span id=anubis-test>%d</span></body></html>", time.Now().Unix())
  371. })
  372. policy, err := libanubis.LoadPoliciesOrDefault("", anubis.DefaultDifficulty)
  373. if err != nil {
  374. t.Fatal(err)
  375. }
  376. listener, err := net.Listen("tcp", ":0")
  377. if err != nil {
  378. t.Fatalf("can't listen on random port: %v", err)
  379. }
  380. addr := listener.Addr().(*net.TCPAddr)
  381. host := "localhost"
  382. port := strconv.Itoa(addr.Port)
  383. s, err := libanubis.New(libanubis.Options{
  384. Next: h,
  385. Policy: policy,
  386. ServeRobotsTXT: true,
  387. Target: "http://" + host + ":" + port,
  388. })
  389. if err != nil {
  390. t.Fatalf("can't construct libanubis.Server: %v", err)
  391. }
  392. ts := &httptest.Server{
  393. Listener: listener,
  394. Config: &http.Server{Handler: s},
  395. }
  396. ts.Start()
  397. t.Log(ts.URL)
  398. t.Cleanup(func() {
  399. ts.Close()
  400. })
  401. return ts.URL
  402. }