From d0a690c30314d41661f706d66f868e6be56a0f66 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 2 Feb 2019 22:48:47 -0600 Subject: [PATCH] Add CORS to API endpoints --- src/invidious.cr | 1 + src/invidious/helpers/helpers.cr | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/invidious.cr b/src/invidious.cr index abdd8cb99..81318acf6 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -3977,6 +3977,7 @@ public_folder "assets" Kemal.config.powered_by_header = false add_handler FilteredCompressHandler.new add_handler DenyFrame.new +add_handler APIHandler.new add_context_storage_type(User) Kemal.config.logger = logger diff --git a/src/invidious/helpers/helpers.cr b/src/invidious/helpers/helpers.cr index 91a802034..942757c32 100644 --- a/src/invidious/helpers/helpers.cr +++ b/src/invidious/helpers/helpers.cr @@ -43,6 +43,18 @@ class FilteredCompressHandler < Kemal::Handler end end +class APIHandler < Kemal::Handler + only ["/api/v1/*"] + + def call(env) + return call_next env unless only_match? env + + env.response.headers["Access-Control-Allow-Origin"] = "*" + + call_next env + end +end + class DenyFrame < Kemal::Handler exclude ["/embed/*"]