Skip to content

Commit cca7690

Browse files
authored
Security: Harden socket creation and validate error code input. (#13765)
1 parent 4c87d58 commit cca7690

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

internal/ingress/annotations/customhttperrors/main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package customhttperrors
1818

1919
import (
20+
"fmt"
2021
"regexp"
2122
"strconv"
2223
"strings"
@@ -72,10 +73,17 @@ func (e customhttperrors) Parse(ing *networking.Ingress) (interface{}, error) {
7273
cSplit := strings.Split(c, ",")
7374
codes := make([]int, 0, len(cSplit))
7475
for _, i := range cSplit {
75-
num, err := strconv.Atoi(i)
76+
// Trim whitespace to handle "404, 500" format
77+
trimmed := strings.TrimSpace(i)
78+
if trimmed == "" {
79+
continue
80+
}
81+
82+
num, err := strconv.Atoi(trimmed)
7683
if err != nil {
77-
return nil, err
84+
return nil, fmt.Errorf("invalid HTTP status code %q: %w", trimmed, err)
7885
}
86+
7987
codes = append(codes, num)
8088
}
8189

internal/ingress/metric/collectors/socket.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ var requestTags = []string{
102102
// the ingress watch namespace and class used by the controller
103103
func NewSocketCollector(pod, namespace, class string, metricsPerHost, metricsPerUndefinedHost, reportStatusClasses bool, buckets HistogramBuckets, bucketFactor float64, maxBuckets uint32, excludeMetrics []string) (*SocketCollector, error) {
104104
socket := "/tmp/nginx/prometheus-nginx.socket"
105+
106+
// Ensure the directory exists
107+
if err := os.MkdirAll("/tmp/nginx", 0o755); err != nil {
108+
return nil, fmt.Errorf("failed to create socket directory: %w", err)
109+
}
110+
105111
// unix sockets must be unlink()ed before being used
106112
//nolint:errcheck // Ignore unlink error
107113
_ = syscall.Unlink(socket)
@@ -111,7 +117,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, metricsPer
111117
return nil, err
112118
}
113119

114-
err = os.Chmod(socket, 0o777) // #nosec
120+
err = os.Chmod(socket, 0o660) // Read/write for owner and group only - more secure than 0o777
115121
if err != nil {
116122
return nil, err
117123
}

0 commit comments

Comments
 (0)