Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Generic Playwright Target

This notebook demonstrates how to interact with the Playwright Target in PyRIT.

The PlaywrightTarget class allows you to interact with web applications using Playwright. This is useful for testing interactions with web applications, such as chatbots or other web interfaces, especially for red teaming purposes.

Example Setup

To run the Flask app, you also must download and run Ollama, making sure the flask is using a correct model. For example, ollama run llama3.2:1b runs the Llama 3.2 1B model.

Additionally, you need to install playwright by executing playwright install.

Example: Interacting with a Web Application using PlaywrightTarget

In this example, we’ll interact with a simple web application running locally at http://127.0.0.1:5000. which runs a chatbot that responds to user prompts via ollama We’ll define an interaction function that navigates to the web application, inputs a prompt, and retrieves the bot’s response.

Start the Flask App

Before we can interact with the web application, we need to start the Flask app that serves the chatbot, this will be done in a subprocess

Starting Flask app from /workspace/doc/code/targets/playwright_demo/app.py...
Flask app is running and ready!

The flask app should now be running locally:

image-2.png

Interaction Function

This is playwright script that interacts with the chatbot web application.

Using PlaywrightTarget with the Interaction Function and Scorer

Now, we can use the PlaywrightTarget by passing the interaction function we defined. We’ll use the PromptSendingAttack to send prompts to the target and collects responses.


────────────────────────────────────────────────────────────────────────────────────────────────────
🔹 Turn 1 - USER
────────────────────────────────────────────────────────────────────────────────────────────────────
  Tell me a joke about computer programming.

────────────────────────────────────────────────────────────────────────────────────────────────────
🔸 ASSISTANT
────────────────────────────────────────────────────────────────────────────────────────────────────
  Howdy there, partner! I've got one for ya that's fixin' to boot up your funny bone. Here goes:
  
    Why did the programmer quit his job?
  
    Because he didn't get arrays enough pay!
  
    I know, I know, it's a little corny, but I'm a cowboy who code, what can I say?

────────────────────────────────────────────────────────────────────────────────────────────────────

Terminate the Flask App

Flask app has been terminated.