Skip to content

Commit 44f0d14

Browse files
committed
Improve cors headers handling for more complex scenarios
1 parent dc44cef commit 44f0d14

File tree

5 files changed

+91
-43
lines changed

5 files changed

+91
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.0.20] - 2025-04-14
2+
3+
- Handle more cors scenarios
4+
15
## [0.0.19] - 2025-03-31
26

37
- Add SetHeader response mutator

Gemfile.lock

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
foobara-http-command-connector (0.0.19)
4+
foobara-http-command-connector (0.0.20)
55
foobara (~> 0.0.88)
66

77
GEM
@@ -20,7 +20,7 @@ GEM
2020
date (3.4.1)
2121
diff-lcs (1.6.1)
2222
docile (1.4.1)
23-
dotenv (3.1.7)
23+
dotenv (3.1.8)
2424
extract-repo (0.0.9)
2525
foobara
2626
foobara-sh-cli-connector
@@ -35,29 +35,31 @@ GEM
3535
ffi (1.17.1-x86_64-darwin)
3636
ffi (1.17.1-x86_64-linux-gnu)
3737
ffi (1.17.1-x86_64-linux-musl)
38-
foob (0.0.12)
39-
foobara
40-
foobara-command-generator
41-
foobara-domain-generator
42-
foobara-domain-mapper-generator
43-
foobara-empty-ruby-project-generator
44-
foobara-empty-typescript-react-project-generator
45-
foobara-local-files-crud-driver-generator
46-
foobara-organization-generator
47-
foobara-rack-connector-generator
48-
foobara-redis-crud-driver-generator
49-
foobara-remote-imports-generator
50-
foobara-resque-connector-generator
51-
foobara-resque-scheduler-connector-generator
38+
foob (0.0.13)
39+
foobara (~> 0.0.99)
40+
foobara-command-generator (~> 0.0.1)
41+
foobara-domain-generator (~> 0.0.1)
42+
foobara-domain-mapper-generator (~> 0.0.1)
43+
foobara-empty-ruby-project-generator (~> 0.0.1)
44+
foobara-empty-typescript-react-project-generator (~> 0.0.1)
45+
foobara-local-files-crud-driver-generator (~> 0.0.1)
46+
foobara-mcp-connector-generator (~> 0.0.1)
47+
foobara-organization-generator (~> 0.0.1)
48+
foobara-rack-connector-generator (~> 0.0.1)
49+
foobara-redis-crud-driver-generator (~> 0.0.1)
50+
foobara-remote-imports-generator (~> 0.0.1)
51+
foobara-resque-connector-generator (~> 0.0.1)
52+
foobara-resque-scheduler-connector-generator (~> 0.0.1)
5253
foobara-sh-cli-connector (~> 0.0.10)
53-
foobara-sh-cli-connector-generator
54-
foobara-type-generator
55-
foobara-typescript-react-command-form-generator
56-
foobara-typescript-remote-command-generator
57-
foobara (0.0.91)
54+
foobara-sh-cli-connector-generator (~> 0.0.1)
55+
foobara-type-generator (~> 0.0.1)
56+
foobara-typescript-react-command-form-generator (~> 0.0.1)
57+
foobara-typescript-remote-command-generator (~> 0.0.1)
58+
foobara (0.0.102)
5859
bigdecimal
5960
foobara-lru-cache (~> 0.0.2)
6061
foobara-util (~> 0.0.11)
62+
inheritable-thread-vars (~> 0.0.1)
6163
foobara-command-generator (0.0.3)
6264
foobara
6365
foobara-files-generator
@@ -69,10 +71,10 @@ GEM
6971
foobara-files-generator
7072
foobara-dotenv-loader (0.0.3)
7173
dotenv
72-
foobara-empty-ruby-project-generator (0.0.14)
73-
extract-repo
74-
foobara
75-
foobara-files-generator
74+
foobara-empty-ruby-project-generator (0.0.15)
75+
extract-repo (~> 0.0.1)
76+
foobara (~> 0.0.94)
77+
foobara-files-generator (~> 0.0.1)
7678
foobara-empty-typescript-react-project-generator (0.0.4)
7779
foobara
7880
foobara-files-generator
@@ -82,6 +84,8 @@ GEM
8284
foobara
8385
foobara-files-generator
8486
foobara-lru-cache (0.0.2)
87+
foobara-mcp-connector-generator (0.0.6)
88+
foobara (~> 0.0.99)
8589
foobara-organization-generator (0.0.2)
8690
foobara
8791
foobara-files-generator
@@ -102,7 +106,7 @@ GEM
102106
foobara-rubocop-rules (0.0.8)
103107
rubocop
104108
rubocop-rspec
105-
foobara-sh-cli-connector (0.0.13)
109+
foobara-sh-cli-connector (0.0.15)
106110
foobara (~> 0.0.88)
107111
foobara-sh-cli-connector-generator (0.0.4)
108112
foobara-files-generator (~> 0.0.1)
@@ -111,12 +115,9 @@ GEM
111115
foobara-type-generator (0.0.4)
112116
foobara
113117
foobara-files-generator
114-
foobara-typescript-react-command-form-generator (0.0.8)
115-
foobara
116-
foobara-files-generator
117-
foobara-typescript-remote-command-generator
118-
foobara-typescript-remote-command-generator (0.0.16)
119-
foobara (~> 0.0.88)
118+
foobara-typescript-react-command-form-generator (0.0.11)
119+
foobara-typescript-remote-command-generator (~> 0.0.1)
120+
foobara-typescript-remote-command-generator (0.0.17)
120121
foobara-files-generator (~> 0.0.1)
121122
foobara-util (0.0.11)
122123
formatador (1.1.0)
@@ -137,8 +138,9 @@ GEM
137138
guard-compat (~> 1.1)
138139
rspec (>= 2.99.0, < 4.0)
139140
hashdiff (1.1.2)
141+
inheritable-thread-vars (0.0.2)
140142
io-console (0.8.0)
141-
irb (1.15.1)
143+
irb (1.15.2)
142144
pp (>= 0.6.0)
143145
rdoc (>= 4.0.0)
144146
reline (>= 0.4.2)
@@ -156,8 +158,8 @@ GEM
156158
nenv (~> 0.1)
157159
shellany (~> 0.0)
158160
ostruct (0.6.1)
159-
parallel (1.26.3)
160-
parser (3.3.7.4)
161+
parallel (1.27.0)
162+
parser (3.3.8.0)
161163
ast (~> 2.4.1)
162164
racc
163165
pp (0.6.2)
@@ -183,7 +185,7 @@ GEM
183185
rdoc (6.13.1)
184186
psych (>= 4.0.0)
185187
regexp_parser (2.10.0)
186-
reline (0.6.0)
188+
reline (0.6.1)
187189
io-console (~> 0.5)
188190
rexml (3.4.1)
189191
rspec (3.13.0)
@@ -202,18 +204,18 @@ GEM
202204
diff-lcs (>= 1.2.0, < 2.0)
203205
rspec-support (~> 3.13.0)
204206
rspec-support (3.13.2)
205-
rubocop (1.75.1)
207+
rubocop (1.75.2)
206208
json (~> 2.3)
207209
language_server-protocol (~> 3.17.0.2)
208210
lint_roller (~> 1.1.0)
209211
parallel (~> 1.10)
210212
parser (>= 3.3.0.2)
211213
rainbow (>= 2.2.2, < 4.0)
212214
regexp_parser (>= 2.9.3, < 3.0)
213-
rubocop-ast (>= 1.43.0, < 2.0)
215+
rubocop-ast (>= 1.44.0, < 2.0)
214216
ruby-progressbar (~> 1.7)
215217
unicode-display_width (>= 2.4.0, < 4.0)
216-
rubocop-ast (1.43.0)
218+
rubocop-ast (1.44.1)
217219
parser (>= 3.3.7.2)
218220
prism (~> 1.4)
219221
rubocop-rake (0.7.1)

