Skip to content

Commit d564b6e

Browse files
NoF0rteehsandeepShubhamRasal
authored
Adding a very simple CORS implementation (#93)
* Added a very simple CORS implementation * Update header add location --------- Co-authored-by: Sandeep Singh <sandeep@projectdiscovery.io> Co-authored-by: NoF0rte <nof0rte@users.noreply.github.com> Co-authored-by: shubhamrasal <shubhamdharmarasal@gmail.com>
1 parent 32ea075 commit d564b6e

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

internal/runner/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Options struct {
3535
MaxFileSize int
3636
HTTP1Only bool
3737
MaxDumpBodySize int
38+
CORS bool
3839
HTTPHeaders HTTPHeaders
3940
}
4041

@@ -64,6 +65,7 @@ func ParseOptions() *Options {
6465
flag.BoolVar(&options.HTTP1Only, "http1", false, "Enable only HTTP1")
6566
flag.IntVar(&options.MaxFileSize, "max-file-size", 50, "Max Upload File Size")
6667
flag.IntVar(&options.MaxDumpBodySize, "max-dump-body-size", -1, "Max Dump Body Size")
68+
flag.BoolVar(&options.CORS, "cors", false, "Enable Cross-Origin Resource Sharing (CORS)")
6769
flag.Var(&options.HTTPHeaders, "header", "Add HTTP Response Header (name: value), can be used multiple times")
6870
flag.Parse()
6971

internal/runner/runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func New(options *Options) (*Runner, error) {
6868
MaxFileSize: r.options.MaxFileSize,
6969
HTTP1Only: r.options.HTTP1Only,
7070
MaxDumpBodySize: unit.ToMb(r.options.MaxDumpBodySize),
71+
CORS: r.options.CORS,
7172
HTTPHeaders: r.options.HTTPHeaders,
7273
})
7374
if err != nil {

pkg/httpserver/corslayer.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package httpserver
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
)
7+
8+
func (t *HTTPServer) corslayer(handler http.Handler) http.Handler {
9+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
10+
headers := w.Header()
11+
headers.Set("Access-Control-Allow-Origin", "*")
12+
if r.Method != http.MethodOptions {
13+
handler.ServeHTTP(w, r)
14+
return
15+
}
16+
17+
headers.Add("Vary", "Origin")
18+
headers.Add("Vary", "Access-Control-Request-Method")
19+
headers.Add("Vary", "Access-Control-Request-Headers")
20+
21+
reqMethod := r.Header.Get("Access-Control-Request-Method")
22+
if reqMethod != "" {
23+
headers.Set("Access-Control-Allow-Methods", strings.ToUpper(reqMethod))
24+
}
25+
26+
w.WriteHeader(http.StatusOK)
27+
})
28+
}

pkg/httpserver/httpserver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Options struct {
2727
HTTP1Only bool
2828
MaxFileSize int // 50Mb
2929
MaxDumpBodySize int64
30+
CORS bool
3031
HTTPHeaders []HTTPHeader
3132
}
3233

@@ -72,6 +73,10 @@ func New(options *Options) (*HTTPServer, error) {
7273
addHandler(h.basicauthlayer)
7374
}
7475

76+
if options.CORS {
77+
addHandler(h.corslayer)
78+
}
79+
7580
httpHandler = h.loglayer(httpHandler)
7681
httpHandler = h.headerlayer(httpHandler, options.HTTPHeaders)
7782

0 commit comments

Comments
 (0)