Skip to content

Security: Harden socket creation and validate error code input. #13765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/ingress/annotations/customhttperrors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package customhttperrors

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -72,10 +73,17 @@ func (e customhttperrors) Parse(ing *networking.Ingress) (interface{}, error) {
cSplit := strings.Split(c, ",")
codes := make([]int, 0, len(cSplit))
for _, i := range cSplit {
num, err := strconv.Atoi(i)
// Trim whitespace to handle "404, 500" format
trimmed := strings.TrimSpace(i)
if trimmed == "" {
continue
}

num, err := strconv.Atoi(trimmed)
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid HTTP status code %q: %w", trimmed, err)
}

codes = append(codes, num)
}

Expand Down
8 changes: 7 additions & 1 deletion internal/ingress/metric/collectors/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ var requestTags = []string{
// the ingress watch namespace and class used by the controller
func NewSocketCollector(pod, namespace, class string, metricsPerHost, metricsPerUndefinedHost, reportStatusClasses bool, buckets HistogramBuckets, bucketFactor float64, maxBuckets uint32, excludeMetrics []string) (*SocketCollector, error) {
socket := "/tmp/nginx/prometheus-nginx.socket"

// Ensure the directory exists
if err := os.MkdirAll("/tmp/nginx", 0o755); err != nil {
return nil, fmt.Errorf("failed to create socket directory: %w", err)
}

// unix sockets must be unlink()ed before being used
//nolint:errcheck // Ignore unlink error
_ = syscall.Unlink(socket)
Expand All @@ -111,7 +117,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost, metricsPer
return nil, err
}

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