Skip to content

Commit e36db17

Browse files
authored
fix(request.header): core.request.header return string instead of table (#11127)
1 parent b19fdcf commit e36db17

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

apisix/core/request.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ function _M.header(ctx, name)
107107
if not ctx then
108108
ctx = ngx.ctx.api_ctx
109109
end
110-
return _headers(ctx)[name]
110+
111+
local value = _headers(ctx)[name]
112+
return type(value) == "table" and value[1] or value
111113
end
112114

113115
local function modify_header(ctx, header_name, header_value, override)

apisix/plugins/real-ip.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ end
9090
local function get_addr(conf, ctx)
9191
if conf.source == "http_x_forwarded_for" then
9292
-- use the last address from X-Forwarded-For header
93-
local addrs = core.request.header(ctx, "X-Forwarded-For")
93+
-- after core.request.header function changed
94+
-- we need to get original header value by using core.request.headers
95+
local addrs = core.request.headers(ctx)["X-Forwarded-For"]
9496
if not addrs then
9597
return nil
9698
end

apisix/plugins/ua-restriction.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ end
150150

151151

152152
function _M.access(conf, ctx)
153-
local user_agent = core.request.header(ctx, "User-Agent")
153+
-- after core.request.header function changed
154+
-- we need to get original header value by using core.request.headers
155+
local user_agent = core.request.headers(ctx)["User-Agent"]
154156

155157
if not user_agent then
156158
if conf.bypass_missing then

t/core/request.t

+2-2
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,10 @@ $s
454454
local h = core.request.header(ctx, "test_header")
455455
ngx.say(h)
456456
core.request.add_header(ctx, "test_header", "t2")
457-
local h2 = core.request.header(ctx, "test_header")
457+
local h2 = core.request.headers(ctx)["test_header"]
458458
ngx.say(json.encode(h2))
459459
core.request.add_header(ctx, "test_header", "t3")
460-
local h3 = core.request.header(ctx, "test_header")
460+
local h3 = core.request.headers(ctx)["test_header"]
461461
ngx.say(json.encode(h3))
462462
}
463463
}

t/plugin/hmac-auth.t

+72-11
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,67 @@ passed
382382

383383

384384

