Skip to content

Commit da47ac4

Browse files
committed
fix: validate file path
Signed-off-by: n4mlz <[email protected]>
1 parent cdb74dd commit da47ac4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pkg/certificate/service.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package certificate
1717
import (
1818
"context"
1919
"fmt"
20+
"os"
2021
"sync"
2122
"time"
2223

@@ -71,6 +72,46 @@ func New(ctx context.Context, idCfg *config.IdentityConfig) (daemon.Daemon, erro
7172
var localFileKeyPEM []byte
7273
var localFileIdentity *InstanceIdentity
7374

75+
// validate files
76+
isValidFiles := func() error {
77+
isValidFile := func(path string) error {
78+
info, err := os.Stat(path)
79+
if err != nil {
80+
if os.IsNotExist(err) {
81+
return fmt.Errorf("file is not exist: %w", err)
82+
} else {
83+
return fmt.Errorf("unknown path error: %w", err)
84+
}
85+
}
86+
87+
mode := info.Mode().Perm()
88+
if mode&0200 == 0 {
89+
// no permition for writing file
90+
return fmt.Errorf("operation not permited: %w", err)
91+
}
92+
93+
return nil
94+
}
95+
96+
for _, certFile := range idCfg.ServiceCert.CopperArgos.Cert.Paths {
97+
err := isValidFile(certFile)
98+
if err != nil {
99+
return err
100+
}
101+
}
102+
for _, keyFile := range idCfg.ServiceCert.CopperArgos.Key.Paths {
103+
err := isValidFile(keyFile)
104+
if err != nil {
105+
return err
106+
}
107+
}
108+
err := isValidFile(idCfg.CaCertFile)
109+
if err != nil {
110+
return err
111+
}
112+
return nil
113+
}
114+
74115
// Write files to local file system
75116
writeFiles := func() error {
76117
w := util.NewWriter()
@@ -209,6 +250,11 @@ func New(ctx context.Context, idCfg *config.IdentityConfig) (daemon.Daemon, erro
209250
}
210251

211252
run := func() error {
253+
err := isValidFiles()
254+
if err != nil {
255+
return err
256+
}
257+
212258
if idCfg.ServiceCert.CopperArgos.Use {
213259
log.Infof("Attempting to request x509 certificate to identity provider[%s]...", idCfg.ServiceCert.CopperArgos.Provider)
214260

0 commit comments

Comments
 (0)