-
Notifications
You must be signed in to change notification settings - Fork 98
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(cors): allow OPTIONS request to KDF server #2191
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
mm2src/mm2_main/src/rpc.rs
Outdated
if req.method == Method::OPTIONS { | ||
return Response::builder() | ||
.status(StatusCode::OK) | ||
.header(ACCESS_CONTROL_ALLOW_ORIGIN, rpc_cors.clone()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the clone
here we don't need i guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed here 3dc9715
mm2src/mm2_main/src/rpc.rs
Outdated
let rpc_cors = match ctx.conf["rpccors"].as_str() { | ||
Some(s) => try_sf!(HeaderValue::from_str(s)), | ||
None => HeaderValue::from_static("http://localhost:3000"), | ||
None => { | ||
let rpc_cors = if ctx.is_https() { | ||
format!("https://localhost:{}", local_port) | ||
} else { | ||
format!("http://localhost:{}", local_port) | ||
}; | ||
try_sf!(HeaderValue::from_str(&rpc_cors)) | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we want to abandon rpccors
now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize now that this port should be the frontend port used in development. I can use the client port here instead, but this opens it up for any port the request comes from, e.g., a script running locally on any port in the browser can access the RPC service. I will revert to port 3000 as it's commonly used for frontend development.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here 6f144e9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything is looks good to me, only small note
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM :)
I am wondering tho how did cors work before since the specs mention that the browser sends an OPTIONS
request first and we didn't cover that.
@mariocynicys I'm assuming this wasn't picked up until now because there hasn't been an attempt to implement remote RPC access from the browser until now and since running WASM KDF in the browser doesn't use HTTP. This is being worked on now as part of a Flutter SDK package for KDF repository here. The demo site is available here: https://komodo-playground.web.app/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shamardy I'm having the same CORS errors for 500
responses because the Access-Control-Allow-Origin
header isn't included in the response. Because of this, the browser does not allow access to the actual response and throws a generic XML
exception.
Here's an example of the request and response using curl. Note the absence of the cors headers in the response.
curl -v --url "127.0.0.1:7783" --data '{
"method": "&version"
}'
* Trying 127.0.0.1:7783...
* Connected to 127.0.0.1 (127.0.0.1) port 7783
> POST / HTTP/1.1
> Host: 127.0.0.1:7783
> User-Agent: curl/8.6.0
> Accept: */*
> Content-Length: 26
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 500 Internal Server Error
< content-type: application/json
< content-length: 25
< date: Tue, 20 Aug 2024 13:57:08 GMT
<
* Connection #0 to host 127.0.0.1 left intact
{"error":"Invalid input"}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work.. just couple notes.
it seems like the build that you used to run this test doesn't reflect with the changes in the PR yet.. |
@CharlVS are you using latest build/commit from this PR? If you are then the error has to be related to something in your mm2 config as far as I can see. Only errors I don't return cors for is the ones before cors is set. let ctx = try_sf!(MmArc::from_ffi_handle(ctx_h));
// https://github.com/artemii235/SuperNET/issues/219
let rpc_cors = match ctx.conf["rpccors"].as_str() {
Some(s) => try_sf!(HeaderValue::from_str(s)),
None => {
if ctx.is_https() {
HeaderValue::from_static("https://localhost:3000")
} else {
HeaderValue::from_static("http://localhost:3000")
}
},
}; So either |
Thanks, @shamardy. Yeah, it looks like I was not running the latest version, even though I double-checked that I had the latest zip downloaded. I suspect I may have unzipped the previous build after I downloaded the new zip. I'm testing it out now. I will test again now and let you know. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working as expected. Thanks for swiftly resolving this :)
82b2a97
to
2d3e268
Compare
a102bbb
to
9b94bf0
Compare
* dev: chore(RPCs): rename `get_peers_info` RPC to `get_directly_connected_peers` (KomodoPlatform#2195) chore(WASM-builds): remove `wasm-opt` overriding (KomodoPlatform#2200) fix(coins): add p2p feature to mm2_net dependency (KomodoPlatform#2210) chore(test): turn on debug assertion (KomodoPlatform#2204) feat(sia): extract sia lib to external repo (KomodoPlatform#2167) feat(eth-swap): eth tpu v2 methods, eth docker test enhancements (KomodoPlatform#2169) fix(cors): allow OPTIONS request to KDF server (KomodoPlatform#2191) docs(README): update commit badges to use dev branch (KomodoPlatform#2193) use default value for `komodo_proxy` (KomodoPlatform#2192) feat(cosmos): komodo-defi-proxy support (KomodoPlatform#2173)
* dev: chore(RPCs): rename `get_peers_info` RPC to `get_directly_connected_peers` (#2195) chore(WASM-builds): remove `wasm-opt` overriding (#2200) fix(coins): add p2p feature to mm2_net dependency (#2210) chore(test): turn on debug assertion (#2204) feat(sia): extract sia lib to external repo (#2167) feat(eth-swap): eth tpu v2 methods, eth docker test enhancements (#2169) fix(cors): allow OPTIONS request to KDF server (#2191) docs(README): update commit badges to use dev branch (#2193) use default value for `komodo_proxy` (#2192) feat(cosmos): komodo-defi-proxy support (#2173)
@KomodoPlatform/qa To Test:
Already tested by @CharlVS
No docs needed for this PR.