Skip to content
Myr

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

2 files+32 added−15 removed

modified:src/server.go+19 added-12 removed
181 unmodified lines
182 return 0, nil, nil183 })184185 section := 0186 var getMatch []string187Loop: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 lines
217 case "x-api-key":218 apiKey = strings.TrimSpace(pair[1])219 }220 }222 case 2:221 case 2: // Request body222 body += text223 }224 }225226 if len(server.apiKey) != 0 && subtle.ConstantTimeCompare([]byte(apiKey), server.apiKey) != 1 {227 return unauthorized("invalid api key")228 }229230 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 }237238 if len(body) < contentLength {239 return bad("incomplete request")240 }241 body = body[:contentLength]
36 unmodified lines
modified:test/test_server.rb+13 added-3 removed
29 unmodified lines
30 end31 end3233 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.body4344 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 end4950 # 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] }5657 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 end62end