Skip to content

Commit 71d92c3

Browse files
feat: add openBaseDir for php website config (#8866)
1 parent a4d6343 commit 71d92c3

File tree

17 files changed

+127
-22
lines changed

17 files changed

+127
-22
lines changed

agent/app/api/v2/website.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,3 +1101,23 @@ func (b *BaseApi) ClearProxyCache(c *gin.Context) {
11011101
}
11021102
helper.Success(c)
11031103
}
1104+
1105+
// @Tags Website
1106+
// @Summary Operate Cross Site Access
1107+
// @Accept json
1108+
// @Param request body request.CrossSiteAccessOp true "request"
1109+
// @Success 200
1110+
// @Security ApiKeyAuth
1111+
// @Security Timestamp
1112+
// @Router /websites/crosssite [post]
1113+
func (b *BaseApi) OperateCrossSiteAccess(c *gin.Context) {
1114+
var req request.CrossSiteAccessOp
1115+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
1116+
return
1117+
}
1118+
if err := websiteService.OperateCrossSiteAccess(req); err != nil {
1119+
helper.InternalServer(c, err)
1120+
return
1121+
}
1122+
helper.Success(c)
1123+
}

agent/app/dto/request/website.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,8 @@ type WebsiteProxyDel struct {
289289
ID uint `json:"id" validate:"required"`
290290
Name string `json:"name" validate:"required"`
291291
}
292+
293+
type CrossSiteAccessOp struct {
294+
WebsiteID uint `json:"websiteID" validate:"required"`
295+
Operation string `json:"operation" validate:"required,oneof=Enable Disable"`
296+
}

agent/app/dto/response/website.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type WebsiteDTO struct {
1515
RuntimeName string `json:"runtimeName"`
1616
RuntimeType string `json:"runtimeType"`
1717
SiteDir string `json:"siteDir"`
18+
OpenBaseDir bool `json:"openBaseDir"`
1819
}
1920

2021
type WebsiteRes struct {

agent/app/service/website.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ type IWebsiteService interface {
124124
GetWebsiteResource(websiteID uint) ([]response.Resource, error)
125125
ListDatabases() ([]response.Database, error)
126126
ChangeDatabase(req request.ChangeDatabase) error
127+
128+
OperateCrossSiteAccess(req request.CrossSiteAccessOp) error
127129
}
128130

129131
func NewIWebsiteService() IWebsiteService {
@@ -428,7 +430,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
428430
return err
429431
}
430432
if runtime.Type == constant.RuntimePHP && runtime.Resource == constant.ResourceAppstore {
431-
createPHPConfig(website)
433+
createOpenBasedirConfig(website)
432434
}
433435
}
434436
tx, ctx := helper.GetTxAndContext()
@@ -573,6 +575,9 @@ func (w WebsiteService) GetWebsite(id uint) (response.WebsiteDTO, error) {
573575
}
574576
res.RuntimeType = runtime.Type
575577
res.RuntimeName = runtime.Name
578+
if runtime.Type == constant.RuntimePHP {
579+
res.OpenBaseDir = files.NewFileOp().Stat(path.Join(GetSitePath(website, SiteIndexDir), ".user.ini"))
580+
}
576581
}
577582
return res, nil
578583
}
@@ -3278,3 +3283,18 @@ func (w WebsiteService) ChangeDatabase(req request.ChangeDatabase) error {
32783283
website.DbType = req.DatabaseType
32793284
return websiteRepo.Save(context.Background(), &website)
32803285
}
3286+
3287+
func (w WebsiteService) OperateCrossSiteAccess(req request.CrossSiteAccessOp) error {
3288+
website, err := websiteRepo.GetFirst(repo.WithByID(req.WebsiteID))
3289+
if err != nil {
3290+
return err
3291+
}
3292+
if req.Operation == constant.StatusEnable {
3293+
createOpenBasedirConfig(&website)
3294+
}
3295+
if req.Operation == constant.StatusDisable {
3296+
fileOp := files.NewFileOp()
3297+
return fileOp.DeleteFile(path.Join(GetSitePath(website, SiteIndexDir), ".user.ini"))
3298+
}
3299+
return nil
3300+
}

agent/app/service/website_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ func createAllWebsitesWAFConfig(websites []model.Website) error {
349349
return nil
350350
}
351351

352-
func createPHPConfig(website *model.Website) {
352+
func createOpenBasedirConfig(website *model.Website) {
353353
fileOp := files.NewFileOp()
354354
userIniPath := path.Join(GetSitePath(*website, SiteIndexDir), ".user.ini")
355355
_ = fileOp.CreateFile(userIniPath)
356-
_ = fileOp.SaveFile(userIniPath, fmt.Sprintf("open_basedir=/www/sites/%s/index", website.Alias), 0644)
356+
_ = fileOp.SaveFile(userIniPath, fmt.Sprintf("open_basedir=/www/sites/%s/index:/tmp/", website.Alias), 0644)
357357
}
358358

359359
func createWafConfig(website *model.Website, domains []model.WebsiteDomain) error {

agent/router/ro_website.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,7 @@ func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) {
8484
websiteRouter.GET("/resource/:id", baseApi.GetWebsiteResource)
8585
websiteRouter.GET("/databases", baseApi.GetWebsiteDatabase)
8686
websiteRouter.POST("/databases", baseApi.ChangeWebsiteDatabase)
87+
88+
websiteRouter.POST("/crosssite", baseApi.OperateCrossSiteAccess)
8789
}
8890
}

frontend/src/api/interface/website.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export namespace Website {
3636
appName: string;
3737
runtimeName: string;
3838
runtimeType: string;
39+
openBaseDir: boolean;
3940
}
4041
export interface WebsiteRes extends CommonModel {
4142
protocol: string;
@@ -646,4 +647,9 @@ export namespace Website {
646647
databaseID: number;
647648
databaseType: string;
648649
}
650+
651+
export interface CrossSiteAccessOp {
652+
websiteID: number;
653+
operation: string;
654+
}
649655
}

frontend/src/api/modules/website.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,7 @@ export const operateCustomRewrite = (req: Website.CustomRewirte) => {
343343
export const listCustomRewrite = () => {
344344
return http.get<string[]>(`/websites/rewrite/custom`);
345345
};
346+
347+
export const operateCrossSiteAccess = (req: Website.CrossSiteAccessOp) => {
348+
return http.post(`/websites/crosssite`, req);
349+
};

frontend/src/lang/modules/en.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,9 @@ const message = {
24762476
useProxy: 'Use Proxy',
24772477
useProxyHelper: 'Use the proxy server address in the panel settings',
24782478
westCN: 'West Digital',
2479+
openBaseDir: 'Prevent Cross-Site Attacks',
2480+
openBaseDirHelper:
2481+
'open_basedir is used to restrict the PHP file access path, which helps prevent cross-site access and enhance security',
24792482
},
24802483
php: {
24812484
short_open_tag: 'Short tag support',

frontend/src/lang/modules/ja.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,9 @@ const message = {
23822382
useProxy: 'プロキシを使用',
23832383
useProxyHelper: 'パネル設定のプロキシサーバーアドレスを使用',
23842384
westCN: '西部デジタル',
2385+
openBaseDir: 'クロスサイト攻撃を防ぐ',
2386+
openBaseDirHelper:
2387+
'open_basedir は PHP ファイルのアクセスパスを制限し、クロスサイトアクセスを防ぎセキュリティを向上させるために使用されます',
23852388
},
23862389
php: {
23872390
short_open_tag: '短いタグサポート',

0 commit comments

Comments
 (0)