Copilot commented on code in PR #12650:
URL: https://github.com/apache/trafficserver/pull/12650#discussion_r2507601129


##########
plugins/lua/ts_lua_client_request.cc:
##########
@@ -1139,3 +1147,83 @@ ts_lua_client_request_get_ssl_curve(lua_State *L)
 
   return 1;
 }
+
+static int
+ts_lua_client_request_client_addr_get_verified_addr(lua_State *L)
+{
+  struct sockaddr const *verified_addr;
+  ts_lua_http_ctx       *http_ctx;
+  int                    family   = AF_UNSPEC;
+  char                   vip[128] = "";
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnVerifiedAddrGet(http_ctx->txnp, &verified_addr) == TS_SUCCESS) {
+    if (verified_addr->sa_family == AF_INET) {
+      inet_ntop(AF_INET, (const void *)&((struct sockaddr_in 
*)verified_addr)->sin_addr, vip, sizeof(vip));
+      family = AF_INET;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else if (verified_addr->sa_family == AF_INET6) {
+      inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 
*)verified_addr)->sin6_addr, vip, sizeof(vip));
+      family = AF_INET6;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else {
+      lua_pushnil(L);
+      lua_pushnil(L);
+    }
+  } else {
+    lua_pushnil(L);
+    lua_pushnil(L);
+  }
+
+  return 2;
+}
+
+static int
+ts_lua_client_request_client_addr_set_verified_addr(lua_State *L)
+{
+  union {
+    struct sockaddr_in  sin4;
+    struct sockaddr_in6 sin6;
+    struct sockaddr     sa;
+  } addr;
+  memset(&addr, 0, sizeof(addr));
+  ts_lua_http_ctx *http_ctx;
+  int              n;
+  int              family;
+  const char      *vip;
+  size_t           vip_len;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  n = lua_gettop(L);
+
+  if (n == 2) {
+    vip    = luaL_checklstring(L, 1, &vip_len);
+    family = luaL_checknumber(L, 2);
+
+    if (family == AF_INET) {
+      addr.sin4.sin_family = AF_INET;
+      addr.sin4.sin_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin4.sin_addr)) {

Review Comment:
   The error messages 'invalid ipv4 address' and 'invalid ipv6 address' don't 
clarify that `inet_pton` returns 0 for invalid input but also returns -1 for 
system errors. Consider distinguishing between parse failures and system 
errors, or document that this treats both cases as invalid addresses.



##########
tests/gold_tests/pluginTest/lua/verified_addr.lua:
##########
@@ -0,0 +1,87 @@
+--  Licensed to the Apache Software Foundation (ASF) under one
+--  or more contributor license agreements.  See the NOTICE file
+--  distributed with this work for additional information
+--  regarding copyright ownership.  The ASF licenses this file
+--  to you under the Apache License, Version 2.0 (the
+--  "License"); you may not use this file except in compliance
+--  with the License.  You may obtain a copy of the License at
+--
+--  http://www.apache.org/licenses/LICENSE-2.0
+--
+--  Unless required by applicable law or agreed to in writing, software
+--  distributed under the License is distributed on an "AS IS" BASIS,
+--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+--  See the License for the specific language governing permissions and
+--  limitations under the License.
+
+function do_remap()
+    local result = ""
+    
+    -- Test 1: Check if verified address is initially nil
+    local ip1, family1 = ts.client_request.client_addr.get_verified_addr()
+    if not ip1 then
+        result = result .. "initial:nil;"
+    end
+    
+    -- Test 2: Set an IPv4 verified address from X-Real-IP header
+    local real_ip = ts.client_request.header["X-Real-IP"]
+    if real_ip then
+        local success, err = pcall(function()
+            ts.client_request.client_addr.set_verified_addr(real_ip, 2)
+        end)
+        
+        if success then
+            result = result .. "set:success;"
+            
+            -- Test 3: Get the verified address we just set
+            local ip2, family2 = 
ts.client_request.client_addr.get_verified_addr()
+            if ip2 then
+                result = result .. "get:" .. ip2 .. ":" .. tostring(family2) 
.. ";"
+            else
+                result = result .. "get:failed;"
+            end
+        else
+            result = result .. "set:failed;"
+        end
+    end
+    
+    -- Test 4: Try setting an IPv6 address from X-Real-IP-V6 header
+    local real_ipv6 = ts.client_request.header["X-Real-IP-V6"]
+    if real_ipv6 then
+        local success, err = pcall(function()
+            ts.client_request.client_addr.set_verified_addr(real_ipv6, 
TS_LUA_AF_INET6)
+        end)
+        
+        if success then
+            result = result .. "setv6:success;"
+            
+            -- Get the IPv6 verified address
+            local ip3, family3 = 
ts.client_request.client_addr.get_verified_addr()
+            if ip3 then
+                result = result .. "getv6:" .. ip3 .. ":" .. tostring(family3) 
.. ";"
+            else
+                result = result .. "getv6:failed;"
+            end
+        else
+            result = result .. "setv6:failed;"
+        end
+    end
+    
+    -- Test 5: Try setting an invalid address (should fail)
+    local invalid_ip = ts.client_request.header["X-Invalid-IP"]
+    if invalid_ip then
+        local success, err = pcall(function()
+            ts.client_request.client_addr.set_verified_addr(invalid_ip, 
TS_LUA_AF_INET)

Review Comment:
   The constant `TS_LUA_AF_INET` is undefined. This should use the numeric 
value `2` (AF_INET) to be consistent with line 30.
   ```suggestion
               ts.client_request.client_addr.set_verified_addr(invalid_ip, 2)
   ```



##########
plugins/lua/ts_lua_client_request.cc:
##########
@@ -1139,3 +1147,83 @@ ts_lua_client_request_get_ssl_curve(lua_State *L)
 
   return 1;
 }
+
+static int
+ts_lua_client_request_client_addr_get_verified_addr(lua_State *L)
+{
+  struct sockaddr const *verified_addr;
+  ts_lua_http_ctx       *http_ctx;
+  int                    family   = AF_UNSPEC;
+  char                   vip[128] = "";
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  if (TSHttpTxnVerifiedAddrGet(http_ctx->txnp, &verified_addr) == TS_SUCCESS) {
+    if (verified_addr->sa_family == AF_INET) {
+      inet_ntop(AF_INET, (const void *)&((struct sockaddr_in 
*)verified_addr)->sin_addr, vip, sizeof(vip));
+      family = AF_INET;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else if (verified_addr->sa_family == AF_INET6) {
+      inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 
*)verified_addr)->sin6_addr, vip, sizeof(vip));
+      family = AF_INET6;
+      lua_pushstring(L, vip);
+      lua_pushnumber(L, family);
+    } else {
+      lua_pushnil(L);
+      lua_pushnil(L);
+    }
+  } else {
+    lua_pushnil(L);
+    lua_pushnil(L);
+  }
+
+  return 2;
+}
+
+static int
+ts_lua_client_request_client_addr_set_verified_addr(lua_State *L)
+{
+  union {
+    struct sockaddr_in  sin4;
+    struct sockaddr_in6 sin6;
+    struct sockaddr     sa;
+  } addr;
+  memset(&addr, 0, sizeof(addr));
+  ts_lua_http_ctx *http_ctx;
+  int              n;
+  int              family;
+  const char      *vip;
+  size_t           vip_len;
+
+  GET_HTTP_CONTEXT(http_ctx, L);
+
+  n = lua_gettop(L);
+
+  if (n == 2) {
+    vip    = luaL_checklstring(L, 1, &vip_len);
+    family = luaL_checknumber(L, 2);
+
+    if (family == AF_INET) {
+      addr.sin4.sin_family = AF_INET;
+      addr.sin4.sin_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin4.sin_addr)) {
+        return luaL_error(L, "invalid ipv4 address");
+      }
+    } else if (family == AF_INET6) {
+      addr.sin6.sin6_family = AF_INET6;
+      addr.sin6.sin6_port   = 0;
+      if (!inet_pton(family, vip, &addr.sin6.sin6_addr)) {
+        return luaL_error(L, "invalid ipv6 address");

Review Comment:
   The error messages 'invalid ipv4 address' and 'invalid ipv6 address' don't 
clarify that `inet_pton` returns 0 for invalid input but also returns -1 for 
system errors. Consider distinguishing between parse failures and system 
errors, or document that this treats both cases as invalid addresses.
   ```suggestion
         int pton_ret = inet_pton(family, vip, &addr.sin4.sin_addr);
         if (pton_ret == 0) {
           return luaL_error(L, "invalid ipv4 address string");
         } else if (pton_ret == -1) {
           return luaL_error(L, "system error in inet_pton for ipv4 address");
         }
       } else if (family == AF_INET6) {
         addr.sin6.sin6_family = AF_INET6;
         addr.sin6.sin6_port   = 0;
         int pton_ret = inet_pton(family, vip, &addr.sin6.sin6_addr);
         if (pton_ret == 0) {
           return luaL_error(L, "invalid ipv6 address string");
         } else if (pton_ret == -1) {
           return luaL_error(L, "system error in inet_pton for ipv6 address");
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to