TUN-7628: Correct Host parsing for Access

Will no longer provide full hostname with path from provided
`--hostname` flag for cloudflared access to the Host header field.
This addresses certain issues caught from a security fix in go
1.19.11 and 1.20.6 in the net/http URL parsing.
This commit is contained in:
Devin Carr
2023-07-25 09:33:11 -07:00
parent bfeaa3418d
commit 81fe0bd12b
85 changed files with 22873 additions and 4442 deletions

View File

@@ -140,3 +140,30 @@ func (s *Service) RecoveryCommand() (string, error) {
p := (*windows.SERVICE_FAILURE_ACTIONS)(unsafe.Pointer(&b[0]))
return windows.UTF16PtrToString(p.Command), nil
}
// SetRecoveryActionsOnNonCrashFailures sets the failure actions flag. If the
// flag is set to false, recovery actions will only be performed if the service
// terminates without reporting a status of SERVICE_STOPPED. If the flag is set
// to true, recovery actions are also perfomed if the service stops with a
// nonzero exit code.
func (s *Service) SetRecoveryActionsOnNonCrashFailures(flag bool) error {
var setting windows.SERVICE_FAILURE_ACTIONS_FLAG
if flag {
setting.FailureActionsOnNonCrashFailures = 1
}
return windows.ChangeServiceConfig2(s.Handle, windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, (*byte)(unsafe.Pointer(&setting)))
}
// RecoveryActionsOnNonCrashFailures returns the current value of the failure
// actions flag. If the flag is set to false, recovery actions will only be
// performed if the service terminates without reporting a status of
// SERVICE_STOPPED. If the flag is set to true, recovery actions are also
// perfomed if the service stops with a nonzero exit code.
func (s *Service) RecoveryActionsOnNonCrashFailures() (bool, error) {
b, err := s.queryServiceConfig2(windows.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG)
if err != nil {
return false, err
}
p := (*windows.SERVICE_FAILURE_ACTIONS_FLAG)(unsafe.Pointer(&b[0]))
return p.FailureActionsOnNonCrashFailures != 0, nil
}

View File

@@ -15,8 +15,6 @@ import (
"golang.org/x/sys/windows/svc"
)
// TODO(brainman): Use EnumDependentServices to enumerate dependent services.
// Service is used to access Windows service.
type Service struct {
Name string
@@ -47,17 +45,25 @@ func (s *Service) Start(args ...string) error {
return windows.StartService(s.Handle, uint32(len(args)), p)
}
// Control sends state change request c to the service s.
// Control sends state change request c to the service s. It returns the most
// recent status the service reported to the service control manager, and an
// error if the state change request was not accepted.
// Note that the returned service status is only set if the status change
// request succeeded, or if it failed with error ERROR_INVALID_SERVICE_CONTROL,
// ERROR_SERVICE_CANNOT_ACCEPT_CTRL, or ERROR_SERVICE_NOT_ACTIVE.
func (s *Service) Control(c svc.Cmd) (svc.Status, error) {
var t windows.SERVICE_STATUS
err := windows.ControlService(s.Handle, uint32(c), &t)
if err != nil {
if err != nil &&
err != windows.ERROR_INVALID_SERVICE_CONTROL &&
err != windows.ERROR_SERVICE_CANNOT_ACCEPT_CTRL &&
err != windows.ERROR_SERVICE_NOT_ACTIVE {
return svc.Status{}, err
}
return svc.Status{
State: svc.State(t.CurrentState),
Accepts: svc.Accepted(t.ControlsAccepted),
}, nil
}, err
}
// Query returns current status of service s.
@@ -76,3 +82,44 @@ func (s *Service) Query() (svc.Status, error) {
ServiceSpecificExitCode: t.ServiceSpecificExitCode,
}, nil
}
// ListDependentServices returns the names of the services dependent on service s, which match the given status.
func (s *Service) ListDependentServices(status svc.ActivityStatus) ([]string, error) {
var bytesNeeded, returnedServiceCount uint32
var services []windows.ENUM_SERVICE_STATUS
for {
var servicesPtr *windows.ENUM_SERVICE_STATUS
if len(services) > 0 {
servicesPtr = &services[0]
}
allocatedBytes := uint32(len(services)) * uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{}))
err := windows.EnumDependentServices(s.Handle, uint32(status), servicesPtr, allocatedBytes, &bytesNeeded,
&returnedServiceCount)
if err == nil {
break
}
if err != syscall.ERROR_MORE_DATA {
return nil, err
}
if bytesNeeded <= allocatedBytes {
return nil, err
}
// ERROR_MORE_DATA indicates the provided buffer was too small, run the call again after resizing the buffer
requiredSliceLen := bytesNeeded / uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{}))
if bytesNeeded%uint32(unsafe.Sizeof(windows.ENUM_SERVICE_STATUS{})) != 0 {
requiredSliceLen += 1
}
services = make([]windows.ENUM_SERVICE_STATUS, requiredSliceLen)
}
if returnedServiceCount == 0 {
return nil, nil
}
// The slice mutated by EnumDependentServices may have a length greater than returnedServiceCount, any elements
// past that should be ignored.
var dependents []string
for i := 0; i < int(returnedServiceCount); i++ {
dependents = append(dependents, windows.UTF16PtrToString(services[i].ServiceName))
}
return dependents, nil
}

View File

@@ -68,6 +68,15 @@ const (
AcceptPreShutdown = Accepted(windows.SERVICE_ACCEPT_PRESHUTDOWN)
)
// ActivityStatus allows for services to be selected based on active and inactive categories of service state.
type ActivityStatus uint32
const (
Active = ActivityStatus(windows.SERVICE_ACTIVE)
Inactive = ActivityStatus(windows.SERVICE_INACTIVE)
AnyActivity = ActivityStatus(windows.SERVICE_STATE_ALL)
)
// Status combines State and Accepted commands to fully describe running service.
type Status struct {
State State