@@ -8,6 +8,18 @@ import (
88 "net/http"
99)
1010
11+ // errorResponse creates an HTTP error response with plain text content.
12+ func errorResponse (statusCode int , message string ) * http.Response {
13+ return & http.Response {
14+ StatusCode : statusCode ,
15+ ProtoMajor : 1 ,
16+ ProtoMinor : 1 ,
17+ Header : http.Header {"Content-Type" : {"text/plain" }},
18+ Body : io .NopCloser (bytes .NewReader ([]byte (message ))),
19+ ContentLength : int64 (len (message )),
20+ }
21+ }
22+
1123// TokenVendor intercepts OAuth2 token exchange requests from the agent's
1224// Google Auth library and returns dummy tokens.
1325//
@@ -36,6 +48,10 @@ type tokenResponse struct {
3648// IsTokenExchange returns true if the request is an OAuth2 token exchange
3749// to Google's token endpoint.
3850func IsTokenExchange (req * http.Request ) bool {
51+ if req == nil || req .URL == nil {
52+ return false
53+ }
54+
3955 host := req .URL .Host
4056 if host == "" {
4157 host = req .Host
@@ -51,6 +67,11 @@ func IsTokenExchange(req *http.Request) bool {
5167// a dummy access token. The real token injection happens later via the
5268// GCloudInjector when the agent makes API calls to *.googleapis.com.
5369func (tv * TokenVendor ) HandleTokenExchange (req * http.Request ) * http.Response {
70+ if req == nil || req .URL == nil {
71+ log .Printf ("DEFENSIVE_CHECK: HandleTokenExchange called with nil request or URL" )
72+ return errorResponse (http .StatusBadRequest , "Malformed token exchange request" )
73+ }
74+
5475 resp := & tokenResponse {
5576 AccessToken : "paude-proxy-managed" ,
5677 ExpiresIn : 3600 ,
@@ -60,7 +81,7 @@ func (tv *TokenVendor) HandleTokenExchange(req *http.Request) *http.Response {
6081 body , err := json .Marshal (resp )
6182 if err != nil {
6283 log .Printf ("ERROR token vendor: marshal response: %v" , err )
63- return nil
84+ return errorResponse ( http . StatusInternalServerError , "Internal token vendor error" )
6485 }
6586
6687 log .Printf ("TOKEN_VEND host=%s path=%s (returned dummy token, real injection at request time)" , req .URL .Host , req .URL .Path )
0 commit comments