Skip to content

Commit e91ea6b

Browse files
committed
fix(wip): uncontrolled data used in path expression
1 parent 226827f commit e91ea6b

File tree

7 files changed

+24
-7
lines changed

7 files changed

+24
-7
lines changed

api/certificate/certificate.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/0xJacky/Nginx-UI/api"
55
"github.com/0xJacky/Nginx-UI/internal/cert"
66
"github.com/0xJacky/Nginx-UI/internal/cosy"
7+
"github.com/0xJacky/Nginx-UI/internal/helper"
78
"github.com/0xJacky/Nginx-UI/internal/nginx"
89
"github.com/0xJacky/Nginx-UI/internal/notification"
910
"github.com/0xJacky/Nginx-UI/model"
@@ -25,7 +26,8 @@ type APICertificate struct {
2526
func Transformer(certModel *model.Cert) (certificate *APICertificate) {
2627
var sslCertificationBytes, sslCertificationKeyBytes []byte
2728
var certificateInfo *cert.Info
28-
if certModel.SSLCertificatePath != "" {
29+
if certModel.SSLCertificatePath != "" &&
30+
helper.IsUnderDirectory(certModel.SSLCertificatePath, nginx.GetConfPath()) {
2931
if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
3032
sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
3133
if !cert.IsCertificate(string(sslCertificationBytes)) {
@@ -36,7 +38,8 @@ func Transformer(certModel *model.Cert) (certificate *APICertificate) {
3638
certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
3739
}
3840

39-
if certModel.SSLCertificateKeyPath != "" {
41+
if certModel.SSLCertificateKeyPath != "" &&
42+
helper.IsUnderDirectory(certModel.SSLCertificateKeyPath, nginx.GetConfPath()) {
4043
if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
4144
sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
4245
if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) {

api/config/modify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func EditConfig(c *gin.Context) {
4747
return
4848
}
4949

50-
if _, err := os.Stat(path); os.IsNotExist(err) {
50+
if !helper.FileExists(path) {
5151
c.JSON(http.StatusNotFound, gin.H{
5252
"message": "file not found",
5353
})

app/src/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"2.0.0-beta.29","build_id":152,"total_build":356}
1+
{"version":"2.0.0-beta.29","build_id":154,"total_build":358,"status_hash":"4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945"}

internal/cert/cert_info.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cert
33
import (
44
"crypto/x509"
55
"encoding/pem"
6+
"github.com/0xJacky/Nginx-UI/internal/helper"
7+
"github.com/0xJacky/Nginx-UI/internal/nginx"
68
"github.com/pkg/errors"
79
"os"
810
"time"
@@ -16,6 +18,10 @@ type Info struct {
1618
}
1719

1820
func GetCertInfo(sslCertificatePath string) (info *Info, err error) {
21+
if !helper.IsUnderDirectory(sslCertificatePath, nginx.GetConfPath()) {
22+
err = errors.New("ssl certificate path is not under the nginx conf path")
23+
return
24+
}
1925
certData, err := os.ReadFile(sslCertificatePath)
2026
if err != nil {
2127
err = errors.Wrap(err, "error read certificate")

internal/cert/payload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ func (c *ConfigPayload) GetKeyType() certcrypto.KeyType {
5353

5454
func (c *ConfigPayload) mkCertificateDir() (err error) {
5555
dir := c.getCertificateDirPath()
56-
if _, err = os.Stat(dir); os.IsNotExist(err) {
56+
if !helper.FileExists(dir) {
5757
err = os.MkdirAll(dir, 0755)
5858
if err == nil {
5959
return nil
6060
}
6161
}
6262

63-
// For windows, replace # with * (issue #403)
64-
c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "#", "*")
63+
// For windows, replace * with # (issue #403)
64+
c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "*", "#")
6565
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
6666
err = os.MkdirAll(c.CertificateDir, 0755)
6767
if err == nil {

internal/chatbot/context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func (c *includeContext) extractIncludes(filename string) {
3333
return
3434
}
3535

36+
if !helper.IsUnderDirectory(filename, nginx.GetConfPath()) {
37+
logger.Error("File is not under the nginx conf path: ", filename)
38+
return
39+
}
40+
3641
// Read the file content
3742
content, err := os.ReadFile(filename)
3843
if err != nil {

internal/helper/tar.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10+
"strings"
1011
)
1112

1213
func UnTar(dst, src string) (err error) {
@@ -37,6 +38,8 @@ func UnTar(dst, src string) (err error) {
3738
return errors.Wrap(err, "unTar tr.Next() error")
3839
case hdr == nil:
3940
return
41+
case strings.Contains(hdr.Name, ".."):
42+
return
4043
}
4144

4245
dstFileDir := filepath.Join(dst, hdr.Name)

0 commit comments

Comments
 (0)