You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "jaspertian (via GitHub)" <gi...@apache.org> on 2024/02/26 08:01:06 UTC

[I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

jaspertian opened a new issue, #10967:
URL: https://github.com/apache/apisix/issues/10967

   ### Current Behavior
   
   get_real_payload 中使用了 merge(real_payload, extra_payload)的方式来合并 payload, 但作为参数传入的 extra_payload 并未做严格检查,调用者可以任意传入 key 和 exp ,覆盖 real_payload 中的值。
   
   ### Expected Behavior
   
   修改代码,当传入的 payload 不为空时,采用 merge( extra_payload, real_payload)合并,并返回 extra_payload
   
   ### Error Logs
   
   _No response_
   
   ### Steps to Reproduce
   
   1. 启用 jwt-auth 插件
   2. 通过 public-api插件暴露 /apisix/plugin/jwt/sign 接口
   3. 构造 { “key":"letmein", exp: 123456} 作为 Body 提交至上述接口
   4. 检查 response 中的 JWT,可发现 payload 中的值被替换为上述 DATA
   
   ### Environment
   
   - APISIX version (run `apisix version`): 3.8.0
   - Operating system (run `uname -a`):
   - OpenResty / Nginx version (run `openresty -V` or `nginx -V`):
   - etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`):
   - APISIX Dashboard version, if relevant:
   - Plugin runner version, for issues related to plugin runners:
   - LuaRocks version, for installation issues (run `luarocks --version`):
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1980175060

   ok,It looks like neither the `key` nor the `exp` should be modified.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "Gallardot (via GitHub)" <gi...@apache.org>.
Gallardot commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1970314710

   > Our tech-team did some modification as follow:
   > 
   > ```
   > local function get_real_payload(key, auth_conf, payload)
   >     local real_payload = {
   >         key = key,
   >         exp = ngx_time() + auth_conf.exp
   >     }
   >     if payload then
   >         local extra_payload = core.json.decode(payload)
   > -        core.table.merge(real_payload, extra_payload)
   > +        core.table.merge(extra_payload, real_payload)
   > +        return extra_payload
   >     end
   >     return real_payload
   > end
   > ```
   
   LGTM. Would you like to create a PR to fix it?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1977886783

   > The `get_real_payload` is only used in endpoint `/apisix/plugin/jwt/sign`, right? 
   Yes.
   
   > This endpoint is used to generate JWT token for users, it doesn't have any influence on gateway or upstream. 
   Yes. The endpoint is a public announced API in official document.
   
   > Why do we need to worry about data tampering?
   1. In practice many user need a centralized jwt ISSUER and this function is a potential solutions.  
   2. There is not any comment or warning that the function is experimental and can't be involved in production environment.
   
   So the bug should be fixed.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1978073205

   But the key(real_payload) and the extra_payload are all from the request, it is acceptable that the extra_payload can modify the real_payload?
   ```
   local function get_real_payload(key, auth_conf, payload)
       local real_payload = {
           key = **key**,
           exp = ngx_time() + auth_conf.exp
       }
       if payload then
           local extra_payload = core.json.decode(**payload**)
           core.table.merge(real_payload, extra_payload)
       end
       return real_payload
   end
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1968311518

   关于 key 值在验证中的使用,可参考 jwt-aut.lua 中的 rewrite 方法:
   
   `
       local user_key = jwt_obj.payload and jwt_obj.payload.key
       if not user_key then
           return 401, {message = "missing user key in JWT token"}
       end
   
       local consumer_conf = consumer_mod.plugin(plugin_name)
       if not consumer_conf then
           return 401, {message = "Missing related consumer"}
       end
   
       local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
   
       local consumer = consumers[user_key]
       if not consumer then
           return 401, {message = "Invalid user key in JWT token"}
       end`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1968318402

   @hanqingwu
   
   > @jaspertian , I can not follow your mean . real_payload is a temporarily value , why are you mean cover real_payload ?
   > 
   > ```
   > local function get_real_payload(key, auth_conf, payload)
   >     local real_payload = {
   >         key = key,
   >         exp = ngx_time() + auth_conf.exp
   >     }
   >     if payload then
   >         local extra_payload = core.json.decode(payload)
   >         core.table.merge(real_payload, extra_payload)
   >     end
   >     return real_payload
   > end
   > ```
   
   如您所示的代码,最后会 return real_payload  作为最终的 jwt payload ,所以其中的值应当确保没有被恶意篡改。


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1978269052

   > But the real_payload (the key) and the extra_payload are all from the request, it is acceptable that the extra_payload can modify the real_payload?
   > 
   > ```
   > local function get_real_payload(key, auth_conf, payload)
   >     local real_payload = {
   >         key = **key**,
   >         exp = ngx_time() + auth_conf.exp
   >     }
   >     if payload then
   >         local extra_payload = core.json.decode(**payload**)
   >         core.table.merge(real_payload, extra_payload)
   >     end
   >     return real_payload
   > end
   > ```
   
   The key had been validated in function gen_token and only valid key will pass to get_real_payload.
   
   Please refer to code line  396 - 413
   
   ```
   local function gen_token()
       local args = core.request.get_uri_args()
       if not args or not args.key then
           return core.response.exit(400)
       end
   
       local key = args.key
       local payload = args.payload
       if payload then
           payload = ngx.unescape_uri(payload)
       end
   
       local consumer_conf = consumer_mod.plugin(plugin_name)
       if not consumer_conf then
           return core.response.exit(404)
       end
   
       local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
   
       core.log.info("consumers: ", core.json.delay_encode(consumers))
       local consumer = consumers[key]
       if not consumer then
           return core.response.exit(404)
       end
   
   ```
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "Gallardot (via GitHub)" <gi...@apache.org>.
Gallardot commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1970297387

   > 1. yes , from source code , if client payload contain  key value will overwrite plugin setting , the  key value from client url paramter also can be real_payload key, so I think key no need to fix.
   > 2. when payload contain exp will overwrite plugin set, and get different exp value between plugin set,	so I think it will be a vulnerability .
   >    `curl -G --data-urlencode 'payload={"uid":10000,"uname":"test",exp: 43200}' http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key -i`
   > 3. finaly I suggest fix like these.
   > 
   > ```
   > --- a/apisix/plugins/jwt-auth.lua
   > +++ b/apisix/plugins/jwt-auth.lua
   > @@ -249,13 +249,13 @@ end
   >  
   >  local function get_real_payload(key, auth_conf, payload)
   >      local real_payload = {
   > -        key = key,
   > -        exp = ngx_time() + auth_conf.exp
   > +        key = key
   >      }
   >      if payload then
   >          local extra_payload = core.json.decode(payload)
   >          core.table.merge(real_payload, extra_payload)
   >      end
   > +    real_payload.exp = ngx_time() + auth_conf.exp
   >      return real_payload
   >  end
   > ```
   
   The `key` field can still be overridden by malicious `payload`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1970312860

   Our tech-team did some modification as follow:
   
   ```
   local function get_real_payload(key, auth_conf, payload)
       local real_payload = {
           key = key,
           exp = ngx_time() + auth_conf.exp
       }
       if payload then
           local extra_payload = core.json.decode(payload)
   -         core.table.merge(real_payload, extra_payload)
   +        core.table.merge(extra_payload, real_payload)
   +        return extra_payload
       end
       return real_payload
   end
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1976287962

   The `get_real_payload` is only used in endpoint `/apisix/plugin/jwt/sign`, right?
   This endpoint is used to generate JWT token for users, it doesn't have any influence on gateway or upstream.
   Why do we need to worry about data tampering?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "hanqingwu (via GitHub)" <gi...@apache.org>.
hanqingwu commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1965701228

   @jaspertian ,  I can not follow your mean .
   real_payload is a temporarily value  , why are you mean cover  real_payload ?
   ```
   local function get_real_payload(key, auth_conf, payload)
       local real_payload = {
           key = key,
           exp = ngx_time() + auth_conf.exp
       }
       if payload then
           local extra_payload = core.json.decode(payload)
           core.table.merge(real_payload, extra_payload)
       end
       return real_payload
   end
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "hanqingwu (via GitHub)" <gi...@apache.org>.
hanqingwu commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1968739835

   1. yes , from source code , if client payload contain  key value will overwrite plugin setting , the  key value from client url paramter also can be real_payload key, so I think key no need to fix. 
   2. when payload contain exp will overwrite plugin set, and get different exp value between plugin set,	so I think it will be a vulnerability . 
   `curl -G --data-urlencode 'payload={"uid":10000,"uname":"test",exp: 43200}' http://127.0.0.1:9080/apisix/plugin/jwt/sign?key=user-key -i`
   
   3. finaly I suggest fix like these.
    
   ```
   --- a/apisix/plugins/jwt-auth.lua
   +++ b/apisix/plugins/jwt-auth.lua
   @@ -249,13 +249,13 @@ end
    
    local function get_real_payload(key, auth_conf, payload)
        local real_payload = {
   -        key = key,
   -        exp = ngx_time() + auth_conf.exp
   +        key = key
        }
        if payload then
            local extra_payload = core.json.decode(payload)
            core.table.merge(real_payload, extra_payload)
        end
   +    real_payload.exp = ngx_time() + auth_conf.exp
        return real_payload
    end
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "monkeyDluffy6017 (via GitHub)" <gi...@apache.org>.
monkeyDluffy6017 closed issue #10967: bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能
URL: https://github.com/apache/apisix/issues/10967


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "shreemaan-abhishek (via GitHub)" <gi...@apache.org>.
shreemaan-abhishek commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1963878386

   could you please update the issue description in english?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1968231036

   > @jaspertian , I can not follow your mean . real_payload is a temporarily value , why are you mean cover real_payload ?
   > 
   > ```
   > local function get_real_payload(key, auth_conf, payload)
   >     local real_payload = {
   >         key = key,
   >         exp = ngx_time() + auth_conf.exp
   >     }
   >     if payload then
   >         local extra_payload = core.json.decode(payload)
   >         core.table.merge(real_payload, extra_payload)
   >     end
   >     return real_payload
   > end
   > ```
   
   按照我的理解,Real-payload 中的 key 值表示的是 CONSUMER, 当客户端携带这个 JWT 访问时,会依据 key 值寻找 CONSUMER  并获取 SECRET 进行签名的验证。所以这个 key 值不能随意被 extra-payload 中的值覆盖。
   
   同理, exp 值代表 JWT 失效时间,是通过 CONSUMER 中的配置值计算得出的,也不能被 extra-payload 中的值覆盖。
   
   所以,real_payload 不是 temporarily value ,而是需要严格保护的值。


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] bug: 插件 jwt-auth 的 function get_real_payload 存在 key 和 exp 被恶意修改的可能 [apisix]

Posted by "jaspertian (via GitHub)" <gi...@apache.org>.
jaspertian commented on issue #10967:
URL: https://github.com/apache/apisix/issues/10967#issuecomment-1970319206

   > > Our tech-team did some modification as follow:
   > > ```
   > > local function get_real_payload(key, auth_conf, payload)
   > >     local real_payload = {
   > >         key = key,
   > >         exp = ngx_time() + auth_conf.exp
   > >     }
   > >     if payload then
   > >         local extra_payload = core.json.decode(payload)
   > > -        core.table.merge(real_payload, extra_payload)
   > > +        core.table.merge(extra_payload, real_payload)
   > > +        return extra_payload
   > >     end
   > >     return real_payload
   > > end
   > > ```
   > 
   > LGTM. Would you like to create a PR to fix it?
   
   Ok, I'll try it. 
   
   It will be my first GITHUB PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org