Skip to content

Commit 39e08eb

Browse files
authored
Merge pull request #3685 from filipochnik/abs-path
Prevent absolute URLs in checks paths
2 parents 12ce2c9 + d234641 commit 39e08eb

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

nomad/structs/structs.go

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"io"
1414
"net"
15+
"net/url"
1516
"os"
1617
"path/filepath"
1718
"reflect"
@@ -2937,6 +2938,13 @@ func (sc *ServiceCheck) validate() error {
29372938
if sc.Path == "" {
29382939
return fmt.Errorf("http type must have a valid http path")
29392940
}
2941+
url, err := url.Parse(sc.Path)
2942+
if err != nil {
2943+
return fmt.Errorf("http type must have a valid http path")
2944+
}
2945+
if url.IsAbs() {
2946+
return fmt.Errorf("http type must have a relative http path")
2947+
}
29402948

29412949
case ServiceCheckScript:
29422950
if sc.Command == "" {

nomad/structs/structs_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,37 @@ func TestTask_Validate_Service_Check(t *testing.T) {
12301230
if err != nil {
12311231
t.Fatalf("err: %v", err)
12321232
}
1233+
1234+
check2 := ServiceCheck{
1235+
Name: "check-name-2",
1236+
Type: ServiceCheckHTTP,
1237+
Interval: 10 * time.Second,
1238+
Timeout: 2 * time.Second,
1239+
Path: "/foo/bar",
1240+
}
1241+
1242+
err = check2.validate()
1243+
if err != nil {
1244+
t.Fatalf("err: %v", err)
1245+
}
1246+
1247+
check2.Path = ""
1248+
err = check2.validate()
1249+
if err == nil {
1250+
t.Fatal("Expected an error")
1251+
}
1252+
if !strings.Contains(err.Error(), "valid http path") {
1253+
t.Fatalf("err: %v", err)
1254+
}
1255+
1256+
check2.Path = "http://www.example.com"
1257+
err = check2.validate()
1258+
if err == nil {
1259+
t.Fatal("Expected an error")
1260+
}
1261+
if !strings.Contains(err.Error(), "relative http path") {
1262+
t.Fatalf("err: %v", err)
1263+
}
12331264
}
12341265

12351266
// TestTask_Validate_Service_Check_AddressMode asserts that checks do not

website/source/api/json-jobs.html.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ The `Task` object supports the following keys:
412412
- `Path`: The path of the HTTP endpoint which Consul will query to query
413413
the health of a service if the type of the check is `http`. Nomad
414414
will add the IP of the service and the port, users are only required
415-
to add the relative URL of the health check endpoint.
415+
to add the relative URL of the health check endpoint. Absolute paths
416+
are not allowed.
416417

417418
- `Protocol`: This indicates the protocol for the HTTP checks. Valid
418419
options are `http` and `https`. We default it to `http`.

0 commit comments

Comments
 (0)