Skip to content

Commit

Permalink
fix: create options api for cors preflight request (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaichaosun authored Apr 18, 2024
1 parent 4a8e62a commit 768c61b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tests/wakunode_rest/test_rest_cors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ proc checkResponse(
expectedOrigin.isSome() and
response.headers.contains("Access-Control-Allow-Origin") and
response.headers.getLastString("Access-Control-Allow-Origin") ==
expectedOrigin.get()
expectedOrigin.get() and
response.headers.contains("Access-Control-Allow-Headers") and
response.headers.getLastString("Access-Control-Allow-Headers") == "Content-Type"
)
):
echo(
Expand Down
2 changes: 2 additions & 0 deletions waku/waku_api/rest/origin_handler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ proc originMiddlewareProc(
if origin.len == 1:
if self.everyOriginAllowed:
response.addHeader("Access-Control-Allow-Origin", "*")
response.addHeader("Access-Control-Allow-Headers", "Content-Type")
elif self.originsMatch(origin[0]):
# The Vary: Origin header to must be set to prevent
# potential cache poisoning attacks:
# https://textslashplain.com/2018/08/02/cors-and-vary/
response.addHeader("Vary", "Origin")
response.addHeader("Access-Control-Allow-Origin", origin[0])
response.addHeader("Access-Control-Allow-Headers", "Content-Type")
else:
return await request.respond(Http403, "Origin not allowed")
elif origin.len == 0:
Expand Down
19 changes: 19 additions & 0 deletions waku/waku_api/rest/relay/handlers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC* = "/relay/v1/auto/messages"
proc installRelayApiHandlers*(
router: var RestRouter, node: WakuNode, cache: MessageCache
) =
router.api(MethodOptions, ROUTE_RELAY_SUBSCRIPTIONSV1) do() -> RestApiResponse:
return RestApiResponse.ok()

router.api(MethodPost, ROUTE_RELAY_SUBSCRIPTIONSV1) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
Expand Down Expand Up @@ -92,6 +95,11 @@ proc installRelayApiHandlers*(
# Successfully unsubscribed from all requested topics
return RestApiResponse.ok()

router.api(MethodOptions, ROUTE_RELAY_MESSAGESV1) do(
pubsubTopic: string
) -> RestApiResponse:
return RestApiResponse.ok()

router.api(MethodGet, ROUTE_RELAY_MESSAGESV1) do(
pubsubTopic: string
) -> RestApiResponse:
Expand Down Expand Up @@ -166,6 +174,9 @@ proc installRelayApiHandlers*(

# Autosharding API

router.api(MethodOptions, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do() -> RestApiResponse:
return RestApiResponse.ok()

router.api(MethodPost, ROUTE_RELAY_AUTO_SUBSCRIPTIONSV1) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
Expand Down Expand Up @@ -203,6 +214,11 @@ proc installRelayApiHandlers*(

return RestApiResponse.ok()

router.api(MethodOptions, ROUTE_RELAY_AUTO_MESSAGESV1) do(
contentTopic: string
) -> RestApiResponse:
return RestApiResponse.ok()

router.api(MethodGet, ROUTE_RELAY_AUTO_MESSAGESV1) do(
contentTopic: string
) -> RestApiResponse:
Expand All @@ -224,6 +240,9 @@ proc installRelayApiHandlers*(
debug "An error ocurred while building the json respose", error = error
return RestApiResponse.internalServerError($error)

router.api(MethodOptions, ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC) do() -> RestApiResponse:
return RestApiResponse.ok()

router.api(MethodPost, ROUTE_RELAY_AUTO_MESSAGESV1_NO_TOPIC) do(
contentBody: Option[ContentBody]
) -> RestApiResponse:
Expand Down

0 comments on commit 768c61b

Please sign in to comment.