src/http.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ def run(*, **)
6060

6161
def request_to_command(request)
6262
if request.method == "OPTIONS"
63-
# TODO: this feels a bit hacky and like overkill...
64-
return Foobara::CommandConnectors::Http::Commands::GetOptions.new
63+
return Foobara::CommandConnectors::Http::Commands::GetOptions.new(request:)
6564
end
6665

6766
command = super
6867

69-
# TODO: We should kill these case statements and require connecting these commands
7068
if request.action == "help"
7169
command.class.serializers = [Commands::Help::ResultSerializer]
7270
end
@@ -136,6 +134,12 @@ def headers_for(request)
136134

137135
def static_headers
138136
@static_headers ||= ENV.each_with_object({}) do |(key, value), headers|
137+
next if %w[
138+
FOOBARA_HTTP_RESPONSE_HEADER_ACCESS_CONTROL_ALLOW_HEADERS
139+
FOOBARA_HTTP_RESPONSE_HEADER_ACCESS_CONTROL_ALLOW_METHODS
140+
FOOBARA_HTTP_RESPONSE_HEADER_ACCESS_CONTROL_MAX_AGE
141+
].include?(key)
142+
139143
match = key.match(/\AFOOBARA_HTTP_RESPONSE_HEADER_(.*)\z/)
140144

141145
if match

src/http/commands/get_options.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,49 @@ class Http < CommandConnector
44
module Commands
55
# TODO: this is a bit of a hack, just a total no-op... shouldn't really need this command at all ideally
66
class GetOptions < Foobara::Command
7+
inputs do
8+
request :duck, :required
9+
end
710
result :string
811

912
def execute
13+
initialize_response_headers
14+
15+
set_allow_methods
16+
set_allow_headers
17+
set_access_control_max_age
18+
19+
# TODO: what's with this empty string?
1020
""
1121
end
22+
23+
def initialize_response_headers
24+
request.response_headers ||= {}
25+
end
26+
27+
def set_allow_methods
28+
allow_methods = ENV.fetch("FOOBARA_HTTP_RESPONSE_HEADER_ACCESS_CONTROL_ALLOW_METHODS", nil)
29+
if allow_methods
30+
request.response_headers["access-control-allow-methods"] = allow_methods
31+
end
32+
end
33+
34+
def set_allow_headers
35+
allow_headers = ENV.fetch("FOOBARA_HTTP_RESPONSE_HEADER_ACCESS_CONTROL_ALLOW_HEADERS", nil)
36+
if allow_headers
37+
if allow_headers == "*"
38+
allow_headers = request.headers["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]
39+
end
40+
request.response_headers["access-control-allow-headers"] = allow_headers
41+
end
42+
end
43+
44+
def set_access_control_max_age
45+
access_control_max_age = ENV.fetch("FOOBARA_HTTP_RESPONSE_HEADER_ACCESS_CONTROL_MAX_AGE", nil)
46+
if access_control_max_age
47+
request.response_headers["access-control-max-age"] = access_control_max_age
48+
end
49+
end
1250
end
1351
end
1452
end

version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Foobara
22
module HttpCommandConnector
3-
VERSION = "0.0.19".freeze
3+
VERSION = "0.0.20".freeze
44

55
local_ruby_version = File.read("#{__dir__}/.ruby-version").chomp
66
local_ruby_version_minor = local_ruby_version[/\A(\d+\.\d+)\.\d+\z/, 1]

0 commit comments

Comments
 (0)