Skip to content

TECH: SslScanner Improvement — Fix IMAP STARTTLS protocol in Test_SSL_Support #8

Open

Description

@EvoJocys

Implements WI-1 from .ai/work-item-improvement.md.

Score: Impact 4 × Confidence 5 ÷ Effort 1 = 20 (highest in current backlog).

Why now

Tool/Common/Test_SSL_Support.cs lines 277-290 ("IMAP" branch, port == 143) is a near-verbatim copy of the POP3 branch. It checks the IMAP greeting against +OK (POP3 format) instead of * OK (RFC 3501 untagged OK), and sends STLS instead of the tagged IMAP STARTTLS command (RFC 2595).

Result: every IMAP STARTTLS scan currently throws InvalidOperationException ("IMAP Server did not respond to connection request"). The advertised IMAP STARTTLS support listed in README.md does not work.

v0 scope

User can run an IMAP STARTTLS scan on port 143 and receive certificate details instead of an exception.

Implementation outline

In the if (port == 143) block of TestStarTLS:

  1. Change the greeting check from connectResponse.StartsWith("+OK") to connectResponse.StartsWith("* OK") (RFC 3501 untagged OK).
  2. Replace clearTextWriter.WriteLine("STLS") with the tagged IMAP command clearTextWriter.WriteLine("a001 STARTTLS") (RFC 2595).
  3. Replace startTlsResponse.StartsWith("+OK") with a tag-tolerant check for OK in the response (e.g. startTlsResponse.IndexOf(" OK", StringComparison.OrdinalIgnoreCase) >= 0); the IMAP server replies a001 OK Begin TLS.
  4. Update the two exception messages to refer to IMAP / STARTTLS correctly (currently say STLS).
  5. Bump the patch version in Tool/JocysCom.SslScanner.Tool.csproj and add a ChangeLog.txt line:
    - Fix: IMAP STARTTLS protocol — use '* OK' greeting and tagged 'STARTTLS' command (RFC 3501/2595).
  6. Verify against a real IMAP server (e.g. an Office 365 or Dovecot host on port 143) — handshake should succeed and certificate fields populate.

Symbiosis

None blocked directly, but a green IMAP path makes the related [Open] items in the registry safer to land on top:

  • WI-2 Populate DataItem.SAN — affects every protocol; easier to verify once IMAP scans actually return a cert.
  • WI-3 Honour ScriptExecutorParam.Cancel — independent.

AI agent: load and follow the work-item-improvement skill.

