Server: Support passphrase for SSL private key (#4488) * SSL: support passphrase for private key * Recommend CLI argument or environment variable for key passphrase * Fix SSL passphrase handling to ensure it is always a string
Signed| @@ -40,9 +40,15 @@ browserLaunch: | ||
| 40 | 40 | port: 8000 |
| 41 | 41 | # -- SSL options -- |
| 42 | 42 | ssl: |
| 43 | + # Enable SSL/TLS encryption | |
| 43 | 44 | enabled: false |
| 45 | + # Path to certificate (relative to server root) | |
| 44 | 46 | certPath: "./certs/cert.pem" |
| 47 | + # Path to private key (relative to server root) | |
| 45 | 48 | keyPath: "./certs/privkey.pem" |
| 49 | + # Private key passphrase (leave empty if not needed) | |
| 50 | + # For better security, use a CLI argument or an environment variable (SILLYTAVERN_SSL_KEYPASSPHRASE) | |
| 51 | + keyPassphrase: "" | |
| 46 | 52 | # -- SECURITY CONFIGURATION -- |
| 47 | 53 | # Toggle whitelist mode |
| 48 | 54 | whitelistMode: true |
| @@ -27,6 +27,7 @@ import { initConfig } from './config-init.js'; | ||
| 27 | 27 | * @property {boolean} ssl If enable SSL |
| 28 | 28 | * @property {string} certPath Path to certificate |
| 29 | 29 | * @property {string} keyPath Path to private key |
| 30 | + * @property {string} keyPassphrase SSL private key passphrase | |
| 30 | 31 | * @property {boolean} whitelistMode If enable whitelist mode |
| 31 | 32 | * @property {boolean} basicAuthMode If enable basic authentication |
| 32 | 33 | * @property {boolean} requestProxyEnabled If enable outgoing request proxy |
| @@ -70,6 +71,7 @@ export class CommandLineParser { | ||
| 70 | 71 | ssl: false, |
| 71 | 72 | certPath: 'certs/cert.pem', |
| 72 | 73 | keyPath: 'certs/privkey.pem', |
| 74 | + keyPassphrase: '', | |
| 73 | 75 | whitelistMode: true, |
| 74 | 76 | basicAuthMode: false, |
| 75 | 77 | requestProxyEnabled: false, |
| @@ -193,6 +195,11 @@ export class CommandLineParser { | ||
| 193 | 195 | default: null, |
| 194 | 196 | describe: 'Path to SSL private key file', |
| 195 | 197 | }) |
| 198 | + .option('keyPassphrase', { | |
| 199 | + type: 'string', | |
| 200 | + default: null, | |
| 201 | + describe: 'Passphrase for the SSL private key', | |
| 202 | + }) | |
| 196 | 203 | .option('whitelist', { |
| 197 | 204 | type: 'boolean', |
| 198 | 205 | default: null, |
| @@ -291,6 +298,7 @@ export class CommandLineParser { | ||
| 291 | 298 | ssl: cliArguments.ssl ?? getConfigValue('ssl.enabled', defaultConfig.ssl, 'boolean'), |
| 292 | 299 | certPath: cliArguments.certPath ?? getConfigValue('ssl.certPath', defaultConfig.certPath), |
| 293 | 300 | keyPath: cliArguments.keyPath ?? getConfigValue('ssl.keyPath', defaultConfig.keyPath), |
| 301 | + keyPassphrase: cliArguments.keyPassphrase ?? getConfigValue('ssl.keyPassphrase', defaultConfig.keyPassphrase), | |
| 294 | 302 | whitelistMode: cliArguments.whitelist ?? getConfigValue('whitelistMode', defaultConfig.whitelistMode, 'boolean'), |
| 295 | 303 | basicAuthMode: cliArguments.basicAuthMode ?? getConfigValue('basicAuthMode', defaultConfig.basicAuthMode, 'boolean'), |
| 296 | 304 | requestProxyEnabled: cliArguments.requestProxyEnabled ?? getConfigValue('requestProxy.enabled', defaultConfig.requestProxyEnabled, 'boolean'), |
| @@ -233,9 +233,11 @@ export class ServerStartup { | ||
| 233 | 233 | #createHttpsServer(url, ipVersion) { |
| 234 | 234 | this.#verifySslOptions(); |
| 235 | 235 | return new Promise((resolve, reject) => { |
| 236 | + /** @type {import('https').ServerOptions} */ | |
| 236 | 237 | const sslOptions = { |
| 237 | 238 | cert: fs.readFileSync(this.cliArgs.certPath), |
| 238 | 239 | key: fs.readFileSync(this.cliArgs.keyPath), |
| 240 | + passphrase: String(this.cliArgs.keyPassphrase ?? ''), | |
| 239 | 241 | }; |
| 240 | 242 | const server = https.createServer(sslOptions, this.app); |
| 241 | 243 | server.on('error', reject); |