Skip to content

feat: process Slack image attachments with security hardening#31

Open
electronicBlacksmith wants to merge 4 commits intoghostwright:mainfrom
electronicBlacksmith:feature/slack-image-attachments
Open

feat: process Slack image attachments with security hardening#31
electronicBlacksmith wants to merge 4 commits intoghostwright:mainfrom
electronicBlacksmith:feature/slack-image-attachments

Conversation

@electronicBlacksmith
Copy link
Copy Markdown

Summary

Slack messages with file attachments (subtype: file_share) were silently dropped by the blanket subtype filter. This PR adds image attachment processing with security hardening and user feedback for unsupported file types.

Image attachment processing

  • Download attached images (PNG, JPEG, GIF, WebP) from Slack to data/uploads/ and append file paths to the agent prompt so it can read them via its Read tool
  • Add files:read OAuth scope to the Slack app manifest
  • Handle both DM and app_mention events with file attachments
  • Clean up uploads older than 24 hours on connect
  • Support file-only messages (no text) with [User sent attached files] fallback

Security hardening

  • Path traversal prevention. sanitizeFilename() strips directory components (../, ..\\) and null bytes from Slack filenames, with defense-in-depth resolve().startsWith() check matching the pattern in ui/serve.ts:isPathSafe
  • SSRF prevention. File downloads restricted to files.slack.com and files-pri.slack.com hostnames only, blocking crafted url_private values targeting internal endpoints
  • Zod validation. All Slack file records validated through SlackFileSchema before processing, rejecting malformed external input at the boundary
  • Token access cleanup. Bot token stored as private field instead of fragile (this.app.client as unknown as Record<string, unknown>).token double-cast

User feedback for unsupported files

  • New SkippedFileInfo type with structured reasons (unsupported_type, too_large, download_failed) surfaced in the agent prompt so it can naturally inform users when their PDF, CSV, or other non-image attachment was skipped
  • InboundAttachment widened to discriminated union (image | document) for future file type support

Code extraction

  • File handling logic extracted from slack.ts into slack-files.ts (132 lines) as standalone functions, matching the existing slack-formatter.ts and slack-actions.ts patterns

Test plan

  • 182 channel tests pass, 0 failures (30 new tests in slack-files.test.ts)
  • Typecheck clean (tsc --noEmit)
  • Lint clean (biome check)
  • Path traversal: ../../../etc/passwd, backslash traversal, null bytes, empty string all sanitized
  • SSRF: cloud metadata endpoint (169.254.169.254), localhost, and arbitrary hosts blocked; files.slack.com and files-pri.slack.com allowed
  • Zod: missing fields, invalid URLs, negative sizes, non-object records all rejected
  • Skipped file feedback: PDF returns unsupported_type, oversized returns too_large, HTTP failure returns download_failed, mixed batch handled correctly
  • Verified on live Slack instance: PDF and CSV correctly reported as skipped, images downloaded and processed

Messages with file attachments (subtype: file_share) were silently
dropped by the blanket subtype filter. This downloads attached images
to data/uploads/ and appends file paths to the prompt so the agent
can read them via its Read tool. Also adds files:read to the Slack
app manifest.
The downloadSlackFiles() function used unsanitized Slack filenames
directly in join(), allowing crafted names with ../ to write outside
the uploads directory. Also, url_private was fetched without hostname
validation, enabling SSRF via crafted file records.

Fixes:
- Extract file handling into slack-files.ts with sanitizeFilename()
  that strips directory components and null bytes, plus defense-in-depth
  resolve().startsWith() check (matching isPathSafe in ui/serve.ts)
- Add SSRF host allowlist restricting downloads to files.slack.com and
  files-pri.slack.com
- Add Zod schema validation for Slack file records (external input)
- Store botToken as private field instead of fragile double-cast
  through this.app.client

30 new tests covering sanitization, Zod rejection paths, SSRF blocking,
download failures, and cleanup lifecycle.
When users attach non-image files (PDF, CSV, etc.) to Slack messages,
the files were silently dropped with no indication. Users had no way
to know their attachment was ignored.

- Add SkippedFileInfo type with structured reasons (unsupported_type,
  too_large, download_failed) and optional mimetype
- Widen InboundAttachment to discriminated union (image | document)
  for future file type support
- Wire skippedFiles through both app_mention and DM event handlers
- Append skipped file context to agent prompt so it can naturally
  inform the user about unsupported attachments
- Update slack.test.ts to verify skippedFiles on skip and failure
@mcheemaa mcheemaa self-requested a review April 2, 2026 03:09
Bun's mock.module replaces modules process-wide, causing other test
files (config, evolution, roles) to lose real fs functions like
writeFileSync and mkdirSync. This caused 114 test failures in CI.

Replace node:fs mocking with real temp directories for cleanup tests
and remove fs-dependent integration tests that are now covered by
the unit tests in slack-files.test.ts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant