2019-11-24 20:27:59 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
// WARNING: This scenario is intended to be used with TLS enabled in the authelia backend.
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
type BackendProtectionScenario struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBackendProtectionScenario() *BackendProtectionScenario {
|
|
|
|
return &BackendProtectionScenario{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BackendProtectionScenario) AssertRequestStatusCode(method, url string, expectedStatusCode int) {
|
|
|
|
s.Run(url, func() {
|
|
|
|
req, err := http.NewRequest(method, url, nil)
|
|
|
|
s.Assert().NoError(err)
|
|
|
|
|
|
|
|
tr := &http.Transport{
|
2020-05-05 07:57:30 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec // Needs to be enabled in suites. Not used in production.
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := client.Do(req)
|
|
|
|
s.Assert().NoError(err)
|
2021-03-11 07:36:58 +00:00
|
|
|
s.Assert().Equal(expectedStatusCode, res.StatusCode)
|
2019-11-24 20:27:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BackendProtectionScenario) TestProtectionOfBackendEndpoints() {
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/totp", AutheliaBaseURL), 403)
|
2022-03-03 11:20:43 +00:00
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/webauthn/assertion", AutheliaBaseURL), 403)
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/webauthn/attestation", AutheliaBaseURL), 403)
|
2019-12-07 17:14:26 +00:00
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/user/info/2fa_method", AutheliaBaseURL), 403)
|
|
|
|
|
|
|
|
s.AssertRequestStatusCode("GET", fmt.Sprintf("%s/api/user/info", AutheliaBaseURL), 403)
|
2020-06-21 13:40:37 +00:00
|
|
|
s.AssertRequestStatusCode("GET", fmt.Sprintf("%s/api/configuration", AutheliaBaseURL), 403)
|
2019-11-24 20:27:59 +00:00
|
|
|
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/totp/identity/start", AutheliaBaseURL), 403)
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/totp/identity/finish", AutheliaBaseURL), 403)
|
2022-03-03 11:20:43 +00:00
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/webauthn/identity/start", AutheliaBaseURL), 403)
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/secondfactor/webauthn/identity/finish", AutheliaBaseURL), 403)
|
2019-11-24 20:27:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:36:58 +00:00
|
|
|
func (s *BackendProtectionScenario) TestInvalidEndpointsReturn404() {
|
|
|
|
s.AssertRequestStatusCode("GET", fmt.Sprintf("%s/api/not_existing", AutheliaBaseURL), 404)
|
|
|
|
s.AssertRequestStatusCode("HEAD", fmt.Sprintf("%s/api/not_existing", AutheliaBaseURL), 404)
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/not_existing", AutheliaBaseURL), 404)
|
|
|
|
|
|
|
|
s.AssertRequestStatusCode("GET", fmt.Sprintf("%s/api/not_existing/second", AutheliaBaseURL), 404)
|
|
|
|
s.AssertRequestStatusCode("HEAD", fmt.Sprintf("%s/api/not_existing/second", AutheliaBaseURL), 404)
|
|
|
|
s.AssertRequestStatusCode("POST", fmt.Sprintf("%s/api/not_existing/second", AutheliaBaseURL), 404)
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
func TestRunBackendProtection(t *testing.T) {
|
2021-03-14 07:08:26 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping suite test in short mode")
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:27:59 +00:00
|
|
|
suite.Run(t, NewBackendProtectionScenario())
|
|
|
|
}
|