Activity

  1. EvoJocys commented on May 26, 2026

    @EvoJocys
    MemberAuthor

    [AI Review] · Story Points: 2 · Difficulty: None (0) · Impact: Very High (4) · AI Work: 90% · Human Work: 10%

    Review Summary

    Skill Matrix: C#, .NET 8, IMAP protocol (RFC 3501 / RFC 2595), SSL/TLS / SslStream, TCP networking

    Story Points (reasons):
    - Work type: Bug fix — baseline 1–5; root cause is fully confirmed and the exact fix strings are prescribed verbatim in the description.
    - Calibration match: All changes are ≤10 mechanical string replacements across one method, a one-line version bump, and a changelog prepend — matches the '2' anchor (few hours, straightforward bug fix).
    - Codebase context: Prior vector-store analysis confirmed the defect is isolated to the `if (port == 143)` block in `Test_SSL_Support.cs`; no other files require logic changes.
    - No adjustment — AC specifies no staged rollout, no cross-team coordination, and no performance/security metrics; manual IMAP server verification is the only human step and is already factored into the baseline.

    Split recommendation: No

    Notes: Qdrant searches fully confirmed the defect. The live source of `Tool/Common/Test_SSL_Support.cs` (lines 241–354) retrieved from the vector store shows the exact buggy `if (port == 143)` block verbatim: greeting check uses `+OK` (POP3 format) and the STARTTLS command is `STLS` (POP3 command), both incorrect per RFC 3501/2595. The POP3 branch (`if (port == 110)`) immediately above is structurally correct for POP3; the IMAP block is a near-verbatim copy of it. Every IMAP STARTTLS scan unconditionally throws `InvalidOperationException("IMAP Server did not respond to connection request")` because RFC 3501 servers always greet with `* OK`, never `+OK`.

    Impact is 4 (Very High): the advertised IMAP STARTTLS feature (documented in README.md) is completely non-functional, constituting a 100% regression on a published, user-visible capability.

    Fix surface is ≤10 changed lines across three files:
    - `Tool/Common/Test_SSL_Support.cs` — 3 logic corrections + 2 exception message string updates inside the `if (port == 143)` block.
    - `Tool/JocysCom.SslScanner.Tool.csproj` — `1.1.6` → `1.1.7`.
    - `Tool/Documents/ChangeLog.txt` — prepend a dated v1.1.7 entry.

    No automated test projects exist in the repository (confirmed by `.ai/repository-analysis.instructions.md`); no CI pipeline is present. Manual verification against a live IMAP server on port 143 is the only available validation path and represents the sole human-effort item.

    Symbiosis context: WI-2 (`DataItem.SAN` never populated — `ScriptExecutor` never reads any SAN-extraction code; `SanColumn` in `DataListControl.xaml` is bound but always blank) and WI-3 (`ScriptExecutorParam.Cancel` declared but never read in the scan loop) are independent but easier to verify once IMAP scans return real certificates.

    The story was subsequently shipped as PR (branch `feat/wi-8-tech-sslscanner-improvement-fix-imap-sta`, commit `71c0e6c`), and the WI-1 registry entry was flipped to [Done]. No clarifying questions are needed: the description supplies exact RFC citations, specific string replacements, the target version, and the changelog format. The AcceptanceCriteria field is empty but the implementation outline in the description is fully unambiguous.

    Clarifying Questions from Developer:
    None

    Proposed Solution: **1. Edit `Tool/Common/Test_SSL_Support.cs` — `if (port == 143)` block inside `TestStarTLS`:**
    - Change `connectResponse.StartsWith("+OK")` → `connectResponse.StartsWith("* OK")` (RFC 3501 untagged OK).
    - Change `clearTextWriter.WriteLine("STLS")` → `clearTextWriter.WriteLine("a001 STARTTLS")` (RFC 2595 tagged command).
    - Change `startTlsResponse.StartsWith("+OK")` → `startTlsResponse.IndexOf(" OK", StringComparison.OrdinalIgnoreCase) >= 0` (tag-tolerant; server replies `a001 OK Begin TLS`).
    - Update both exception messages to reference `IMAP … STARTTLS` instead of `IMAP … STLS`.

    **2. Bump version in `Tool/JocysCom.SslScanner.Tool.csproj`:**
    - `1.1.6` → `1.1.7`.

    **3. Prepend to `Tool/Documents/ChangeLog.txt`:**
    ```
    2025-XX-XX v1.1.7
    - Fix: IMAP STARTTLS protocol — use '* OK' greeting and tagged 'STARTTLS' command (RFC 3501/2595).
    ```

    **4. Manual verification (human step):**
    Run the tool (or `Tool/Common/Test_SSL_Support.bat`) against a live IMAP server on port 143 (e.g., `imap.gmail.com 143`). Confirm the scan completes without exception and certificate fields (CN, ValidFrom, ValidTo, Algorithm) are populated in the UI grid.

    Related Items:
    Tool/Common/Test_SSL_Support.cs (lines 241–290)
    Tool/Common/Test_SSL_Support.cs (lines 1–240)
    .ai/work-item-improvement.md
    Tool/JocysCom.SslScanner.Tool.csproj
    Tool/Documents/ChangeLog.txt
    README.md
    Tool/Common/ScriptExecutor.cs
    Tool/Common/DataItem.cs
    Tool/Common/ScriptExecutorParam.cs
    Tool/Controls/DataListControl.xaml (lines 236–444)
    Tool/Common/Test_SSL_Support.bat
    JocysCom.SslScanner.slnx

    Solver Agent Prompt: Use to instruct other AI agents (e.g., CoPilot, CLINE...) to implement the solution.
    **Work Item to Address:**
    

    Title: TECH: SslScanner Improvement — Fix IMAP STARTTLS protocol in Test_SSL_Support

    Description:
    The IMAP branch (if (port == 143)) inside the static TestStarTLS method in Tool/Common/Test_SSL_Support.cs is a near-verbatim copy of the POP3 branch. It uses the wrong wire format for IMAP:

    • It checks the server greeting against +OK (POP3 format) instead of * OK (RFC 3501 untagged OK).
    • It sends STLS (POP3 command) instead of the tagged IMAP command a001 STARTTLS (RFC 2595).
    • Its STARTTLS response check also uses +OK instead of a tag-tolerant OK check (IMAP servers reply a001 OK Begin TLS).
    • Both exception messages incorrectly reference STLS rather than STARTTLS.

    Consequence: every IMAP STARTTLS scan currently throws InvalidOperationException("IMAP Server did not respond to connection request"). The IMAP STARTTLS feature advertised in README.md is completely non-functional.

    The buggy code (from Tool/Common/Test_SSL_Support.cs):

    // IMAP: https://tools.ietf.org/html/rfc2595
    if (port == 143)
    {
        var connectResponse = clearTextReader.ReadLine();
        if (!connectResponse.StartsWith("+OK"))  // BUG: should be "* OK" (RFC 3501)
            throw new InvalidOperationException("IMAP Server did not respond to connection request");
        // STARTTLS
        clearTextWriter.WriteLine("STLS");  // BUG: should be "a001 STARTTLS" (RFC 2595)
        var startTlsResponse = clearTextReader.ReadLine();
        if (!startTlsResponse.StartsWith("+OK"))  // BUG: should check for " OK" anywhere
            throw new InvalidOperationException("IMAP Server did not respond to STLS request");
        sslStream.AuthenticateAsClient(host, null, protocol, false);
        success = true;
        result.UpdateFromSslStream(sslStream);
    }

    The correct POP3 branch (if (port == 110)) immediately above this block is correct for POP3 and serves as a structural reference.

    Acceptance Criteria (derived from implementation outline):

    1. connectResponse.StartsWith("+OK") in the IMAP block → connectResponse.StartsWith("* OK").
    2. clearTextWriter.WriteLine("STLS") → clearTextWriter.WriteLine("a001 STARTTLS").
    3. startTlsResponse.StartsWith("+OK") → startTlsResponse.IndexOf(" OK", StringComparison.OrdinalIgnoreCase) >= 0.
    4. Both exception messages updated to reference IMAP/STARTTLS rather than STLS.
    5. <Version> in Tool/JocysCom.SslScanner.Tool.csproj bumped from 1.1.6 to 1.1.7.
    6. A new entry prepended to Tool/Documents/ChangeLog.txt:
      - Fix: IMAP STARTTLS protocol — use '* OK' greeting and tagged 'STARTTLS' command (RFC 3501/2595).
    7. Running the scanner against a real IMAP server on port 143 completes without exception and returns certificate details.

    Key Comments:

    • A prior AI review confirmed story points of 2, impact Very High (4), AI work ~90%, human work ~10%.
    • The fix was shipped on 2026-05-26 via branch feat/wi-8-tech-sslscanner-improvement-fix-imap-sta, commit 71c0e6c, affecting files: .ai/work-item-improvement.md, Tool/Common/Test_SSL_Support.cs, Tool/Documents/ChangeLog.txt, Tool/JocysCom.SslScanner.Tool.csproj.

    Potentially Relevant Files/Artifacts:

    • Tool/Common/Test_SSL_Support.cs — Primary target; contains the exact defect in the if (port == 143) block (lines 277–290).
    • .ai/work-item-improvement.md — Backlog registry confirming WI-1 scope and evidence pointer.
    • Tool/JocysCom.SslScanner.Tool.csproj — Version bump required (1.1.6 → 1.1.7).
    • Tool/Documents/ChangeLog.txt — Changelog entry to prepend; current latest entry is 2025-05-08 v1.1.6.
    • README.md — Advertises IMAP STARTTLS support (motivation context); no README change required for this fix.
    • Tool/Common/ScriptExecutor.cs — Downstream consumer of Test_SSL_Support results; no changes needed for this fix.
    • Tool/Common/DataItem.cs — Data model; CN, ValidFrom, ValidTo, Algorithm, PublicKeyData, SecurityProtocols will populate after fix.
    • Tool/Common/Test_SSL_Support.bat — Batch runner for manual verification against a live IMAP server.
    • JocysCom.SslScanner.slnx — Single-project solution; all changes scoped to one project.

    Please analyze this work item and the provided context. Devise and implement the fix. Your working directory will be the root of the repository. Note: there are no automated test projects in this repository, so manual verification against a live IMAP server on port 143 is the expected validation path.

  2. EvoJocys commented on May 26, 2026

    @EvoJocys
    MemberAuthor

    Shipped as PR #10 (branch feat/wi-8-tech-sslscanner-improvement-fix-imap-sta). Registry row WI-1 flipped [Working] -> [Done].

  3. EvoJocys commented on May 26, 2026

    @EvoJocys
    MemberAuthor

    [AI Coded] · WI 8 · 71c0e6c

    AI Coded
    Work item: #8 — TECH: SslScanner Improvement — Fix IMAP STARTTLS protocol in Test_SSL_Support
    Pull request: PR #10
    Branch: feat/wi-8-tech-sslscanner-improvement-fix-imap-sta
    Commit: 71c0e6c
    Files changed: .ai/work-item-improvement.md, Tool/Common/Test_SSL_Support.cs, Tool/Documents/ChangeLog.txt, Tool/JocysCom.SslScanner.Tool.csproj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions