mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-16 13:17:19 +02:00
cmd/cli: use os.CreateTemp for symlink-safe temp file creation
Current code writes to a predictable path, which on systems without `fs.protected_symlinks` (e.g. embedded routers) could allow a local attacker with API compromise to perform symlink attacks.
This commit is contained in:
committed by
Cuong Manh Le
parent
c54ff701bd
commit
97e5e99b8d
+13
-5
@@ -2002,17 +2002,25 @@ func doValidateCdRemoteConfig(cdUID string, fatal bool) error {
|
|||||||
} else {
|
} else {
|
||||||
if errors.As(cfgErr, &viper.ConfigParseError{}) {
|
if errors.As(cfgErr, &viper.ConfigParseError{}) {
|
||||||
if configStr, _ := base64.StdEncoding.DecodeString(rc.Ctrld.CustomConfig); len(configStr) > 0 {
|
if configStr, _ := base64.StdEncoding.DecodeString(rc.Ctrld.CustomConfig); len(configStr) > 0 {
|
||||||
tmpDir := os.TempDir()
|
|
||||||
tmpConfFile := filepath.Join(tmpDir, "ctrld.toml")
|
|
||||||
errorLogged := false
|
errorLogged := false
|
||||||
// Write remote config to a temporary file to get details error.
|
// Write remote config to a uniquely named temporary file to get detailed error.
|
||||||
if we := os.WriteFile(tmpConfFile, configStr, 0600); we == nil {
|
if tmpFile, tmpErr := os.CreateTemp("", "ctrld-*.toml"); tmpErr == nil {
|
||||||
|
tmpConfFile := tmpFile.Name()
|
||||||
|
if _, err := tmpFile.Write(configStr); err != nil {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("failed to write temporary config file")
|
||||||
|
}
|
||||||
|
if err := tmpFile.Close(); err != nil {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("failed to save temporary config file")
|
||||||
|
|
||||||
|
}
|
||||||
if de := decoderErrorFromTomlFile(tmpConfFile); de != nil {
|
if de := decoderErrorFromTomlFile(tmpConfFile); de != nil {
|
||||||
row, col := de.Position()
|
row, col := de.Position()
|
||||||
mainLog.Load().Error().Msgf("failed to parse custom config at line: %d, column: %d, error: %s", row, col, de.Error())
|
mainLog.Load().Error().Msgf("failed to parse custom config at line: %d, column: %d, error: %s", row, col, de.Error())
|
||||||
errorLogged = true
|
errorLogged = true
|
||||||
}
|
}
|
||||||
_ = os.Remove(tmpConfFile)
|
if err := os.Remove(tmpConfFile); err != nil {
|
||||||
|
mainLog.Load().Error().Err(err).Msg("failed to remove temporary config file")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// If we could not log details error, emit what we have already got.
|
// If we could not log details error, emit what we have already got.
|
||||||
if !errorLogged {
|
if !errorLogged {
|
||||||
|
|||||||
Reference in New Issue
Block a user