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

fix "Invalid Request Origin" error #10573

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 19 additions & 36 deletions src/compiler/crystal/tools/playground/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -472,31 +472,25 @@ module Crystal::Playground
end

client_ws = PathWebSocketHandler.new "/client" do |ws, context|
origin = context.request.headers["Origin"]
if !accept_request?(origin)
Log.warn { "Invalid Request Origin: #{origin}" }
ws.close :policy_violation, "Invalid Request Origin"
else
@sessions_key += 1
@sessions[@sessions_key] = session = Session.new(ws, @sessions_key, @port)
Log.info { "/client WebSocket connected as session=#{@sessions_key}" }

ws.on_message do |message|
json = JSON.parse(message)
case json["type"].as_s
when "run"
source = json["source"].as_s
tag = json["tag"].as_i
session.run source, tag
when "stop"
session.stop
when "format"
source = json["source"].as_s
tag = json["tag"].as_i
session.format source, tag
else
# TODO: maybe raise because it's an unexpected message?
end
@sessions_key += 1
@sessions[@sessions_key] = session = Session.new(ws, @sessions_key, @port)
Log.info { "/client WebSocket connected as session=#{@sessions_key}" }

ws.on_message do |message|
json = JSON.parse(message)
case json["type"].as_s
when "run"
source = json["source"].as_s
tag = json["tag"].as_i
session.run source, tag
when "stop"
session.stop
when "format"
source = json["source"].as_s
tag = json["tag"].as_i
session.format source, tag
else
# TODO: maybe raise because it's an unexpected message?
end
end
end
Expand Down Expand Up @@ -530,16 +524,5 @@ module Crystal::Playground
raise Playground::Error.new(ex.message)
end
end

private def accept_request?(origin)
case @host
when nil
origin == "http://127.0.0.1:#{@port}" || origin == "http://localhost:#{@port}"
when "0.0.0.0"
true
else
origin == "http://#{@host}:#{@port}"
end
end
end
end