Skip to content

fix(security): delete dead code and harden MIME, attachment paths, OAuth, and token storage - #19

Merged
willtmc merged 1 commit into
mainfrom
fix/security-mime-oauth-storage
Nov 25, 2026
Merged

willtmc merged 1 commit into
mainfrom
fix/security-mime-oauth-storage

Conversation

@willtmc

@willtmc willtmc commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

Removes dead code and hardens outbound MIME construction, inbound attachment downloads, local OAuth callbacks, and account/token persistence.

Changes

  1. Dead code removal

    • Deleted unused/broken GmailService.updateDraft() (src/gmail-service.ts).
    • Removed unused accessToken?: string from EmailAccount.oauth2 (src/types.ts) and its setCredentials reference.
    • Removed legacy .mailcp/ entry from .gitignore.
  2. Deduplicated & hardened MIME builder (src/gmail-service.ts)

    • Extracted exported buildRawMimeMessage(...) and validateAttachmentPaths(...) shared by createDraft and sendMessage.
    • Blocked CRLF header injection across From, To, Cc, Bcc, Subject, In-Reply-To, References, and attachment filenames.
    • Folded encodeMimeHeader UTF-8 Base64 encoded-words to <= 75 chars per token on clean UTF-8 code-point boundaries (no split multi-byte characters or emoji).
    • Added Content-Transfer-Encoding: base64 with 76-char \r\n wrapping for UTF-8 text/HTML bodies and attachment payloads.
    • Sanitized Content-Disposition attachment filenames (strip CR/LF/NUL//, escape \ and ") and switched MIME boundaries to crypto.randomBytes(16).toString("hex").
    • Preflight-validated --attach paths (exists, regular file, total bytes <= 25 MB) before making Gmail API calls.
  3. Inbound attachment path traversal fix & deduplication (src/gmail-service.ts)

    • Extracted shared downloadPartsForMessages(...) used by both getThread(..., true) and downloadMessageAttachments(...).
    • Sanitized inbound filenames (\ → /, path.posix.basename, strip control chars & leading dots, fallback to "attachment") and enforced path.resolve(filePath).startsWith(path.resolve(attachmentDir) + path.sep).
    • Enforced 0700 permissions on the attachments directory (including pre-existing directories) and 0600 on downloaded files.
  4. Hardened account & token storage (src/account-storage.ts)

    • Enforced 0700 on the config directory (new and existing).
    • Switched accounts.json and credentials.json saves to atomic same-directory temp-file writes (0600 + chmodSync + renameSync).
    • Returned empty accounts only on ENOENT; invalid JSON, non-array payloads, or read errors now throw an actionable error instead of silently returning [].
  5. Hardened OAuth flow (src/gmail-oauth-flow.ts)

    • Added cryptographically random CSRF state (crypto.randomBytes(24).toString("hex")) and prompt: "consent", strictly verified with crypto.timingSafeEqual before token exchange.
    • Escaped HTML entities (escapeHtml) in localhost callback error responses to prevent reflected HTML injection.
    • Replaced deprecated url.parse() with new URL(...) and removed access to private oauth2Client._clientId / _clientSecret.

Verification

  • npm ci --no-audit --no-fund
  • npm run build
  • node --test test/*.test.js — 39/39 passing (including new test/security-mime-storage.test.js)
  • npx biome check src/ — clean (7 files checked)
  • npx tsc --noEmit — clean

@willtmc
willtmc merged commit c45e145 into main Nov 25, 2026
1 check passed
@willtmc
willtmc deleted the fix/security-mime-oauth-storage branch November 25, 2026 15:02
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