@@ -10,7 +10,6 @@ import (
1010 "encoding/json"
1111 "fmt"
1212 "io"
13- "io/fs"
1413 "log/slog"
1514 "net"
1615 "net/http"
@@ -117,6 +116,14 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
117116 rootDir := filepath .Dir (instance .GetInstanceRuntime ().GetConfigPath ())
118117
119118 for _ , conf := range payload .Config {
119+ if ! ncp .agentConfig .IsDirectoryAllowed (conf .File ) {
120+ slog .WarnContext (ctx , "File included in NGINX config is outside of allowed directories, " +
121+ "excluding from config" ,
122+ "file" , conf .File )
123+
124+ continue
125+ }
126+
120127 formatMap := make (map [string ]string )
121128 err := ncp .crossplaneConfigTraverse (ctx , & conf ,
122129 func (ctx context.Context , parent , directive * crossplane.Directive ) error {
@@ -138,10 +145,8 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
138145 "is disabled while applying a config; " + "log errors to file to enable error monitoring" ,
139146 directive .Args [0 ]), "error_log" , directive .Args [0 ])
140147 }
141- case "root" :
142- rootFiles := ncp .rootFiles (ctx , directive .Args [0 ])
143- nginxConfigContext .Files = append (nginxConfigContext .Files , rootFiles ... )
144- case "ssl_certificate" , "proxy_ssl_certificate" , "ssl_client_certificate" , "ssl_trusted_certificate" :
148+ case "ssl_certificate" , "proxy_ssl_certificate" , "ssl_client_certificate" ,
149+ "ssl_trusted_certificate" :
145150 sslCertFile := ncp .sslCert (ctx , directive .Args [0 ], rootDir )
146151 if ! ncp .isDuplicateFile (nginxConfigContext .Files , sslCertFile ) {
147152 nginxConfigContext .Files = append (nginxConfigContext .Files , sslCertFile )
@@ -195,18 +200,17 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
195200}
196201
197202func (ncp * NginxConfigParser ) ignoreLog (logPath string ) bool {
198- logLower := strings .ToLower (logPath )
199203 ignoreLogs := []string {"off" , "/dev/stderr" , "/dev/stdout" , "/dev/null" , "stderr" , "stdout" }
200204
201- if strings .HasPrefix (logLower , "syslog:" ) || slices .Contains (ignoreLogs , logLower ) {
205+ if strings .HasPrefix (logPath , "syslog:" ) || slices .Contains (ignoreLogs , logPath ) {
202206 return true
203207 }
204208
205- if ncp .isExcludeLog (logLower ) {
209+ if ncp .isExcludeLog (logPath ) {
206210 return true
207211 }
208212
209- if ! ncp .agentConfig .IsDirectoryAllowed (logLower ) {
213+ if ! ncp .agentConfig .IsDirectoryAllowed (logPath ) {
210214 slog .Warn ("Log being read is outside of allowed directories" , "log_path" , logPath )
211215 }
212216
@@ -315,39 +319,6 @@ func (ncp *NginxConfigParser) errorLogDirectiveLevel(directive *crossplane.Direc
315319 return ""
316320}
317321
318- func (ncp * NginxConfigParser ) rootFiles (ctx context.Context , rootDir string ) (rootFiles []* mpi.File ) {
319- if ! ncp .agentConfig .IsDirectoryAllowed (rootDir ) {
320- slog .DebugContext (ctx , "Root directory not in allowed directories" , "root_directory" , rootDir )
321- return rootFiles
322- }
323-
324- err := filepath .WalkDir (rootDir ,
325- func (path string , d fs.DirEntry , err error ) error {
326- if err != nil {
327- return err
328- }
329-
330- if d .IsDir () {
331- return nil
332- }
333-
334- rootFileMeta , fileMetaErr := files .FileMeta (path )
335- if fileMetaErr != nil {
336- return fileMetaErr
337- }
338-
339- rootFiles = append (rootFiles , & mpi.File {FileMeta : rootFileMeta })
340-
341- return nil
342- },
343- )
344- if err != nil {
345- slog .WarnContext (ctx , "Unable to walk root directory" , "root_directory" , rootDir )
346- }
347-
348- return rootFiles
349- }
350-
351322func (ncp * NginxConfigParser ) sslCert (ctx context.Context , file , rootDir string ) (sslCertFile * mpi.File ) {
352323 if strings .Contains (file , "$" ) {
353324 // cannot process any filepath with variables
0 commit comments