@@ -2,9 +2,13 @@ package nginx
22
33import (
44 "encoding/json"
5+ "fmt"
56 "github.com/0xJacky/Nginx-UI/api"
7+ "github.com/0xJacky/Nginx-UI/internal/cache"
8+ "github.com/0xJacky/Nginx-UI/internal/helper"
69 "github.com/0xJacky/Nginx-UI/internal/logger"
710 "github.com/0xJacky/Nginx-UI/internal/nginx"
11+ "github.com/0xJacky/Nginx-UI/settings"
812 "github.com/gin-gonic/gin"
913 "github.com/gorilla/websocket"
1014 "github.com/hpcloud/tail"
@@ -30,6 +34,7 @@ type controlStruct struct {
3034type nginxLogPageResp struct {
3135 Content string `json:"content"`
3236 Page int64 `json:"page"`
37+ Error string `json:"error,omitempty"`
3338}
3439
3540func GetNginxLogPage (c * gin.Context ) {
@@ -46,28 +51,37 @@ func GetNginxLogPage(c *gin.Context) {
4651 logPath , err := getLogPath (& control )
4752
4853 if err != nil {
54+ c .JSON (http .StatusInternalServerError , nginxLogPageResp {
55+ Error : err .Error (),
56+ })
4957 logger .Error (err )
5058 return
5159 }
5260
5361 logFileStat , err := os .Stat (logPath )
5462
5563 if err != nil {
56- c .JSON (http .StatusOK , nginxLogPageResp {})
64+ c .JSON (http .StatusInternalServerError , nginxLogPageResp {
65+ Error : err .Error (),
66+ })
5767 logger .Error (err )
5868 return
5969 }
6070
6171 if ! logFileStat .Mode ().IsRegular () {
62- c .JSON (http .StatusOK , nginxLogPageResp {})
72+ c .JSON (http .StatusInternalServerError , nginxLogPageResp {
73+ Error : "log file is not regular file" ,
74+ })
6375 logger .Error ("log file is not regular file:" , logPath )
6476 return
6577 }
6678
6779 f , err := os .Open (logPath )
6880
6981 if err != nil {
70- c .JSON (http .StatusOK , nginxLogPageResp {})
82+ c .JSON (http .StatusInternalServerError , nginxLogPageResp {
83+ Error : err .Error (),
84+ })
7185 logger .Error (err )
7286 return
7387 }
@@ -90,15 +104,19 @@ func GetNginxLogPage(c *gin.Context) {
90104 // seek
91105 _ , err = f .Seek (offset , io .SeekStart )
92106 if err != nil && err != io .EOF {
93- c .JSON (http .StatusOK , nginxLogPageResp {})
107+ c .JSON (http .StatusInternalServerError , nginxLogPageResp {
108+ Error : err .Error (),
109+ })
94110 logger .Error (err )
95111 return
96112 }
97113
98114 n , err := f .Read (buf )
99115
100116 if err != nil && err != io .EOF {
101- c .JSON (http .StatusOK , nginxLogPageResp {})
117+ c .JSON (http .StatusInternalServerError , nginxLogPageResp {
118+ Error : err .Error (),
119+ })
102120 logger .Error (err )
103121 return
104122 }
@@ -109,7 +127,30 @@ func GetNginxLogPage(c *gin.Context) {
109127 })
110128}
111129
130+ // isLogPathUnderWhiteList checks if the log path is under one of the paths in LogDirWhiteList
131+ func isLogPathUnderWhiteList (path string ) bool {
132+ cacheKey := fmt .Sprintf ("isLogPathUnderWhiteList:%s" , path )
133+ res , ok := cache .Get (cacheKey )
134+ // no cache, check it
135+ if ! ok {
136+ for _ , whitePath := range settings .NginxSettings .LogDirWhiteList {
137+ if helper .IsUnderDirectory (path , whitePath ) {
138+ cache .Set (cacheKey , true , 0 )
139+ return true
140+ }
141+ }
142+ return false
143+ }
144+ return res .(bool )
145+ }
146+
112147func getLogPath (control * controlStruct ) (logPath string , err error ) {
148+ if len (settings .NginxSettings .LogDirWhiteList ) == 0 {
149+ err = errors .New ("The settings.NginxSettings.LogDirWhiteList has not been configured. " +
150+ "For security reasons, please configure a whitelist of log directories. " +
151+ "Please visit https://nginxui.com/guide/config-nginx.html for more information." )
152+ return
153+ }
113154 switch control .Type {
114155 case "site" :
115156 var config * nginx.NgxConfig
@@ -172,6 +213,11 @@ func getLogPath(control *controlStruct) (logPath string, err error) {
172213 logPath = path
173214 }
174215
216+ // check if logPath is under one of the paths in LogDirWhiteList
217+ if ! isLogPathUnderWhiteList (logPath ) {
218+ err = errors .New ("The log path is not under the paths in LogDirWhiteList." )
219+ return "" , err
220+ }
175221 return
176222}
177223
0 commit comments