IMimeComplianceLogger Interface

An interface for recording MIME compliance violations.

Definition

Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public interface IMimeComplianceLogger

Remarks

Implementations of this interface are intended to capture and record information about MIME compliance issues detected during parsing. This can be used for diagnostics, auditing, or reporting purposes in systems that process MIME data.

No limit is placed by default on how many issues a single message, or even a single header, may produce. A malformed message is not obliged to be malformed in only a few places, so the number of issues is bounded only by the size of the input. When the messages being parsed are untrusted, set MaxIssuesPerViolation to bound the report. An implementation that retains issues rather than summarizing them should impose its own limit as well, since it cannot assume that every caller configures one.

Example

C#
                    // IMimeComplianceLogger receives a MimeComplianceIssue for each violation of the MIME
// specifications that the MimeReader (or MimeParser) encounters while parsing.
class ComplianceReport : IMimeComplianceLogger
{
    public readonly List<MimeComplianceIssue> Issues = new List<MimeComplianceIssue> ();

    public void Log (in MimeComplianceIssue issue)
    {
        Issues.Add (issue);
    }
}

public static MimeMessage ParseAndReportCompliance (string fileName)
{
    using (var stream = File.OpenRead (fileName)) {
        var report = new ComplianceReport ();
        var parser = new MimeParser (stream, MimeFormat.Entity) {
            ComplianceLogger = report,

            // Messages from a local message store (such as an mbox or Maildir) are not
            // subject to the transport requirements (such as line-length limits) and so
            // such violations are rated with a lower severity.
            ComplianceContext = MimeComplianceContext.Storage,

            // When parsing untrusted messages, limit the number of times that each violation
            // may be reported to avoid unbounded memory usage.
            MaxComplianceIssuesPerViolation = 100
        };

        var message = parser.ParseMessage ();

        foreach (var issue in report.Issues) {
            // Each issue includes the location in the stream where the violation was found,
            // how severe it is, and a description of the problem.
            Console.WriteLine ("[{0}] line {1}, column {2}: {3}", issue.Severity, issue.LineNumber, issue.ColumnNumber, issue.Description);

            // The Categories property describes the kinds of risk that the violation poses.
            if ((issue.Categories & MimeComplianceCategories.Security) != 0)
                Console.WriteLine ("    This violation may have security implications: {0}", issue.Remarks);
        }

        return message;
    }
}
                  

Methods

Log Log a MIME compliance violation.

See Also

Reference