2021-02-13 13:38:33 +02:00
package pagewriter
2021-02-07 00:05:45 +02:00
import (
2021-02-07 00:17:59 +02:00
"errors"
2021-02-07 00:05:45 +02:00
"html/template"
"io/ioutil"
"net/http/httptest"
2021-03-21 20:20:57 +02:00
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
2021-02-07 00:05:45 +02:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
2021-02-12 20:25:46 +02:00
var _ = Describe ( "Error Page Writer" , func ( ) {
var errorPage * errorPageWriter
2021-02-07 00:17:59 +02:00
BeforeEach ( func ( ) {
2021-03-21 20:20:57 +02:00
tmpl , err := template . New ( "" ) . Parse ( "{{.Title}} {{.Message}} {{.ProxyPrefix}} {{.StatusCode}} {{.Redirect}} {{.RequestID}} {{.Footer}} {{.Version}}" )
2021-02-07 00:17:59 +02:00
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2021-02-12 20:25:46 +02:00
errorPage = & errorPageWriter {
template : tmpl ,
proxyPrefix : "/prefix/" ,
footer : "Custom Footer Text" ,
version : "v0.0.0-test" ,
2021-02-07 00:17:59 +02:00
}
} )
2021-02-07 00:05:45 +02:00
2021-02-12 20:25:46 +02:00
Context ( "WriteErrorPage" , func ( ) {
2021-02-07 00:05:45 +02:00
It ( "Writes the template to the response writer" , func ( ) {
recorder := httptest . NewRecorder ( )
2021-03-21 20:20:57 +02:00
errorPage . WriteErrorPage ( recorder , ErrorPageOpts {
Status : 403 ,
RedirectURL : "/redirect" ,
RequestID : testRequestID ,
AppError : "Access Denied" ,
} )
2021-02-07 00:05:45 +02:00
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2021-03-21 20:20:57 +02:00
Expect ( string ( body ) ) . To ( Equal ( "Forbidden You do not have permission to access this resource. /prefix/ 403 /redirect 11111111-2222-4333-8444-555555555555 Custom Footer Text v0.0.0-test" ) )
2021-02-10 21:34:19 +02:00
} )
It ( "With a different code, uses the stock message for the correct code" , func ( ) {
recorder := httptest . NewRecorder ( )
2021-03-21 20:20:57 +02:00
errorPage . WriteErrorPage ( recorder , ErrorPageOpts {
Status : 500 ,
RedirectURL : "/redirect" ,
RequestID : testRequestID ,
AppError : "Access Denied" ,
} )
2021-02-10 21:34:19 +02:00
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2021-03-21 20:20:57 +02:00
Expect ( string ( body ) ) . To ( Equal ( "Internal Server Error Oops! Something went wrong. For more information contact your server administrator. /prefix/ 500 /redirect 11111111-2222-4333-8444-555555555555 Custom Footer Text v0.0.0-test" ) )
2021-02-10 21:34:19 +02:00
} )
It ( "With a message override, uses the message" , func ( ) {
recorder := httptest . NewRecorder ( )
2021-03-21 20:20:57 +02:00
errorPage . WriteErrorPage ( recorder , ErrorPageOpts {
Status : 403 ,
RedirectURL : "/redirect" ,
RequestID : testRequestID ,
AppError : "Access Denied" ,
Messages : [ ] interface { } {
"An extra message: %s" ,
"with more context." ,
} ,
} )
2021-02-10 21:34:19 +02:00
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2021-03-21 20:20:57 +02:00
Expect ( string ( body ) ) . To ( Equal ( "Forbidden An extra message: with more context. /prefix/ 403 /redirect 11111111-2222-4333-8444-555555555555 Custom Footer Text v0.0.0-test" ) )
} )
It ( "Sanitizes malicious user input" , func ( ) {
recorder := httptest . NewRecorder ( )
errorPage . WriteErrorPage ( recorder , ErrorPageOpts {
Status : 403 ,
RedirectURL : "/redirect" ,
RequestID : "<script>alert(1)</script>" ,
AppError : "Access Denied" ,
} )
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( string ( body ) ) . To ( Equal ( "Forbidden You do not have permission to access this resource. /prefix/ 403 /redirect <script>alert(1)</script> Custom Footer Text v0.0.0-test" ) )
2021-02-07 00:05:45 +02:00
} )
} )
2021-02-07 00:17:59 +02:00
Context ( "ProxyErrorHandler" , func ( ) {
It ( "Writes a bad gateway error the response writer" , func ( ) {
req := httptest . NewRequest ( "" , "/bad-gateway" , nil )
2021-03-21 20:20:57 +02:00
req = middlewareapi . AddRequestScope ( req , & middlewareapi . RequestScope {
RequestID : testRequestID ,
} )
2021-02-07 00:17:59 +02:00
recorder := httptest . NewRecorder ( )
errorPage . ProxyErrorHandler ( recorder , req , errors . New ( "some upstream error" ) )
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2021-03-21 20:20:57 +02:00
Expect ( string ( body ) ) . To ( Equal ( "Bad Gateway There was a problem connecting to the upstream server. /prefix/ 502 11111111-2222-4333-8444-555555555555 Custom Footer Text v0.0.0-test" ) )
2021-02-10 21:34:19 +02:00
} )
} )
Context ( "With Debug enabled" , func ( ) {
BeforeEach ( func ( ) {
tmpl , err := template . New ( "" ) . Parse ( "{{.Message}}" )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2021-02-12 20:25:46 +02:00
errorPage . template = tmpl
errorPage . debug = true
2021-02-10 21:34:19 +02:00
} )
2021-02-12 20:25:46 +02:00
Context ( "WriteErrorPage" , func ( ) {
2021-02-10 21:34:19 +02:00
It ( "Writes the detailed error in place of the message" , func ( ) {
recorder := httptest . NewRecorder ( )
2021-03-21 20:20:57 +02:00
errorPage . WriteErrorPage ( recorder , ErrorPageOpts {
Status : 403 ,
RedirectURL : "/redirect" ,
AppError : "Debug error" ,
} )
2021-02-10 21:34:19 +02:00
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( string ( body ) ) . To ( Equal ( "Debug error" ) )
} )
} )
Context ( "ProxyErrorHandler" , func ( ) {
It ( "Writes a bad gateway error the response writer" , func ( ) {
req := httptest . NewRequest ( "" , "/bad-gateway" , nil )
2021-03-21 20:20:57 +02:00
req = middlewareapi . AddRequestScope ( req , & middlewareapi . RequestScope {
RequestID : testRequestID ,
} )
2021-02-10 21:34:19 +02:00
recorder := httptest . NewRecorder ( )
errorPage . ProxyErrorHandler ( recorder , req , errors . New ( "some upstream error" ) )
body , err := ioutil . ReadAll ( recorder . Result ( ) . Body )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( string ( body ) ) . To ( Equal ( "some upstream error" ) )
} )
2021-02-07 00:17:59 +02:00
} )
} )
2021-02-07 00:05:45 +02:00
} )