validate browser session id when running tasks or workflows (#2874)
Some checks failed
Run tests and pre-commit / Frontend Lint and Build (push) Has been cancelled
Publish Fern Docs / run (push) Has been cancelled
Run tests and pre-commit / Run tests and pre-commit hooks (push) Has been cancelled

This commit is contained in:
Shuchang Zheng 2025-07-04 01:49:51 -07:00 committed by GitHub
parent 133cee4358
commit f832206f38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 0 deletions

View file

@ -715,3 +715,11 @@ class MissingBrowserSessionError(SkyvernHTTPException):
class MissingBrowserAddressError(SkyvernException): class MissingBrowserAddressError(SkyvernException):
def __init__(self, browser_session_id: str) -> None: def __init__(self, browser_session_id: str) -> None:
super().__init__(f"Browser session {browser_session_id} does not have an address.") super().__init__(f"Browser session {browser_session_id} does not have an address.")
class BrowserSessionNotFound(SkyvernHTTPException):
def __init__(self, browser_session_id: str) -> None:
super().__init__(
f"Browser session {browser_session_id} does not exist or is not live.",
status_code=status.HTTP_404_NOT_FOUND,
)

View file

@ -27,6 +27,7 @@ from skyvern.constants import (
ScrapeType, ScrapeType,
) )
from skyvern.exceptions import ( from skyvern.exceptions import (
BrowserSessionNotFound,
BrowserStateMissingPage, BrowserStateMissingPage,
DownloadFileMaxWaitingTime, DownloadFileMaxWaitingTime,
EmptyScrapePage, EmptyScrapePage,
@ -223,6 +224,15 @@ class ForgeAgent:
async def create_task(self, task_request: TaskRequest, organization_id: str) -> Task: async def create_task(self, task_request: TaskRequest, organization_id: str) -> Task:
webhook_callback_url = str(task_request.webhook_callback_url) if task_request.webhook_callback_url else None webhook_callback_url = str(task_request.webhook_callback_url) if task_request.webhook_callback_url else None
totp_verification_url = str(task_request.totp_verification_url) if task_request.totp_verification_url else None totp_verification_url = str(task_request.totp_verification_url) if task_request.totp_verification_url else None
# validate browser session id
if task_request.browser_session_id:
browser_session = await app.DATABASE.get_persistent_browser_session(
session_id=task_request.browser_session_id,
organization_id=organization_id,
)
if not browser_session:
raise BrowserSessionNotFound(browser_session_id=task_request.browser_session_id)
task = await app.DATABASE.create_task( task = await app.DATABASE.create_task(
url=str(task_request.url), url=str(task_request.url),
title=task_request.title, title=task_request.title,

View file

@ -10,6 +10,7 @@ from skyvern import analytics
from skyvern.config import settings from skyvern.config import settings
from skyvern.constants import GET_DOWNLOADED_FILES_TIMEOUT, SAVE_DOWNLOADED_FILES_TIMEOUT from skyvern.constants import GET_DOWNLOADED_FILES_TIMEOUT, SAVE_DOWNLOADED_FILES_TIMEOUT
from skyvern.exceptions import ( from skyvern.exceptions import (
BrowserSessionNotFound,
FailedToSendWebhook, FailedToSendWebhook,
InvalidCredentialId, InvalidCredentialId,
MissingValueForParameter, MissingValueForParameter,
@ -779,6 +780,15 @@ class WorkflowService:
organization_id: str, organization_id: str,
parent_workflow_run_id: str | None = None, parent_workflow_run_id: str | None = None,
) -> WorkflowRun: ) -> WorkflowRun:
# validate the browser session id
if workflow_request.browser_session_id:
browser_session = await app.DATABASE.get_persistent_browser_session(
session_id=workflow_request.browser_session_id,
organization_id=organization_id,
)
if not browser_session:
raise BrowserSessionNotFound(browser_session_id=workflow_request.browser_session_id)
return await app.DATABASE.create_workflow_run( return await app.DATABASE.create_workflow_run(
workflow_permanent_id=workflow_permanent_id, workflow_permanent_id=workflow_permanent_id,
workflow_id=workflow_id, workflow_id=workflow_id,