Skip to content

Commit 850c235

Browse files
authored
Merge pull request #14 from bbrowning/http-403-for-blocked-connect
Return HTTP 403 for blocked CONNECT requests to stop client retry storms
2 parents c2be1d0 + 8ad83e8 commit 850c235

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

internal/proxy/proxy.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ func (cf *ClientFilter) String() string {
174174
return strings.Join(parts, ", ")
175175
}
176176

177+
// rejectMsg is the generic response body for all rejected requests (CONNECT and plain HTTP).
178+
// Intentionally vague to avoid revealing why the request was blocked.
179+
const rejectMsg = "Request blocked by proxy policy"
180+
177181
// Config holds proxy configuration.
178182
type Config struct {
179183
ListenAddr string
@@ -219,6 +223,7 @@ func New(cfg Config) *http.Server {
219223
srcIP := parseClientIP(ctx)
220224
if srcIP == nil || !cfg.ClientFilter.IsAllowed(srcIP) {
221225
log.Printf("CLIENT_REJECTED CONNECT %s from %s (not in allowed clients)", host, clientIP(ctx))
226+
ctx.Resp = goproxy.NewResponse(ctx.Req, goproxy.ContentTypeText, http.StatusForbidden, rejectMsg)
222227
return rejectConnect, host
223228
}
224229
}
@@ -232,6 +237,7 @@ func New(cfg Config) *http.Server {
232237
if cfg.BlockedLogger != nil {
233238
cfg.BlockedLogger.Log(clientIP(ctx), "CONNECT", host)
234239
}
240+
ctx.Resp = goproxy.NewResponse(ctx.Req, goproxy.ContentTypeText, http.StatusForbidden, rejectMsg)
235241
return rejectConnect, host
236242
}
237243

@@ -240,6 +246,7 @@ func New(cfg Config) *http.Server {
240246
if cfg.BlockedLogger != nil {
241247
cfg.BlockedLogger.Log(clientIP(ctx), "CONNECT", host)
242248
}
249+
ctx.Resp = goproxy.NewResponse(ctx.Req, goproxy.ContentTypeText, http.StatusForbidden, rejectMsg)
243250
return rejectConnect, host
244251
}
245252

@@ -264,7 +271,7 @@ func New(cfg Config) *http.Server {
264271
return req, goproxy.NewResponse(req,
265272
goproxy.ContentTypeText,
266273
http.StatusForbidden,
267-
"Client IP not allowed by proxy policy",
274+
rejectMsg,
268275
)
269276
}
270277
}
@@ -284,7 +291,7 @@ func New(cfg Config) *http.Server {
284291
return req, goproxy.NewResponse(req,
285292
goproxy.ContentTypeText,
286293
http.StatusForbidden,
287-
"Port not allowed by proxy policy",
294+
rejectMsg,
288295
)
289296
}
290297
}
@@ -298,7 +305,7 @@ func New(cfg Config) *http.Server {
298305
return req, goproxy.NewResponse(req,
299306
goproxy.ContentTypeText,
300307
http.StatusForbidden,
301-
"Domain not allowed by proxy policy",
308+
rejectMsg,
302309
)
303310
}
304311

0 commit comments

Comments
 (0)