Skip to content

fix(privacy): read Italian certified email (PEC) whose attachment names are unquoted - #287

Open
6801318d8d wants to merge 1 commit into
TKasperczyk:mainfrom
6801318d8d:fix/pec-signed-unquoted-names
Open

6801318d8d wants to merge 1 commit into
TKasperczyk:mainfrom
6801318d8d:fix/pec-signed-unquoted-names

Conversation

@6801318d8d

Copy link
Copy Markdown
Contributor

Change

With "Allow MCP clients to read encrypted messages" off, getMessage withholds every message delivered through Italian certified email (PEC, posta elettronica certificata), both the body and rawSource:

[Message content withheld: encryption status could not be determined. Enable encrypted-message access or open it in Thunderbird.]

These messages are signed, not encrypted. The PEC provider wraps the original message in an S/MIME-signed envelope:

multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256
  multipart/mixed
    multipart/alternative (text/plain, text/html)    provider notice
    message/rfc822; name=postacert.eml               the original message
    application/xml; name=daticert.xml               delivery data
  application/pkcs7-signature; name=smime.p7s

Many senders behind PEC (JavaMail-based protocol systems of public bodies) write attachment names with spaces and parentheses unquoted:

Content-Type: application/pdf; name=Signed decision (1).pdf

Cause. The strict raw MIME gate (isStrictRawMimeStructuredValue) accepts only a token or a quoted string as a parameter value. STRICT_MIME_PARAMETER_RE matches name=Signed, the tail check then fails on decision (1).pdf, isStrictlyWellFormedRawMime returns false and classifyRawMessageEncryption(raw, 0, { signedRaw: true }) returns "unknown". The read-only fallback in classifyRawMessageReadSafety cannot recover the message either, because the envelope embeds a message whose parts are base64 (embedded && nonIdentity). Running the production classifiers on a real PEC message confirmed that this rule is the only one that fires: with the gate relaxed as below, the strict classifier returns "clear".

Fix. libmime reads an unquoted parameter value up to the next ; and trims trailing whitespace (nsMIMEHeaderParamImpl::DoParameterInternal, the unquoted branch), and parseRawMimeHeaderValue / collectRawMimeParameterNames already read it the same way. The gate now accepts that form for name and filename only, made of token characters, parentheses and spaces or tabs, the same names it already accepts in quoted form. Still refused:

  • unquoted values with spaces for any other parameter (charset=utf-8 x), and for RFC 2231 segments (filename*0=a b);
  • quotes and backslashes inside an unquoted value (where our splitter and libmime would disagree);
  • everything else the gate refused before.

The protected-name rule and the nested-encryption checks still see these names: an unquoted name=… .p7m on an application/octet-stream part stays "unknown", and an application/pkcs7-mime part with an unquoted name stays "encrypted".

Validation

  • New tests in test/privacy-messages.test.cjs:
    • a PEC-shaped S/MIME envelope (synthetic: fictitious addresses and names) added to the realistic signed fixtures: gate, classifier, getMessage, rawSource and reply;
    • the same envelope with an unquoted .p7m name on an octet-stream part (withheld) and with an enveloped pkcs7-mime part (encrypted);
    • four new malformed-leaf cases (unquoted charset with a space, quote or backslash in an unquoted name, unquoted continuation segment with a space).
  • The two PEC tests fail without the change and pass with it.
  • npm test on Windows, Node 24: 4,990 pass, 4 fail, 16 skipped. The same 4 failures occur on unmodified main (message-headers, protected-subjects, search-messages-scan: SyntaxError: Cannot use import statement outside a module; one Windows path assertion in bridge-requests).
  • npx eslint extension/mcp_server/api.js test/privacy-messages.test.cjs: 0 errors (5 existing warnings elsewhere in api.js).
  • Manual check: the production classifiers, run in a node:vm sandbox on a real PEC message from the offline store of Thunderbird 160.0, return "unknown" before and "clear" after the change. The message itself is not included.

🤖 Generated with Claude Code

… the signed raw path

Italian certified email (PEC) providers deliver every message inside an
S/MIME-signed envelope (multipart/signed, application/pkcs7-signature) that
carries the original as postacert.eml. Several senders behind it, JavaMail
based, write attachment names with spaces and parentheses unquoted, e.g.
"Content-Type: application/pdf; name=Signed decision (1).pdf".

The strict raw MIME gate only accepted a token or a quoted string as a
parameter value, so such a name made isStrictlyWellFormedRawMime return
false and classifyRawMessageEncryption return "unknown". getMessage and
rawSource then withheld these clear-signed messages ("encryption status
could not be determined"), and the read-only fallback could not recover
them either because the envelope embeds a message with base64 parts.

libmime reads an unquoted parameter value up to the next ';' and trims
trailing whitespace (nsMIMEHeaderParamImpl::DoParameterInternal), which is
what parseRawMimeHeaderValue and collectRawMimeParameterNames already do.
The gate now accepts that form for name and filename only, made of token
characters, parentheses and spaces or tabs, without quotes or backslashes,
like the quoted form it already accepts. Other parameters, RFC 2231
segments, quotes and backslashes in unquoted values are still refused, and
the protected-name and nested-encryption checks still apply to these names.

Tests: a PEC-shaped signed envelope with an embedded message, unquoted
names and an XML attachment is read through the gate, getMessage, rawSource
and reply; the same envelope with an unquoted .p7m name on an
octet-stream part is still withheld, and with an enveloped pkcs7-mime part
is still classified as encrypted; four new malformed-leaf cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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