385-
=== TEST 15: add consumer with 0 clock skew
385+
=== TEST 15: verify: ok (multiple duplicates X-HMAC-SIGNATURE header)
386+
--- config
387+
location /t {
388+
content_by_lua_block {
389+
local ngx_time = ngx.time
390+
local ngx_http_time = ngx.http_time
391+
local core = require("apisix.core")
392+
local t = require("lib.test_admin")
393+
local hmac = require("resty.hmac")
394+
local ngx_encode_base64 = ngx.encode_base64
395+
396+
local secret_key = "my-secret-key"
397+
local timestamp = ngx_time()
398+
local gmt = ngx_http_time(timestamp)
399+
local access_key = "my-access-key"
400+
local custom_header_a = "asld$%dfasf"
401+
local custom_header_b = "23879fmsldfk"
402+
403+
local signing_string = {
404+
"GET",
405+
"/hello",
406+
"",
407+
access_key,
408+
gmt,
409+
"x-custom-header-a:" .. custom_header_a,
410+
"x-custom-header-b:" .. custom_header_b
411+
}
412+
signing_string = core.table.concat(signing_string, "\n") .. "\n"
413+
core.log.info("signing_string:", signing_string)
414+
415+
local signature = hmac:new(secret_key, hmac.ALGOS.SHA256):final(signing_string)
416+
core.log.info("signature:", ngx_encode_base64(signature))
417+
local headers = {}
418+
local encoded_signature = ngx_encode_base64(signature)
419+
headers["X-HMAC-SIGNATURE"] = {encoded_signature, "another-signature"}
420+
headers["X-HMAC-ALGORITHM"] = "hmac-sha256"
421+
headers["Date"] = gmt
422+
headers["X-HMAC-ACCESS-KEY"] = access_key
423+
headers["X-HMAC-SIGNED-HEADERS"] = "x-custom-header-a;x-custom-header-b"
424+
headers["x-custom-header-a"] = custom_header_a
425+
headers["x-custom-header-b"] = custom_header_b
426+
427+
local code, body = t.test('/hello',
428+
ngx.HTTP_GET,
429+
"",
430+
nil,
431+
headers
432+
)
433+
434+
ngx.status = code
435+
ngx.say(body)
436+
}
437+
}
438+
--- request
439+
GET /t
440+
--- response_body
441+
passed
442+
443+
444+
445+
=== TEST 16: add consumer with 0 clock skew
386446
--- config
387447
location /t {
388448
content_by_lua_block {
@@ -413,11 +473,12 @@ passed
413473

414474

415475

416-
=== TEST 16: verify: invalid signature
476+
=== TEST 17: verify: invalid signature
417477
--- request
418478
GET /hello
419479
--- more_headers
420480
X-HMAC-SIGNATURE: asdf
481+
X-HMAC-SIGNATURE: asdf
421482
X-HMAC-ALGORITHM: hmac-sha256
422483
Date: Thu, 24 Sep 2020 06:39:52 GMT
423484
X-HMAC-ACCESS-KEY: my-access-key3
@@ -431,7 +492,7 @@ client request can't be validated: Invalid signature
431492

432493

433494

434-
=== TEST 17: add consumer with 1 clock skew
495+
=== TEST 18: add consumer with 1 clock skew
435496
--- config
436497
location /t {
437498
content_by_lua_block {
@@ -463,7 +524,7 @@ passed
463524

464525

465526

466-
=== TEST 18: verify: Invalid GMT format time
527+
=== TEST 19: verify: Invalid GMT format time
467528
--- config
468529
location /t {
469530
content_by_lua_block {
@@ -520,7 +581,7 @@ client request can't be validated: Clock skew exceeded
520581

521582

522583

523-
=== TEST 19: verify: put ok
584+
=== TEST 20: verify: put ok
524585
--- config
525586
location /t {
526587
content_by_lua_block {
@@ -583,7 +644,7 @@ passed
583644

584645

585646

586-
=== TEST 20: verify: put ok (pass auth data by header `Authorization`)
647+
=== TEST 21: verify: put ok (pass auth data by header `Authorization`)
587648
--- config
588649
location /t {
589650
content_by_lua_block {
@@ -645,7 +706,7 @@ passed
645706

646707

647708

648-
=== TEST 21: hit route without auth info
709+
=== TEST 22: hit route without auth info
649710
--- request
650711
GET /hello
651712
--- error_code: 401
@@ -658,7 +719,7 @@ client request can't be validated: access key or signature missing
658719

659720

660721

661-
=== TEST 22: add consumer with signed_headers
722+
=== TEST 23: add consumer with signed_headers
662723
--- config
663724
location /t {
664725
content_by_lua_block {
@@ -690,7 +751,7 @@ passed
690751

691752

692753

693-
=== TEST 23: verify with invalid signed header
754+
=== TEST 24: verify with invalid signed header
694755
--- config
695756
location /t {
696757
content_by_lua_block {
@@ -745,7 +806,7 @@ client request can't be validated: Invalid signed header x-custom-header-c
745806

746807

747808

748-
=== TEST 24: verify ok with signed headers
809+
=== TEST 25: verify ok with signed headers
749810
--- config
750811
location /t {
751812
content_by_lua_block {
@@ -800,7 +861,7 @@ passed
800861

801862

802863

803-
=== TEST 25: add consumer with plugin hmac-auth - empty configuration
864+
=== TEST 26: add consumer with plugin hmac-auth - empty configuration
804865
--- config
805866
location /t {
806867
content_by_lua_block {

0 commit comments

Comments
 (0)