Skip to content

Comments

feat(integrations): Gmail UDFs for email automation#1777

Merged
topher-lo merged 5 commits intoTracecatHQ:mainfrom
generalplantain:feat/gmail-udfs
Dec 22, 2025
Merged

feat(integrations): Gmail UDFs for email automation#1777
topher-lo merged 5 commits intoTracecatHQ:mainfrom
generalplantain:feat/gmail-udfs

Conversation

@generalplantain
Copy link
Contributor

@generalplantain generalplantain commented Dec 8, 2025

Checklist

  • Read CONTRIBUTING.md.
  • PR title is short and non-generic (see previously merged PRs for examples).
  • PR only implements a single feature or fixes a single bug.
  • Tests passing (uv run pytest tests)?
  • Lint / pre-commits passing (pre-commit run --all-files)?

Description

This PR adds Gmail UDFs (User Defined Functions) for email security automation workflows.

Functions Included

  • search_messages - Search Gmail with query syntax
  • get_message - Get full message content
  • get_message_headers - Get headers only (faster)
  • list_labels - List Gmail labels
  • get_thread - Get full email thread
  • get_connected_account - Show connected Gmail account

Features

  • OAuth Integration: Uses Tracecat's built-in OAuth system (google_gmail provider)
  • Security-focused: Designed for phishing investigations and threat hunting
  • Query Syntax: Full Gmail search syntax support
  • Flexible: Read-only by default, optional modify permissions

Use Cases

  • Phishing investigations: Search for suspicious emails by sender, subject, or content
  • Email analysis: Extract headers, attachments, and metadata
  • Automated response: Monitor inbox for security alerts
  • Threat hunting: Search across email history with powerful queries

Related Issues

Companion PR for Gmail OAuth provider: #[PR_NUMBER_FROM_PR1]

Screenshots / Recordings

The Gmail UDFs will appear in the workflow builder under the "Gmail" display group. Users can:

  1. Search emails with Gmail query syntax
  2. Retrieve full messages or just headers
  3. Access email threads for investigation
  4. List available labels

Steps to QA

Prerequisites

  • Gmail OAuth provider must be configured (PR #[NUMBER])
  • User must have connected their Gmail account via OAuth

Testing

  1. Create a new workflow in Tracecat
  2. Add a Gmail action (e.g., "Search Gmail Messages")
  3. Configure the search query (e.g., from:test@example.com)
  4. Run the workflow
  5. Verify email results are returned correctly

Example Queries

  • from:suspicious@evil.com has:attachment - Find emails with attachments from sender
  • subject:urgent after:2024/01/01 - Find urgent emails after date
  • is:unread label:inbox - Find unread emails in inbox
  • has:attachment filename:pdf - Find emails with PDF attachments

Note: Requires companion OAuth provider PR to be merged first for full functionality.


Summary by cubic

Adds Gmail UDFs for security automation. Workflows can search emails, fetch full messages or headers, get threads, list labels, and download attachments using the Tracecat Gmail OAuth.

  • New Features

    • Search messages with Gmail query syntax (search_messages)
    • Get full message content, headers, and body (get_message)
    • Get headers only for faster metadata retrieval (get_message_headers)
    • List user labels (list_labels)
    • Get all messages in a thread (get_thread)
    • Download attachments by message and attachment ID (get_attachment)
  • Migration

    • Configure the google_gmail OAuth provider and connect a Gmail account.

Written for commit db7608f. Summary will update automatically on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (all 1 issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="packages/tracecat-registry/tracecat_registry/integrations/gmail.py">

<violation number="1" location="packages/tracecat-registry/tracecat_registry/integrations/gmail.py:85">
P2: Silent failure when fetching individual message metadata. Non-200 responses are silently ignored, which could lead to confusing partial results without any indication that some messages failed to fetch. Consider logging failed fetches or including error info in the response.</violation>
</file>

Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR

results = []
for msg in messages:
msg_id = msg['id']
msg_response = await client.get(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally prefer to be a very thin wrapper around the API themselves (to avoid losing context). Can we just return the original data as opposed to change the schema?

params={"format": "full"},
)
response.raise_for_status()
msg_data = response.json()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above? Can we just return the msg_data directly?

params={"format": "metadata"},
)
response.raise_for_status()
msg_data = response.json()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above suggestion

messages = []
for msg in thread_data.get("messages", []):
headers = {h["name"]: h["value"] for h in msg.get("payload", {}).get("headers", [])}
messages.append({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

@topher-lo topher-lo added the integrations Pre-built actions label Dec 21, 2025
@topher-lo topher-lo changed the title feat: add Gmail UDFs for email security automation feat(integrations): add Gmail UDFs for email security automation Dec 21, 2025
- Add docstring for OAuth secret with setup instructions
- Return full API response instead of manipulating schema
- Remove all custom response formats - just return response.json()
- Add configurable 'format' parameter for message/thread requests
- Add 'metadata_headers' parameter for header filtering
- Add page_token support for search pagination
- Add get_attachment function for downloading attachments
@topher-lo topher-lo changed the title feat(integrations): add Gmail UDFs for email security automation feat(integrations): Gmail UDFs for email automation Dec 22, 2025
Signed-off-by: Chris Lo <46541035+topher-lo@users.noreply.github.com>
@topher-lo topher-lo merged commit 4c3002c into TracecatHQ:main Dec 22, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrations Pre-built actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants