Fix x-api-key header not required for GET requests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #4627
authored by Junegunn Choi · committed by Junegunn Choi · parent d1f0370 · Browse files · 2 files·+32 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−15 removed
- tests:+13 added−3 removed
- other:+19 added−12 removed
Provenance
- Commit
aad805d- Tree
9186810- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
d1f0370
2 files changed+32 added−15 removed
modified:src/server.go+19 added-12 removed
181 unmodified lines182 return 0, nil, nil183 })184 185 section := 0186+ var getMatch []string187+Loop:188 for scanner.Scan() {189 text := scanner.Text()190 switch section {189- case 0:190- getMatch := getRegex.FindStringSubmatch(text)191- if len(getMatch) > 0 {192- response := server.getHandler(parseGetParams(getMatch[1]))193- if len(response) > 0 {194- return good(response)195- }196- return answer(httpUnavailable+jsonContentType, `{"error":"timeout"}`)197- } else if !strings.HasPrefix(text, "POST / HTTP") {191+ case 0: // Request line192+ getMatch = getRegex.FindStringSubmatch(text)193+ if len(getMatch) == 0 && !strings.HasPrefix(text, "POST / HTTP") {194 return bad("invalid request method")195 }196 section++201- case 1:202- if text == crlf {197+ case 1: // Request headers198+ if text == crlf { // End of headers199+ if len(getMatch) > 0 {200+ break Loop201+ }202 if contentLength == 0 {203 return bad("content-length header missing")204 }205 section++11 unmodified lines217 case "x-api-key":218 apiKey = strings.TrimSpace(pair[1])219 }220 }222- case 2:221+ case 2: // Request body222 body += text223 }224 }225 226 if len(server.apiKey) != 0 && subtle.ConstantTimeCompare([]byte(apiKey), server.apiKey) != 1 {227 return unauthorized("invalid api key")228 }229 230+ if len(getMatch) > 0 {231+ response := server.getHandler(parseGetParams(getMatch[1]))232+ if len(response) > 0 {233+ return good(response)234+ }235+ return answer(httpUnavailable+jsonContentType, `{"error":"timeout"}`)236+ }237+238 if len(body) < contentLength {239 return bad("incomplete request")240 }241 body = body[:contentLength]36 unmodified linesmodified:test/test_server.rb+13 added-3 removed
29 unmodified lines30 end31 end32 33 def test_listen_with_api_key34- post_uri = URI('http://localhost:6266')34+ uri = URI('http://localhost:6266')35 tmux.send_keys 'seq 10 | FZF_API_KEY=123abc fzf --listen 6266', :Enter36 tmux.until { |lines| assert_equal 10, lines.match_count }37 # Incorrect API Key38 [nil, { 'x-api-key' => '' }, { 'x-api-key' => '124abc' }].each do |headers|39- res = Net::HTTP.post(post_uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers)39+ res = Net::HTTP.post(uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers)40 assert_equal '401', res.code41 assert_equal 'Unauthorized', res.message42 assert_equal "invalid api key\n", res.body43+44+ res = Net::HTTP.get_response(uri, headers)45+ assert_equal '401', res.code46+ assert_equal 'Unauthorized', res.message47+ assert_equal "invalid api key\n", res.body48 end49+50 # Valid API Key51 [{ 'x-api-key' => '123abc' }, { 'X-API-Key' => '123abc' }].each do |headers|46- res = Net::HTTP.post(post_uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers)52+ res = Net::HTTP.post(uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers)53 assert_equal '200', res.code54 tmux.until { |lines| assert_equal 100, lines.item_count }55 tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] }56+57+ res = Net::HTTP.get_response(uri, headers)58+ assert_equal '200', res.code59+ assert_equal 'yo', JSON.parse(res.body, symbolize_names: true)[:query]60 end61 end62 end