Repository navigation
feat(email): constrain automated company mail submission #6084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
devantler
wants to merge
6
commits into
main
Choose a base branch
from
codex/mail-submission-6082
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,832
−0
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a764dd
feat(email): constrain automated company mail submission
devantler 883719e
fix(email): compare provisioned credentials without password hashing
devantler 5c09925
fix(email): validate certificates and count malformed authentication
devantler 663a260
Merge commit '3ff956dc4416ca42df34351379fad4445ee5f780' into codex/ma…
devantler 87bbba8
Merge commit '114850b496b9014d90219e352b5ece2ab135bedc' into codex/ma…
devantler 1a8c9e8
fix(email): isolate transactions and honor the payload bound
devantler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Automated mail submission | ||
|
|
||
| **Status:** Proposed | ||
|
|
||
| ## Context | ||
|
|
||
| The business portal needs invitation messages with one enforced company sender. | ||
| Application credentials must not grant mailbox access or unrestricted submission. | ||
| An optional upstream content hook cannot be the only boundary: the application | ||
| must remain restricted when that hook is absent or unavailable. | ||
|
|
||
| ## Decision | ||
|
|
||
| Use a private SMTP submission component between the application and company Email. | ||
| It authenticates the application over TLS, validates the configured envelope and | ||
| single message author, and forwards synchronously over verified upstream TLS. | ||
| Its application credential differs from the upstream send-only credential and is | ||
| not accepted by the upstream service. No successful acknowledgement is returned | ||
| before the upstream accepts the complete message. | ||
|
|
||
| The executable has no default listener. It is not installed or enabled by this | ||
| source delivery. Deployment, private credential custody and real delivery remain | ||
| under the dedicated-sender issue. Existing mail, inbound routing and the agents' | ||
| restricted sender are unchanged; no external relay or subscription is introduced. | ||
|
|
||
| ## Alternatives | ||
|
|
||
| - A scoped native DATA hook can reject invalid authors, but omission can skip it. | ||
| Using it alone would make application restriction depend on optional upstream | ||
| configuration and a separate activation hold. | ||
| - Replacing the company mail server would expand the work into mailbox migration | ||
| and recovery. That is unnecessary for an application submission boundary. | ||
|
|
||
| ## Consequences | ||
|
|
||
| The component adds one maintained process and separate private credentials. It | ||
| needs bounded concurrency, sizes, deadlines, authentication attempts and rate | ||
| limits, with no message or credential logging. It performs no background retry. | ||
| A lost upstream acknowledgement can leave delivery uncertain; callers must use | ||
| bounded retries and replay-safe invitation links rather than infer non-delivery. | ||
|
|
||
| Loopback protocol tests use a separately authenticated SMTP peer and generated | ||
| certificates. They are not deployed Stalwart, external delivery, SPF/DKIM/DMARC, | ||
| customer enrollment or portal-activation proof. Those remain explicit parent gates. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Automated mail submission | ||
|
|
||
| This internal SMTP component constrains an application's automated mail before | ||
| submitting it to company mail. It is not a mailbox, delivery queue or relay | ||
| subscription. Nothing starts without an explicit command and private inputs. | ||
|
|
||
| The application authenticates to this component over implicit TLS. Only its | ||
| configured envelope sender and one matching `From` author are accepted. The | ||
| component authenticates separately to the upstream SMTP service, using mandatory | ||
| STARTTLS with an explicit CA and server name. The application credential must | ||
| have no upstream account. Upstream credentials must belong to a dedicated, | ||
| send-only identity; deployment must verify that restriction on the real MTA. | ||
|
|
||
| Success means the upstream service acknowledged the final DATA command. It does | ||
| not mean external delivery. A lost acknowledgement returns a temporary failure; | ||
| the application must tolerate possible duplicates and use stable invitation | ||
| identifiers. Permanent upstream submission refusals remain permanent failures. | ||
| No protocol body, recipient, password or peer error is logged. | ||
|
|
||
| ## Private configuration | ||
|
|
||
| Build from the repository root with `go build -o <output> ./scripts/mail-submission`. | ||
| Use `mail-submission check --config-dir <directory>` to validate inputs without | ||
| starting a listener. `mail-submission serve --config-dir <directory>` starts the | ||
| explicitly configured listener. There is no default address, credential, sender | ||
| or development mode. | ||
|
|
||
| The directory contains these regular files, owned by root or the service user, | ||
| with mode `0600`, `0400`, `0640` or `0440`: | ||
|
|
||
| - `settings.json`: one JSON object with string members `listenAddress`, `sender`, | ||
| `senderName`, `clientUser`, `upstreamAddress`, `upstreamServerName`, | ||
| `upstreamUser` and optional `replyTo`. | ||
| - `client-password` and `upstream-password`: distinct printable secrets of | ||
| 32–256 bytes, without a trailing newline. Usernames must also be distinct. | ||
| - `server.crt` and `server.key`: the receiving certificate chain and private key. | ||
| - `upstream-ca.pem`: only PEM CA certificates for the upstream trust boundary. | ||
|
|
||
| Unknown or duplicate JSON members, unresolved substitutions, malformed trust, | ||
| leaf symlinks and unsafe file permissions are refused. Kubernetes Secret | ||
| projections are read through one anchored `..data` generation. Rotate inputs by | ||
| restarting the component; existing processes intentionally retain their selected | ||
| generation. Do not put actual settings, passwords or certificates in Git. | ||
|
|
||
| `listenAddress` is an explicit numeric IP and unprivileged port. The server | ||
| requires TLS 1.2 or newer, bounds active connections to 16 and each connection to | ||
| 30 seconds, accepts at most three authentication attempts per connection and one | ||
| recipient per message. Messages are limited to 1 MiB and headers to 64 KiB. | ||
| Authentication has a shared burst allowance of sixteen attempts and replenishes\none allowance per second. Submission has a shared burst allowance of ten messages and replenishes one | ||
| allowance every six seconds. RSET does not reset these budgets. The upstream | ||
| connection has an absolute ten-second lifetime. Shutdown allows five seconds for | ||
| active sessions, then closes them. | ||
|
|
||
| A missing `replyTo` refuses all Reply-To headers. When configured, exactly one | ||
| matching address is required. Sender, Return-Path and Resent headers are refused. | ||
| The original accepted message bytes are submitted unchanged. | ||
|
|
||
| ## Validation and deployment boundary | ||
|
|
||
| Run `go test -race ./scripts/mail-submission` and `go vet ./scripts/mail-submission`. | ||
| Tests use generated fictional certificates and native Go SMTP peers on loopback. | ||
| They cover author and envelope refusals, credential separation, receiving and | ||
| upstream TLS, unavailable peers, permanent refusals, lost acknowledgements, | ||
| resource bounds, private input validation and credential replacement. | ||
|
|
||
| These peers are not the production MTA. This change creates no Deployment, | ||
| listener in the live platform, mailbox, real credential or external message. | ||
| Issue #5400 retains deployment, credential custody, protected recovery, | ||
| upstream send-only policy and actual invitation-delivery acceptance. Its | ||
| prerequisite recovery hold must remain intact. The portal remains closed until | ||
| its separate end-to-end sign-in and invitation checks pass. | ||
|
|
||
| The SMTP and SASL libraries are MIT-licensed (`github.com/emersion/go-smtp` and | ||
| `github.com/emersion/go-sasl`). The repository's existing terms apply to this | ||
| component's own source; no new license grant is introduced. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "encoding/json" | ||
| "encoding/pem" | ||
| "errors" | ||
| "io" | ||
| "os" | ||
| "syscall" | ||
| "time" | ||
| ) | ||
|
|
||
| // privateFile only reads regular, non-executable files in the selected directory. | ||
| // The root anchors all member reads; leaf symlinks are never followed. | ||
| func privateFile(root *os.Root, name string, limit int64) ([]byte, error) { | ||
| file, err := root.OpenFile(name, os.O_RDONLY|syscall.O_NOFOLLOW, 0) | ||
| if err != nil { | ||
| return nil, errConfiguration | ||
| } | ||
| defer func() { _ = file.Close() }() | ||
| info, err := file.Stat() | ||
| if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o137 != 0 || info.Size() > limit { | ||
| return nil, errConfiguration | ||
| } | ||
| owner, ok := info.Sys().(*syscall.Stat_t) | ||
| if !ok || (owner.Uid != 0 && int64(owner.Uid) != int64(os.Geteuid())) { | ||
| return nil, errConfiguration | ||
| } | ||
| raw, err := io.ReadAll(io.LimitReader(file, limit+1)) | ||
| if err != nil || int64(len(raw)) > limit { | ||
| return nil, errConfiguration | ||
| } | ||
| if err = file.Close(); err != nil { | ||
| return nil, errConfiguration | ||
| } | ||
| return raw, nil | ||
| } | ||
|
|
||
| func parseSettings(raw []byte) (settings, error) { | ||
| var result settings | ||
| fields := map[string]*string{"listenAddress": &result.ListenAddress, "sender": &result.Sender, "senderName": &result.SenderName, "clientUser": &result.ClientUser, "upstreamAddress": &result.UpstreamAddress, "upstreamServerName": &result.UpstreamServerName, "upstreamUser": &result.UpstreamUser, "replyTo": &result.ReplyTo} | ||
| decoder := json.NewDecoder(bytes.NewReader(raw)) | ||
| token, err := decoder.Token() | ||
| if err != nil || token != json.Delim('{') { | ||
| return result, errConfiguration | ||
| } | ||
| seen := make(map[string]bool) | ||
| for decoder.More() { | ||
| token, err = decoder.Token() | ||
| if err != nil { | ||
| return result, errConfiguration | ||
| } | ||
| key, ok := token.(string) | ||
| destination, known := fields[key] | ||
| if !ok || !known || seen[key] { | ||
| return result, errConfiguration | ||
| } | ||
| seen[key] = true | ||
| value, valueErr := decoder.Token() | ||
| text, stringValue := value.(string) | ||
| if valueErr != nil || !stringValue { | ||
| return result, errConfiguration | ||
| } | ||
| *destination = text | ||
| } | ||
| token, err = decoder.Token() | ||
| if err != nil || token != json.Delim('}') { | ||
| return result, errConfiguration | ||
| } | ||
| if _, err = decoder.Token(); !errors.Is(err, io.EOF) { | ||
| return result, errConfiguration | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func trustCertificates(raw []byte) (*x509.CertPool, error) { | ||
| pool := x509.NewCertPool() | ||
| count := 0 | ||
| for len(bytes.TrimSpace(raw)) > 0 { | ||
| // pem.Decode otherwise silently skips a non-PEM prefix. | ||
| raw = bytes.TrimSpace(raw) | ||
| if !bytes.HasPrefix(raw, []byte("-----BEGIN CERTIFICATE-----")) { | ||
| return nil, errConfiguration | ||
| } | ||
| block, remaining := pem.Decode(raw) | ||
| if block == nil || block.Type != "CERTIFICATE" || len(block.Headers) != 0 { | ||
| return nil, errConfiguration | ||
| } | ||
| cert, err := x509.ParseCertificate(block.Bytes) | ||
| if err != nil || !cert.IsCA { | ||
| return nil, errConfiguration | ||
| } | ||
| pool.AddCert(cert) | ||
| count++ | ||
| raw = remaining | ||
| } | ||
| if count == 0 { | ||
| return nil, errConfiguration | ||
| } | ||
| return pool, nil | ||
| } | ||
|
|
||
| func loadConfiguration(directory string) (cfg configuration, resultErr error) { | ||
| if directory == "" { | ||
| return cfg, errConfiguration | ||
| } | ||
| root, err := os.OpenRoot(directory) | ||
| if err != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| defer func(opened *os.Root) { | ||
| if closeErr := opened.Close(); closeErr != nil { | ||
| resultErr = errConfiguration | ||
| } | ||
| }(root) | ||
| // Kubernetes projects each Secret generation under ..data. Pin that directory | ||
| // once, before reading any members, rather than mixing two generations. | ||
| if _, err = root.Lstat("..data"); err == nil { | ||
| snapshot, e := root.OpenRoot("..data") | ||
| if e != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| defer func(opened *os.Root) { | ||
| if closeErr := opened.Close(); closeErr != nil { | ||
| resultErr = errConfiguration | ||
| } | ||
| }(snapshot) | ||
| root = snapshot | ||
| } else if !errors.Is(err, os.ErrNotExist) { | ||
| return cfg, errConfiguration | ||
| } | ||
| members := make(map[string][]byte) | ||
| for _, name := range []string{"settings.json", "client-password", "upstream-password", "server.crt", "server.key", "upstream-ca.pem"} { | ||
| limit := int64(64 << 10) | ||
| if name == "settings.json" { | ||
| limit = 4 << 10 | ||
| } | ||
| members[name], err = privateFile(root, name, limit) | ||
| if err != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| } | ||
| cfg.Settings, err = parseSettings(members["settings.json"]) | ||
| if err != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| cfg.ClientPassword = string(members["client-password"]) | ||
| cfg.UpstreamPassword = string(members["upstream-password"]) | ||
| pair, err := tls.X509KeyPair(members["server.crt"], members["server.key"]) | ||
| if err != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| leaf, err := x509.ParseCertificate(pair.Certificate[0]) | ||
| if err != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| now := time.Now() | ||
| if now.Before(leaf.NotBefore) || !now.Before(leaf.NotAfter) { | ||
| return cfg, errConfiguration | ||
| } | ||
| roots, err := trustCertificates(members["upstream-ca.pem"]) | ||
| if err != nil { | ||
| return cfg, errConfiguration | ||
| } | ||
| cfg.ServerTLS = &tls.Config{MinVersion: tls.VersionTLS12, Certificates: []tls.Certificate{pair}} | ||
| cfg.UpstreamTLS = &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots, ServerName: cfg.Settings.UpstreamServerName} | ||
| return cfg, cfg.validate() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.