🔒 make half-configured bridge TLS a setup error
Browse files at this commitf425d16b7b1abb3b72a0f1f22f03d47f8d4d4ac3Parent: 0346717- fix
- 🔒 make half-configured bridge TLS a setup error
2 changed files +47 -4
- README.md +6 -1
- src/ai/tls.rs +41 -3
README.mdmodified+6 -1
| @@ -3274,11 +3274,16 @@ stream torn down rather than delivering a late answer. | |||
| 3274 | 3274 | ||
| 3275 | ### TLS, auth & finding the certificate | 3275 | ### TLS, auth & finding the certificate |
| 3276 | 3276 | ||
| 3277 | -QUIC has no plaintext mode. With `AI_TLS_CERT`/`AI_TLS_KEY` unset, a | 3277 | +QUIC has no plaintext mode. With `AI_TLS_CERT`/`AI_TLS_KEY` **both** unset, a |
| 3278 | self-signed certificate is generated at boot and its sha256 fingerprint logged | 3278 | self-signed certificate is generated at boot and its sha256 fingerprint logged |
| 3279 | — services pin that instead of installing a CA. The certificate authenticates | 3279 | — services pin that instead of installing a CA. The certificate authenticates |
| 3280 | *the backend*; the shared token in `Hello` authenticates *the service*. | 3280 | *the backend*; the shared token in `Hello` authenticates *the service*. |
| 3281 | 3281 | ||
| 3282 | +Setting **one** of the two (a blank value counts as unset) is a startup error, | ||
| 3283 | +not a fallback to self-signing: a bridge quietly presenting a throwaway | ||
| 3284 | +`localhost` certificate to services pinning the real one is a deployment that | ||
| 3285 | +looks configured and answers `503` to every chatbot send. | ||
| 3286 | + | ||
| 3282 | So a service does not have to be handed a file out of band, the certificate is | 3287 | So a service does not have to be handed a file out of band, the certificate is |
| 3283 | published over HTTP: | 3288 | published over HTTP: |
| 3284 | 3289 | ||
src/ai/tls.rsmodified+41 -3
| @@ -35,13 +35,36 @@ pub fn install_crypto_provider() { | |||
| 35 | let _ = rustls::crypto::ring::default_provider().install_default(); | 35 | let _ = rustls::crypto::ring::default_provider().install_default(); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | -/// Build the listener's TLS config from a PEM pair, or self-signed when either | 38 | +/// Build the listener's TLS config from a PEM pair, or self-signed when *both* |
| 39 | -/// path is absent. | 39 | +/// paths are absent. |
| 40 | +/// | ||
| 41 | +/// Half-configured is a setup error, never a fallback: with one path set the | ||
| 42 | +/// operator plainly meant to present a real certificate, and self-signing | ||
| 43 | +/// instead would hand every dialling service a throwaway `localhost` leaf, | ||
| 44 | +/// regenerated on every boot, with nothing in the log to say so — every service | ||
| 45 | +/// pinning the real fingerprint then fails the handshake and every chatbot send | ||
| 46 | +/// degrades to a `503` with no cause visible anywhere. Note that config's | ||
| 47 | +/// `parse_optional` folds a blank variable to `None`, so `AI_TLS_KEY=""` | ||
| 48 | +/// arrives here as an absent path like any other. | ||
| 40 | pub fn build(cert_path: Option<&str>, key_path: Option<&str>) -> Result<BridgeTls, AiError> { | 49 | pub fn build(cert_path: Option<&str>, key_path: Option<&str>) -> Result<BridgeTls, AiError> { |
| 41 | install_crypto_provider(); | 50 | install_crypto_provider(); |
| 42 | let (chain, key) = match (cert_path, key_path) { | 51 | let (chain, key) = match (cert_path, key_path) { |
| 43 | (Some(c), Some(k)) => load_pem(c, k)?, | 52 | (Some(c), Some(k)) => load_pem(c, k)?, |
| 44 | - _ => self_signed()?, | 53 | + (Some(_), None) => { |
| 54 | + return Err(AiError::Setup( | ||
| 55 | + "AI_TLS_CERT is set but AI_TLS_KEY is not — set both for a real certificate, \ | ||
| 56 | + or neither for a self-signed development one" | ||
| 57 | + .into(), | ||
| 58 | + )); | ||
| 59 | + } | ||
| 60 | + (None, Some(_)) => { | ||
| 61 | + return Err(AiError::Setup( | ||
| 62 | + "AI_TLS_KEY is set but AI_TLS_CERT is not — set both for a real certificate, \ | ||
| 63 | + or neither for a self-signed development one" | ||
| 64 | + .into(), | ||
| 65 | + )); | ||
| 66 | + } | ||
| 67 | + (None, None) => self_signed()?, | ||
| 45 | }; | 68 | }; |
| 46 | let leaf = chain | 69 | let leaf = chain |
| 47 | .first() | 70 | .first() |
| @@ -151,5 +174,20 @@ mod tests { | |||
| 151 | // refusing to boot. | 174 | // refusing to boot. |
| 152 | let err = build(Some("/nonexistent/cert.pem"), Some("/nonexistent/key.pem")).unwrap_err(); | 175 | let err = build(Some("/nonexistent/cert.pem"), Some("/nonexistent/key.pem")).unwrap_err(); |
| 153 | assert!(matches!(err, AiError::Setup(m) if m.contains("AI_TLS_CERT"))); | 176 | assert!(matches!(err, AiError::Setup(m) if m.contains("AI_TLS_CERT"))); |
| 177 | + | ||
| 178 | + // The half-*configured* case, which used to take the self-signed arm: | ||
| 179 | + // one path set and the other absent (unset, or blank — `parse_optional` | ||
| 180 | + // folds a blank variable to `None` before it reaches here) must name | ||
| 181 | + // the missing variable, not quietly mint a throwaway certificate. | ||
| 182 | + let no_key = build(Some("/nonexistent/cert.pem"), None).unwrap_err(); | ||
| 183 | + assert!( | ||
| 184 | + matches!(&no_key, AiError::Setup(m) if m.contains("AI_TLS_KEY")), | ||
| 185 | + "a cert with no key must name the key: {no_key}" | ||
| 186 | + ); | ||
| 187 | + let no_cert = build(None, Some("/nonexistent/key.pem")).unwrap_err(); | ||
| 188 | + assert!( | ||
| 189 | + matches!(&no_cert, AiError::Setup(m) if m.contains("AI_TLS_CERT")), | ||
| 190 | + "a key with no cert must name the cert: {no_cert}" | ||
| 191 | + ); | ||
| 154 | } | 192 | } |
| 155 | } | 193 | } |