Skip to content

Commit ebba058

Browse files
committed
Merge upstream/dev: CORS/preflight fix from AlexxIT (AlexxIT#2168 follow-up)
Merges 812fa91 (fix(api): improve CORS and preflight request handling — add OPTIONS handling in middleware functions, ensure proper CORS headers on preflight) into the opensensor dev-rebase tip (63c58e7 fix: Stream.Stop() closes RTSP source and all consumer sessions). Clean merge with no source conflicts. Pulled into the lightNVR build target so users picking up the go2rtc-override-hardening release also get the upstream CORS fix.
2 parents 63c58e7 + 812fa91 commit ebba058

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Ultimate camera streaming application with support for dozens formats and protoc
7777
> - [#2130](https://github.com/AlexxIT/go2rtc/pull/2130) — HomeKit Secure Video
7878
> - [#2160](https://github.com/AlexxIT/go2rtc/pull/2160) — WebCodecs streaming support (@seydx)
7979
> - [#2174](https://github.com/AlexxIT/go2rtc/pull/2174) - Socket leaks in go2rtc - creality k2plus issue
80+
> - [#2223](https://github.com/AlexxIT/go2rtc/pull/2223) — Fix CORS preflight requests support
8081
---
8182
8283
## Screenshots

‎internal/api/api.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ func isLoopback(remoteAddr string) bool {
224224

225225
func middlewareAuth(username, password string, localAuth bool, next http.Handler) http.Handler {
226226
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
227+
if r.Method == "OPTIONS" {
228+
next.ServeHTTP(w, r)
229+
return
230+
}
231+
227232
if localAuth || !isLoopback(r.RemoteAddr) {
228233
user, pass, ok := r.BasicAuth()
229234
if !ok || user != username || pass != password {
@@ -242,6 +247,10 @@ func middlewareCORS(next http.Handler) http.Handler {
242247
w.Header().Set("Access-Control-Allow-Origin", "*")
243248
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
244249
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
250+
if r.Method == "OPTIONS" {
251+
w.WriteHeader(http.StatusNoContent)
252+
return
253+
}
245254
next.ServeHTTP(w, r)
246255
})
247256
}

0 commit comments

Comments
 (0)