From 3780c4af29fca1dc006af8b16996e3d42134cff2 Mon Sep 17 00:00:00 2001 From: sh Date: Fri, 23 Oct 2026 11:15:58 +0000 Subject: [PATCH] smp server: validate stats_save_interval --- src/Simplex/Messaging/Server/Main.hs | 5 ++++- tests/CLITests.hs | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Server/Main.hs b/src/Simplex/Messaging/Server/Main.hs index 9cf6c1206..b35c06fb0 100644 --- a/src/Simplex/Messaging/Server/Main.hs +++ b/src/Simplex/Messaging/Server/Main.hs @@ -440,6 +440,9 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath = transports = iniTransports ini sharedHTTP = any (\(_, _, addHTTP) -> addHTTP) transports iniStoreType = either error id $! readStoreType ini + iniStatsSaveInterval = case readIniDefault defStatsSaveInterval "STORE_LOG" "stats_save_interval" ini of + n | n < 1 -> error $ "invalid INI setting stats_save_interval: " <> show n + n -> n iniStoreCfg :: SupportedStore qs ms => SQSType qs -> SMSType ms -> ServerStoreCfg (MsgStoreType qs ms) iniStoreCfg SQSMemory SMSMemory = SSCMemory $ enableStoreLog' ini $> StorePaths {storeLogFile = storeLogFilePath, storeMsgsFile = restoreMessagesFile storeMsgsFilePath} #if defined(dbServerPostgres) @@ -495,7 +498,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath = logStatsStartTime = 0, -- seconds from 00:00 UTC serverStatsLogFile = combine logPath "smp-server-stats.daily.log", serverStatsBackupFile = logStats $> combine logPath "smp-server-stats.log", - statsSaveInterval = readIniDefault defStatsSaveInterval "STORE_LOG" "stats_save_interval" ini, + statsSaveInterval = iniStatsSaveInterval, prometheusInterval = eitherToMaybe (lookupValue "STORE_LOG" "prometheus_interval" ini) >>= readMaybe . T.unpack, prometheusMetricsFile = combine logPath "smp-server-metrics.txt", pendingENDInterval = 15000000, -- 15 seconds diff --git a/tests/CLITests.hs b/tests/CLITests.hs index 3e3b837e6..0f9bd0ec1 100644 --- a/tests/CLITests.hs +++ b/tests/CLITests.hs @@ -86,6 +86,7 @@ cliTests = do it "with store log, no password" $ smpServerTest True False it "static files" smpServerTestStatic it "cloud scripts disable embedded web without certificates" smpCloudScriptsDisableWeb + it "should not start with invalid stats_save_interval" smpServerInvalidStatsSaveInterval #if defined(dbServerPostgres) around_ (postgressBracket ntfTestServerDBConnectInfo) $ before_ (createNtfSchema ntfTestServerDBConnectInfo ntfTestServerDBOpts) $ describe "Ntf server CLI" $ do @@ -204,6 +205,16 @@ smpServerTestStatic = do let X.CertificateChain cc = tlsPeerCert tls in map (X.signedObject . X.getSigned) cc +smpServerInvalidStatsSaveInterval :: HasTestEnv => IO () +smpServerInvalidStatsSaveInterval = do + let iniFile = cfgPath <> "/smp-server.ini" + capture_ (withArgs ["init", "-y", "--no-password"] $ smpServerCLI cfgPath logPath) + >>= (`shouldSatisfy` (("Server initialized, please provide additional server information in " <> iniFile) `isPrefixOf`)) + Right ini <- readIniFile iniFile + writeIniFile iniFile ini {iniSections = HM.adjust (("stats_save_interval", "0") :) "STORE_LOG" (iniSections ini)} + capture_ (withArgs ["start"] $ 5000000 `timeout` smpServerCLI cfgPath logPath) + `shouldThrow` errorCall "invalid INI setting stats_save_interval: 0" + smpCloudScriptsDisableWeb :: HasCallStack => IO () smpCloudScriptsDisableWeb = do linode <- readFile "scripts/smp-server-linode.sh"