Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade:websocket: request origin not allowed by Upgrader.CheckOrigin #367

Closed
fghjhuang opened this issue Mar 23, 2018 · 4 comments
Closed

Comments

@fghjhuang
Copy link

i use the server echo to test,but get this error,can you help me? i am not so good at golang
`package main

import (
"flag"
"log"
"net/http"

"github.com/gorilla/websocket"

)

var addr = flag.String("addr", ":80", "http service address")

var upgrader = websocket.Upgrader{} // use default options

func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}

func main() {
flag.Parse()
log.SetFlags(0)
http.HandleFunc("/", echo)
log.Fatal(http.ListenAndServe(*addr, nil))
}`

@garyburd
Copy link
Contributor

@fghjhuang
Copy link
Author

add to check :CheckOrigin: func(r *http.Request) bool {
return true
},

@mask3000
Copy link

mask3000 commented Jul 4, 2018

Insert “upgrader.CheckOrigin = func(r *http.Request) bool { return true }” before ”conn, err := upgrader.Upgrade(w, r, nil)”

@elithrar
Copy link
Contributor

elithrar commented Jul 4, 2018

You should never blindly trust any Origin by return true.

Have the function range over a list of accepted origins.

@gorilla gorilla locked and limited conversation to collaborators Aug 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants