daemon: disallow container port 0

Signed-off-by: Albin Kerouanton <albin.kerouanton@docker.com>
This commit is contained in:
Albin Kerouanton
2025-12-11 11:07:42 +01:00
parent c64b781df2
commit 43780fe40c
2 changed files with 13 additions and 1 deletions

View File

@@ -335,7 +335,7 @@ func validateHealthCheck(healthConfig *containertypes.HealthConfig) error {
func validatePortBindings(ports networktypes.PortMap) error {
for port := range ports {
if !port.IsValid() {
if !port.IsValid() || port.Num() == 0 {
return errors.Errorf("invalid port specification: %q", port.String())
}

View File

@@ -300,6 +300,18 @@ func TestValidateContainerIsolation(t *testing.T) {
assert.Check(t, is.Error(err, "invalid isolation 'invalid' on "+runtime.GOOS))
}
func TestInvalidContainerPort0(t *testing.T) {
d := Daemon{}
hc := containertypes.HostConfig{
PortBindings: map[network.Port][]network.PortBinding{
network.MustParsePort("0/tcp"): {},
},
}
_, err := d.verifyContainerSettings(&configStore{}, &hc, nil, false)
assert.Error(t, err, `invalid port specification: "0/tcp"`)
}
func TestFindNetworkErrorType(t *testing.T) {
d := Daemon{}
_, err := d.FindNetwork("fakeNet")