playwright_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Integration tests for Anubis, using Playwright.
  2. //
  3. // These tests require an already running Anubis and Playwright server.
  4. //
  5. // Anubis must be configured to redirect to the server started by the test suite.
  6. // The bind address and the Anubis server can be specified using the flags `-bind` and `-anubis` respectively.
  7. //
  8. // Playwright must be started in server mode using `npx playwright@1.50.1 run-server --port 3000`.
  9. // The version must match the minor used by the playwright-go package.
  10. //
  11. // On unsupported systems you may be able to use a container instead: https://playwright.dev/docs/docker#remote-connection
  12. //
  13. // 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.
  14. package test
  15. import (
  16. "flag"
  17. "fmt"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/url"
  21. "os"
  22. "os/exec"
  23. "testing"
  24. "time"
  25. "github.com/TecharoHQ/anubis"
  26. libanubis "github.com/TecharoHQ/anubis/lib"
  27. "github.com/playwright-community/playwright-go"
  28. )
  29. var (
  30. playwrightPort = flag.Int("playwright-port", 9001, "Playwright port")
  31. playwrightServer = flag.String("playwright", "ws://localhost:9001", "Playwright server URL")
  32. playwrightMaxTime = flag.Duration("playwright-max-time", 5*time.Second, "maximum time for Playwright requests")
  33. playwrightMaxHardTime = flag.Duration("playwright-max-hard-time", 5*time.Minute, "maximum time for hard Playwright requests")
  34. testCases = []testCase{
  35. {
  36. name: "firefox",
  37. action: actionChallenge,
  38. realIP: placeholderIP,
  39. userAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0",
  40. },
  41. {
  42. name: "headlessChrome",
  43. action: actionDeny,
  44. realIP: placeholderIP,
  45. userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/120.0.6099.28 Safari/537.36",
  46. },
  47. {
  48. name: "kagiBadIP",
  49. action: actionChallenge,
  50. isHard: true,
  51. realIP: placeholderIP,
  52. userAgent: "Mozilla/5.0 (compatible; Kagibot/1.0; +https://kagi.com/bot)",
  53. },
  54. {
  55. name: "kagiGoodIP",
  56. action: actionAllow,
  57. realIP: "216.18.205.234",
  58. userAgent: "Mozilla/5.0 (compatible; Kagibot/1.0; +https://kagi.com/bot)",
  59. },
  60. {
  61. name: "unknownAgent",
  62. action: actionAllow,
  63. realIP: placeholderIP,
  64. userAgent: "AnubisTest/0",
  65. },
  66. }
  67. )
  68. const (
  69. actionAllow action = "ALLOW"
  70. actionDeny action = "DENY"
  71. actionChallenge action = "CHALLENGE"
  72. placeholderIP = "fd11:5ee:bad:c0de::"
  73. playwrightVersion = "1.50.1"
  74. )
  75. type action string
  76. type testCase struct {
  77. name string
  78. action action
  79. isHard bool
  80. realIP, userAgent string
  81. }
  82. func doesNPXExist(t *testing.T) {
  83. t.Helper()
  84. if _, err := exec.LookPath("npx"); err != nil {
  85. t.Skipf("npx not found in PATH, skipping integration smoke testing: %v", err)
  86. }
  87. }
  88. func run(t *testing.T, command string) string {
  89. t.Helper()
  90. shPath, err := exec.LookPath("sh")
  91. if err != nil {
  92. t.Fatalf("[unexpected] %v", err)
  93. }
  94. t.Logf("running command: %s", command)
  95. cmd := exec.Command(shPath, "-c", command)
  96. cmd.Stdin = nil
  97. cmd.Stderr = os.Stderr
  98. output, err := cmd.Output()
  99. if err != nil {
  100. t.Fatalf("can't run command: %v", err)
  101. }
  102. return string(output)
  103. }
  104. func daemonize(t *testing.T, command string) {
  105. t.Helper()
  106. shPath, err := exec.LookPath("sh")
  107. if err != nil {
  108. t.Fatalf("[unexpected] %v", err)
  109. }
  110. t.Logf("daemonizing command: %s", command)
  111. cmd := exec.Command(shPath, "-c", command)
  112. cmd.Stdin = nil
  113. cmd.Stderr = os.Stderr
  114. cmd.Stdout = os.Stdout
  115. if err := cmd.Start(); err != nil {
  116. t.Fatalf("can't daemonize command: %v", err)
  117. }
  118. t.Cleanup(func() {
  119. cmd.Process.Kill()
  120. })
  121. }
  122. func startPlaywright(t *testing.T) {
  123. t.Helper()
  124. if os.Getenv("CI") == "true" {
  125. run(t, fmt.Sprintf("npx --yes playwright@%s install --with-deps", playwrightVersion))
  126. } else {
  127. run(t, fmt.Sprintf("npx --yes playwright@%s install", playwrightVersion))
  128. }
  129. daemonize(t, fmt.Sprintf("npx --yes playwright@%s run-server --port %d", playwrightVersion, *playwrightPort))
  130. for {
  131. if _, err := http.Get(fmt.Sprintf("http://localhost:%d", *playwrightPort)); err != nil {
  132. time.Sleep(500 * time.Millisecond)
  133. continue
  134. }
  135. break
  136. }
  137. //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.
  138. time.Sleep(2 * time.Second)
  139. }
  140. func TestPlaywrightBrowser(t *testing.T) {
  141. if os.Getenv("DONT_USE_NETWORK") != "" {
  142. t.Skip("test requires network egress")
  143. return
  144. }
  145. doesNPXExist(t)
  146. startPlaywright(t)
  147. pw := setupPlaywright(t)
  148. anubisURL := spawnAnubis(t)
  149. browsers := []playwright.BrowserType{pw.Chromium, pw.Firefox, pw.WebKit}
  150. for _, typ := range browsers {
  151. t.Run(typ.Name()+"/warmup", func(t *testing.T) {
  152. browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
  153. ExposeNetwork: playwright.String("<loopback>"),
  154. })
  155. if err != nil {
  156. t.Fatalf("could not connect to remote browser: %v", err)
  157. }
  158. defer browser.Close()
  159. ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
  160. AcceptDownloads: playwright.Bool(false),
  161. ExtraHttpHeaders: map[string]string{
  162. "X-Real-Ip": "127.0.0.1",
  163. },
  164. UserAgent: playwright.String("Sephiroth"),
  165. })
  166. if err != nil {
  167. t.Fatalf("could not create context: %v", err)
  168. }
  169. defer ctx.Close()
  170. page, err := ctx.NewPage()
  171. if err != nil {
  172. t.Fatalf("could not create page: %v", err)
  173. }
  174. defer page.Close()
  175. timeout := 2.0
  176. page.Goto(anubisURL, playwright.PageGotoOptions{
  177. Timeout: &timeout,
  178. })
  179. })
  180. for _, tc := range testCases {
  181. name := fmt.Sprintf("%s/%s", typ.Name(), tc.name)
  182. t.Run(name, func(t *testing.T) {
  183. _, hasDeadline := t.Deadline()
  184. if tc.isHard && hasDeadline {
  185. t.Skip("skipping hard challenge with deadline")
  186. }
  187. var perfomedAction action
  188. var err error
  189. for i := 0; i < 5; i++ {
  190. perfomedAction, err = executeTestCase(t, tc, typ, anubisURL)
  191. if perfomedAction == tc.action {
  192. break
  193. }
  194. time.Sleep(time.Duration(i+1) * 250 * time.Millisecond)
  195. }
  196. if perfomedAction != tc.action {
  197. t.Errorf("unexpected test result, expected %s, got %s", tc.action, perfomedAction)
  198. }
  199. if err != nil {
  200. t.Fatalf("test error: %v", err)
  201. }
  202. })
  203. }
  204. }
  205. }
  206. func buildBrowserConnect(name string) string {
  207. u, _ := url.Parse(*playwrightServer)
  208. q := u.Query()
  209. q.Set("browser", name)
  210. u.RawQuery = q.Encode()
  211. return u.String()
  212. }
  213. func executeTestCase(t *testing.T, tc testCase, typ playwright.BrowserType, anubisURL string) (action, error) {
  214. deadline, _ := t.Deadline()
  215. browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
  216. ExposeNetwork: playwright.String("<loopback>"),
  217. })
  218. if err != nil {
  219. return "", fmt.Errorf("could not connect to remote browser: %w", err)
  220. }
  221. defer browser.Close()
  222. ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
  223. AcceptDownloads: playwright.Bool(false),
  224. ExtraHttpHeaders: map[string]string{
  225. "X-Real-Ip": tc.realIP,
  226. },
  227. UserAgent: playwright.String(tc.userAgent),
  228. })
  229. if err != nil {
  230. return "", fmt.Errorf("could not create context: %w", err)
  231. }
  232. defer ctx.Close()
  233. page, err := ctx.NewPage()
  234. if err != nil {
  235. return "", fmt.Errorf("could not create page: %w", err)
  236. }
  237. defer page.Close()
  238. // Attempt challenge.
  239. start := time.Now()
  240. _, err = page.Goto(anubisURL, playwright.PageGotoOptions{
  241. Timeout: pwTimeout(tc, deadline),
  242. })
  243. if err != nil {
  244. return "", pwFail(t, page, "could not navigate to test server: %v", err)
  245. }
  246. hadChallenge := false
  247. switch tc.action {
  248. case actionChallenge:
  249. // FIXME: This could race if challenge is completed too quickly.
  250. checkImage(t, tc, deadline, page, "#image[src*=pensive], #image[src*=happy]")
  251. hadChallenge = true
  252. case actionDeny:
  253. checkImage(t, tc, deadline, page, "#image[src*=sad]")
  254. return actionDeny, nil
  255. }
  256. // Ensure protected resource was provided.
  257. res, err := page.Locator("#anubis-test").TextContent(playwright.LocatorTextContentOptions{
  258. Timeout: pwTimeout(tc, deadline),
  259. })
  260. end := time.Now()
  261. if err != nil {
  262. pwFail(t, page, "could not get text content: %v", err)
  263. }
  264. var tm int64
  265. if _, err := fmt.Sscanf(res, "%d", &tm); err != nil {
  266. pwFail(t, page, "unexpected output: %s", res)
  267. }
  268. if tm < start.Unix() || end.Unix() < tm {
  269. pwFail(t, page, "unexpected timestamp in output: %d not in range %d..%d", tm, start.Unix(), end.Unix())
  270. }
  271. if hadChallenge {
  272. return actionChallenge, nil
  273. } else {
  274. return actionAllow, nil
  275. }
  276. }
  277. func checkImage(t *testing.T, tc testCase, deadline time.Time, page playwright.Page, locator string) {
  278. image := page.Locator(locator)
  279. err := image.WaitFor(playwright.LocatorWaitForOptions{
  280. Timeout: pwTimeout(tc, deadline),
  281. })
  282. if err != nil {
  283. pwFail(t, page, "could not wait for result: %v", err)
  284. }
  285. failIsVisible, err := image.IsVisible()
  286. if err != nil {
  287. pwFail(t, page, "could not check result image: %v", err)
  288. }
  289. if !failIsVisible {
  290. pwFail(t, page, "expected result image not visible")
  291. }
  292. }
  293. func pwFail(t *testing.T, page playwright.Page, format string, args ...any) error {
  294. t.Helper()
  295. saveScreenshot(t, page)
  296. return fmt.Errorf(format, args...)
  297. }
  298. func pwTimeout(tc testCase, deadline time.Time) *float64 {
  299. max := *playwrightMaxTime
  300. if tc.isHard {
  301. max = *playwrightMaxHardTime
  302. }
  303. d := time.Until(deadline)
  304. if d <= 0 || d > max {
  305. return playwright.Float(float64(max.Milliseconds()))
  306. }
  307. return playwright.Float(float64(d.Milliseconds()))
  308. }
  309. func saveScreenshot(t *testing.T, page playwright.Page) {
  310. t.Helper()
  311. data, err := page.Screenshot()
  312. if err != nil {
  313. t.Logf("could not take screenshot: %v", err)
  314. return
  315. }
  316. f, err := os.CreateTemp("", "anubis-test-fail-*.png")
  317. if err != nil {
  318. t.Logf("could not create temporary file: %v", err)
  319. return
  320. }
  321. defer f.Close()
  322. _, err = f.Write(data)
  323. if err != nil {
  324. t.Logf("could not write screenshot: %v", err)
  325. return
  326. }
  327. t.Logf("screenshot saved to %s", f.Name())
  328. }
  329. func setupPlaywright(t *testing.T) *playwright.Playwright {
  330. err := playwright.Install(&playwright.RunOptions{
  331. SkipInstallBrowsers: true,
  332. })
  333. if err != nil {
  334. t.Fatalf("could not install Playwright: %v", err)
  335. }
  336. pw, err := playwright.Run()
  337. if err != nil {
  338. t.Fatalf("could not start Playwright: %v", err)
  339. }
  340. return pw
  341. }
  342. func spawnAnubis(t *testing.T) string {
  343. t.Helper()
  344. h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  345. w.Header().Add("Content-Type", "text/html")
  346. fmt.Fprintf(w, "<html><body><span id=anubis-test>%d</span></body></html>", time.Now().Unix())
  347. })
  348. policy, err := libanubis.LoadPoliciesOrDefault("", anubis.DefaultDifficulty)
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. s, err := libanubis.New(libanubis.Options{
  353. Next: h,
  354. Policy: policy,
  355. ServeRobotsTXT: true,
  356. })
  357. if err != nil {
  358. t.Fatalf("can't construct libanubis.Server: %v", err)
  359. }
  360. ts := httptest.NewServer(s)
  361. t.Log(ts.URL)
  362. t.Cleanup(func() {
  363. ts.Close()
  364. })
  365. return ts.URL
  366. }