Browse code

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 authored on 2022/04/10 19:38:45
Showing 2 changed files
... ...
@@ -277,6 +277,10 @@ func parseAddress(address string) (*location, error) {
277 277
 		}, nil
278 278
 	}
279 279
 
280
+	if !strings.Contains(address, "://") {
281
+		address = defaultProtocol + "://" + address
282
+	}
283
+
280 284
 	protocol := defaultProtocol
281 285
 	if urlutil.IsTransportURL(address) {
282 286
 		addr, err := url.Parse(address)
... ...
@@ -285,6 +289,9 @@ func parseAddress(address string) (*location, error) {
285 285
 		}
286 286
 		// unix and unixgram socket
287 287
 		if addr.Scheme == "unix" || addr.Scheme == "unixgram" {
288
+			if strings.TrimLeft(addr.Path, "/") == "" {
289
+				return nil, errors.New("path is empty")
290
+			}
288 291
 			return &location{
289 292
 				protocol: addr.Scheme,
290 293
 				host:     "",
... ...
@@ -295,6 +302,10 @@ func parseAddress(address string) (*location, error) {
295 295
 		// tcp|udp
296 296
 		protocol = addr.Scheme
297 297
 		address = addr.Host
298
+
299
+		if addr.Path != "" {
300
+			return nil, errors.New("should not contain a path element")
301
+		}
298 302
 	}
299 303
 
300 304
 	host, port, err := net.SplitHostPort(address)
... ...
@@ -102,7 +102,7 @@ func TestValidateLogOptAddress(t *testing.T) {
102 102
 		},
103 103
 		{
104 104
 			addr:        "corrupted:c",
105
-			expectedErr: "invalid syntax",
105
+			expectedErr: "invalid port",
106 106
 		},
107 107
 		{
108 108
 			addr:        "tcp://example.com:port",
... ...
@@ -113,6 +113,10 @@ func TestValidateLogOptAddress(t *testing.T) {
113 113
 			expectedErr: "invalid port",
114 114
 		},
115 115
 		{
116
+			addr:        "unix://",
117
+			expectedErr: "path is empty",
118
+		},
119
+		{
116 120
 			addr: "unix:///some/socket.sock",
117 121
 			expected: location{
118 122
 				protocol: "unix",
... ...
@@ -136,6 +140,10 @@ func TestValidateLogOptAddress(t *testing.T) {
136 136
 			address := tc.addr + path
137 137
 			t.Run(address, func(t *testing.T) {
138 138
 				err := ValidateLogOpt(map[string]string{addressKey: address})
139
+				if path != "" {
140
+					assert.ErrorContains(t, err, "should not contain a path element")
141
+					return
142
+				}
139 143
 				if tc.expectedErr != "" {
140 144
 					assert.ErrorContains(t, err, tc.expectedErr)
141 145
 					return