Ver código fonte

daemon/logger/fluentd: validate path element

fix some missing validation: the driver was silently ignoring path elements
in some cases, and expecting a host (not an URL), and for unix sockets did
not validate if a path was specified.

For the latter case, we should have a fix in the upstream driver, as it
uses an empty path as default path for the socket (`defaultSocketPath`),
and performs no validation.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 anos atrás
pai
commit
0f40aefccd

+ 11 - 0
daemon/logger/fluentd/fluentd.go

@@ -277,6 +277,10 @@ func parseAddress(address string) (*location, error) {
 		}, nil
 		}, nil
 	}
 	}
 
 
+	if !strings.Contains(address, "://") {
+		address = defaultProtocol + "://" + address
+	}
+
 	protocol := defaultProtocol
 	protocol := defaultProtocol
 	if urlutil.IsTransportURL(address) {
 	if urlutil.IsTransportURL(address) {
 		addr, err := url.Parse(address)
 		addr, err := url.Parse(address)
@@ -285,6 +289,9 @@ func parseAddress(address string) (*location, error) {
 		}
 		}
 		// unix and unixgram socket
 		// unix and unixgram socket
 		if addr.Scheme == "unix" || addr.Scheme == "unixgram" {
 		if addr.Scheme == "unix" || addr.Scheme == "unixgram" {
+			if strings.TrimLeft(addr.Path, "/") == "" {
+				return nil, errors.New("path is empty")
+			}
 			return &location{
 			return &location{
 				protocol: addr.Scheme,
 				protocol: addr.Scheme,
 				host:     "",
 				host:     "",
@@ -295,6 +302,10 @@ func parseAddress(address string) (*location, error) {
 		// tcp|udp
 		// tcp|udp
 		protocol = addr.Scheme
 		protocol = addr.Scheme
 		address = addr.Host
 		address = addr.Host
+
+		if addr.Path != "" {
+			return nil, errors.New("should not contain a path element")
+		}
 	}
 	}
 
 
 	host, port, err := net.SplitHostPort(address)
 	host, port, err := net.SplitHostPort(address)

+ 9 - 1
daemon/logger/fluentd/fluentd_test.go

@@ -102,7 +102,7 @@ func TestValidateLogOptAddress(t *testing.T) {
 		},
 		},
 		{
 		{
 			addr:        "corrupted:c",
 			addr:        "corrupted:c",
-			expectedErr: "invalid syntax",
+			expectedErr: "invalid port",
 		},
 		},
 		{
 		{
 			addr:        "tcp://example.com:port",
 			addr:        "tcp://example.com:port",
@@ -112,6 +112,10 @@ func TestValidateLogOptAddress(t *testing.T) {
 			addr:        "tcp://example.com:-1",
 			addr:        "tcp://example.com:-1",
 			expectedErr: "invalid port",
 			expectedErr: "invalid port",
 		},
 		},
+		{
+			addr:        "unix://",
+			expectedErr: "path is empty",
+		},
 		{
 		{
 			addr: "unix:///some/socket.sock",
 			addr: "unix:///some/socket.sock",
 			expected: location{
 			expected: location{
@@ -136,6 +140,10 @@ func TestValidateLogOptAddress(t *testing.T) {
 			address := tc.addr + path
 			address := tc.addr + path
 			t.Run(address, func(t *testing.T) {
 			t.Run(address, func(t *testing.T) {
 				err := ValidateLogOpt(map[string]string{addressKey: address})
 				err := ValidateLogOpt(map[string]string{addressKey: address})
+				if path != "" {
+					assert.ErrorContains(t, err, "should not contain a path element")
+					return
+				}
 				if tc.expectedErr != "" {
 				if tc.expectedErr != "" {
 					assert.ErrorContains(t, err, tc.expectedErr)
 					assert.ErrorContains(t, err, tc.expectedErr)
 					return
 					return