|
| 1 | +-- | Sanctify-PHP CLI entry point |
| 2 | +-- SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | +module Main where |
| 4 | + |
| 5 | +import System.Environment (getArgs) |
| 6 | +import System.Exit (exitFailure, exitSuccess) |
| 7 | +import Data.Text (Text) |
| 8 | +import qualified Data.Text as T |
| 9 | +import qualified Data.Text.IO as TIO |
| 10 | +import System.Directory (doesFileExist, doesDirectoryExist, listDirectory) |
| 11 | +import System.FilePath ((</>), takeExtension) |
| 12 | +import Control.Monad (forM, filterM, when) |
| 13 | +import Data.Either (partitionEithers) |
| 14 | + |
| 15 | +import Sanctify.Parser |
| 16 | +import Sanctify.AST |
| 17 | +import Sanctify.Analysis.Security |
| 18 | +import Sanctify.Analysis.Types |
| 19 | +import Sanctify.WordPress.Constraints |
| 20 | +import Sanctify.Transform.StrictTypes |
| 21 | +import Sanctify.Transform.Sanitize |
| 22 | +import Sanctify.Transform.TypeHints |
| 23 | +import Sanctify.Emit |
| 24 | +import Sanctify.Config |
| 25 | +import Sanctify.Report |
| 26 | + |
| 27 | +main :: IO () |
| 28 | +main = do |
| 29 | + args <- getArgs |
| 30 | + case args of |
| 31 | + ["--help"] -> printHelp |
| 32 | + ["-h"] -> printHelp |
| 33 | + ["--version"] -> putStrLn "sanctify-php 0.1.0" |
| 34 | + ["analyze", path] -> analyzeCommand path |
| 35 | + ["fix", path] -> fixCommand path |
| 36 | + ["report", path] -> reportCommand path |
| 37 | + ["export", "--php-ini", path] -> exportPhpIniCommand path |
| 38 | + ["export", "--nginx", path] -> exportNginxCommand path |
| 39 | + ["export", "--guix", path] -> exportGuixCommand path |
| 40 | + _ -> do |
| 41 | + putStrLn "Usage: sanctify <command> [options] <path>" |
| 42 | + putStrLn "Run 'sanctify --help' for more information." |
| 43 | + exitFailure |
| 44 | + |
| 45 | +printHelp :: IO () |
| 46 | +printHelp = putStrLn $ unlines |
| 47 | + [ "sanctify-php - Haskell-based PHP hardening and security analysis" |
| 48 | + , "" |
| 49 | + , "USAGE:" |
| 50 | + , " sanctify <command> [options] <path>" |
| 51 | + , "" |
| 52 | + , "COMMANDS:" |
| 53 | + , " analyze <path> Analyze PHP files for security issues" |
| 54 | + , " fix <path> Auto-fix safe issues and report others" |
| 55 | + , " report <path> Generate detailed report" |
| 56 | + , " export Export configuration for infrastructure" |
| 57 | + , "" |
| 58 | + , "EXPORT SUBCOMMANDS:" |
| 59 | + , " --php-ini <path> Generate recommended php.ini settings" |
| 60 | + , " --nginx <path> Generate nginx security rules" |
| 61 | + , " --guix <path> Generate Guix channel overrides" |
| 62 | + , "" |
| 63 | + , "OPTIONS:" |
| 64 | + , " -h, --help Show this help" |
| 65 | + , " --version Show version" |
| 66 | + , "" |
| 67 | + , "EXAMPLES:" |
| 68 | + , " sanctify analyze ./wp-content/plugins/my-plugin/" |
| 69 | + , " sanctify fix --auto-only ./src/" |
| 70 | + , " sanctify report --format=sarif ./theme/ > report.sarif" |
| 71 | + , " sanctify export --php-ini ./project/ >> php.ini" |
| 72 | + , "" |
| 73 | + , "For container integration, see:" |
| 74 | + , " guix/wordpress-container.scm" |
| 75 | + ] |
| 76 | + |
| 77 | +-- | Analyze command |
| 78 | +analyzeCommand :: FilePath -> IO () |
| 79 | +analyzeCommand path = do |
| 80 | + files <- findPhpFiles path |
| 81 | + when (null files) $ do |
| 82 | + putStrLn $ "No PHP files found in: " ++ path |
| 83 | + exitFailure |
| 84 | + |
| 85 | + putStrLn $ "Analyzing " ++ show (length files) ++ " PHP files..." |
| 86 | + |
| 87 | + results <- forM files $ \file -> do |
| 88 | + content <- TIO.readFile file |
| 89 | + case parsePhpString file content of |
| 90 | + Left err -> do |
| 91 | + putStrLn $ " Parse error in " ++ file ++ ": " ++ show err |
| 92 | + pure (file, [], []) |
| 93 | + Right ast -> do |
| 94 | + let secIssues = analyzeSecurityIssues ast |
| 95 | + let wpIssues = if isWordPressCode ast |
| 96 | + then checkWordPressConstraints ast |
| 97 | + else [] |
| 98 | + pure (file, secIssues, wpIssues) |
| 99 | + |
| 100 | + -- Print results |
| 101 | + let totalSec = sum $ map (\(_, s, _) -> length s) results |
| 102 | + let totalWp = sum $ map (\(_, _, w) -> length w) results |
| 103 | + |
| 104 | + putStrLn "" |
| 105 | + putStrLn $ "Found " ++ show totalSec ++ " security issues" |
| 106 | + putStrLn $ "Found " ++ show totalWp ++ " WordPress issues" |
| 107 | + putStrLn "" |
| 108 | + |
| 109 | + forM_ results $ \(file, secIssues, wpIssues) -> |
| 110 | + when (not (null secIssues) || not (null wpIssues)) $ do |
| 111 | + putStrLn $ file ++ ":" |
| 112 | + forM_ secIssues $ \issue -> |
| 113 | + putStrLn $ " [" ++ show (issueSeverity issue) ++ "] " ++ T.unpack (issueDescription issue) |
| 114 | + forM_ wpIssues $ \issue -> |
| 115 | + putStrLn $ " [WP:" ++ show (wpIssueType issue) ++ "] " ++ T.unpack (wpDescription issue) |
| 116 | + putStrLn "" |
| 117 | + |
| 118 | + if totalSec + totalWp > 0 |
| 119 | + then exitFailure |
| 120 | + else exitSuccess |
| 121 | + |
| 122 | +-- | Fix command |
| 123 | +fixCommand :: FilePath -> IO () |
| 124 | +fixCommand path = do |
| 125 | + files <- findPhpFiles path |
| 126 | + putStrLn $ "Processing " ++ show (length files) ++ " PHP files..." |
| 127 | + |
| 128 | + forM_ files $ \file -> do |
| 129 | + content <- TIO.readFile file |
| 130 | + case parsePhpString file content of |
| 131 | + Left _ -> putStrLn $ " Skipping (parse error): " ++ file |
| 132 | + Right ast -> do |
| 133 | + -- Apply safe transformations |
| 134 | + let transformed = applyTransforms ast |
| 135 | + let output = emitPhp transformed |
| 136 | + -- Show diff (don't modify in-place by default) |
| 137 | + when (content /= output) $ do |
| 138 | + putStrLn $ " Would fix: " ++ file |
| 139 | + -- In production, write to file or show diff |
| 140 | + |
| 141 | + putStrLn "Done. Use --in-place to apply changes." |
| 142 | + |
| 143 | +-- | Apply safe transformations |
| 144 | +applyTransforms :: PhpFile -> PhpFile |
| 145 | +applyTransforms = addStrictTypes . addAbspathCheck . addTypeHintsFile |
| 146 | + where |
| 147 | + addTypeHintsFile file = addAllTypeHints emptyContext file |
| 148 | + |
| 149 | +-- | Report command |
| 150 | +reportCommand :: FilePath -> IO () |
| 151 | +reportCommand path = do |
| 152 | + files <- findPhpFiles path |
| 153 | + fileReports <- forM files $ \file -> do |
| 154 | + content <- TIO.readFile file |
| 155 | + case parsePhpString file content of |
| 156 | + Left _ -> pure $ generateFileReport file [] [] 0 0 False |
| 157 | + Right ast -> do |
| 158 | + let secIssues = analyzeSecurityIssues ast |
| 159 | + let wpIssues = if isWordPressCode ast |
| 160 | + then checkWordPressConstraints ast |
| 161 | + else [] |
| 162 | + let autoFixed = length $ filter (canAutoFix . issueType) secIssues |
| 163 | + let manual = length secIssues - autoFixed |
| 164 | + pure $ generateFileReport file secIssues wpIssues autoFixed manual False |
| 165 | + |
| 166 | + report <- generateReport defaultConfig fileReports |
| 167 | + TIO.putStrLn $ renderText report |
| 168 | + where |
| 169 | + canAutoFix :: IssueType -> Bool |
| 170 | + canAutoFix MissingStrictTypes = True |
| 171 | + canAutoFix _ = False |
| 172 | + |
| 173 | +-- | Export php.ini recommendations |
| 174 | +exportPhpIniCommand :: FilePath -> IO () |
| 175 | +exportPhpIniCommand path = do |
| 176 | + issues <- collectIssues path |
| 177 | + TIO.putStrLn $ emitPhpIniRecommendations issues |
| 178 | + |
| 179 | +-- | Export nginx rules |
| 180 | +exportNginxCommand :: FilePath -> IO () |
| 181 | +exportNginxCommand path = do |
| 182 | + issues <- collectIssues path |
| 183 | + TIO.putStrLn $ emitNginxRules issues |
| 184 | + |
| 185 | +-- | Export Guix overrides |
| 186 | +exportGuixCommand :: FilePath -> IO () |
| 187 | +exportGuixCommand path = do |
| 188 | + issues <- collectIssues path |
| 189 | + TIO.putStrLn $ emitGuixOverrides issues |
| 190 | + |
| 191 | +-- | Collect all issues from a path |
| 192 | +collectIssues :: FilePath -> IO [SecurityIssue] |
| 193 | +collectIssues path = do |
| 194 | + files <- findPhpFiles path |
| 195 | + concat <$> forM files (\file -> do |
| 196 | + content <- TIO.readFile file |
| 197 | + case parsePhpString file content of |
| 198 | + Left _ -> pure [] |
| 199 | + Right ast -> pure $ analyzeSecurityIssues ast) |
| 200 | + |
| 201 | +-- | Find all PHP files in a path |
| 202 | +findPhpFiles :: FilePath -> IO [FilePath] |
| 203 | +findPhpFiles path = do |
| 204 | + isFile <- doesFileExist path |
| 205 | + if isFile |
| 206 | + then if takeExtension path == ".php" |
| 207 | + then pure [path] |
| 208 | + else pure [] |
| 209 | + else do |
| 210 | + isDir <- doesDirectoryExist path |
| 211 | + if isDir |
| 212 | + then do |
| 213 | + entries <- listDirectory path |
| 214 | + let fullPaths = map (path </>) entries |
| 215 | + files <- filterM doesFileExist fullPaths |
| 216 | + dirs <- filterM doesDirectoryExist fullPaths |
| 217 | + let phpFiles = filter ((== ".php") . takeExtension) files |
| 218 | + subFiles <- concat <$> mapM findPhpFiles dirs |
| 219 | + pure $ phpFiles ++ subFiles |
| 220 | + else pure [] |
0 commit comments