Skip to content
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

Support CIDR entries in DeniedPortForwardingRemoteIPs list #481

Open
wants to merge 1 commit into
base: mainline
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions agent/session/plugins/port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,17 @@ func (p *PortPlugin) validateParameters(portParameters PortParameters, config ag
// Port forwarding to IMDS, VPC DNS, and local IP address is not allowed
hostIPAddress := net.ParseIP(host)
for _, address := range append(appConfig.Mgs.DeniedPortForwardingRemoteIPs, dnsAddress...) {
if hostIPAddress.Equal(net.ParseIP(address)) {
return errors.New(fmt.Sprintf("Forwarding to IP address %s is forbidden.", portParameters.Host))
_, network, err := net.ParseCIDR(address)
if err != nil {
// Not a CIDR
if hostIPAddress.Equal(net.ParseIP(address)) {
return errors.New(fmt.Sprintf("Forwarding to IP address %s is forbidden.", portParameters.Host))
}
} else {
// CIDR
if network.Contains(hostIPAddress) {
return errors.New(fmt.Sprintf("Forwarding to IP address %s is forbidden.", portParameters.Host))
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions agent/session/plugins/port/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (suite *PortTestSuite) TestValidateParametersWhenDefaultDenylistHostNotAllo
mockContext := &context.Mock{}
suite.plugin.context = mockContext

mockContext.On("AppConfig").Return(appconfig.SsmagentConfig{Mgs: appconfig.MgsConfig{DeniedPortForwardingRemoteIPs: []string{"169.254.169.254", "fd00:ec2::254", "169.254.169.253", "fd00:ec2::253", "169.254.169.123", "169.254.169.250"}}})
mockContext.On("AppConfig").Return(appconfig.SsmagentConfig{Mgs: appconfig.MgsConfig{DeniedPortForwardingRemoteIPs: []string{"169.254.169.254", "fd00:ec2::254", "169.254.169.253", "fd00:ec2::253", "169.254.169.123", "169.254.169.250", "10.0.0.0/16"}}})
mockContext.On("Log").Return(mockLog)

err := suite.plugin.validateParameters(PortParameters{PortNumber: "80", Host: "169.254.169.253"}, configuration)
Expand All @@ -349,19 +349,22 @@ func (suite *PortTestSuite) TestValidateParametersWhenDefaultDenylistHostNotAllo
assert.Contains(suite.T(), err.Error(), "Forwarding to IP address 169.254.169.250 is forbidden.")
err = suite.plugin.validateParameters(PortParameters{PortNumber: "80", Host: "169.254.169.123"}, configuration)
assert.Contains(suite.T(), err.Error(), "Forwarding to IP address 169.254.169.123 is forbidden.")

err = suite.plugin.validateParameters(PortParameters{PortNumber: "80", Host: "10.0.15.24"}, configuration)
assert.Contains(suite.T(), err.Error(), "Forwarding to IP address 10.0.15.24 is forbidden.")
mockContext.AssertExpectations(suite.T())
}

func (suite *PortTestSuite) TestValidateParametersWhenValidHostAndPort() {
mockContext := &context.Mock{}
suite.plugin.context = mockContext

mockContext.On("AppConfig").Return(appconfig.SsmagentConfig{Mgs: appconfig.MgsConfig{DeniedPortForwardingRemoteIPs: []string{"169.254.169.254", "fd00:ec2::254", "169.254.169.253", "fd00:ec2::253"}}})
mockContext.On("AppConfig").Return(appconfig.SsmagentConfig{Mgs: appconfig.MgsConfig{DeniedPortForwardingRemoteIPs: []string{"169.254.169.254", "fd00:ec2::254", "169.254.169.253", "fd00:ec2::253", "10.0.0.0/16"}}})
mockContext.On("Log").Return(mockLog)

err := suite.plugin.validateParameters(PortParameters{PortNumber: "80", Host: "127.0.0.1"}, configuration)
assert.Nil(suite.T(), err)
err = suite.plugin.validateParameters(PortParameters{PortNumber: "80", Host: "10.1.0.4"}, configuration)
assert.Nil(suite.T(), err)

mockContext.AssertExpectations(suite.T())
}
Expand Down