• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/util-lua-flowvarlib.c
1
/* Copyright (C) 2025 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
#include "suricata-common.h"
19
#include "app-layer-protos.h"
20
#include "flow-var.h"
21
#include "lauxlib.h"
22
#include "util-debug.h"
23
#include "util-lua-common.h"
24
#include "util-lua-flowvarlib.h"
25
#include "util-lua.h"
26
#include "util-var-name.h"
27
#include "detect-lua.h"
28
#include "detect-lua-extensions.h"
29

30
static const char suricata_flowvar_mt[] = "suricata:flowvar:mt";
31

32
static DetectLuaData *GetLuaData(lua_State *luastate)
UNCOV
33
{
×
UNCOV
34
    DetectLuaData *ld;
×
UNCOV
35
    lua_pushlightuserdata(luastate, (void *)&luaext_key_ld);
×
UNCOV
36
    lua_gettable(luastate, LUA_REGISTRYINDEX);
×
UNCOV
37
    ld = lua_touserdata(luastate, -1);
×
UNCOV
38
    return ld;
×
UNCOV
39
}
×
40

41
/**
42
 * \brief Register a flowvar.
43
 *
44
 * Ensures that a flowvar exists for the provided name, will be
45
 * created if needed.
46
 *
47
 * The flowvar ID is returned, however as this is most likely to be
48
 * used in the scripts "init" method, this ID is unlikely to be used.
49
 */
50
static int LuaFlowvarRegister(lua_State *L)
UNCOV
51
{
×
UNCOV
52
    DetectLuaData *ld = GetLuaData(L);
×
UNCOV
53
    const char *name = luaL_checkstring(L, 1);
×
UNCOV
54
    uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id));
×
UNCOV
55
    *flowvar_id = VarNameStoreRegister(name, VAR_TYPE_FLOW_VAR);
×
UNCOV
56
    if (*flowvar_id == 0) {
×
57
        return luaL_error(L, "failed to register flowvar");
×
58
    }
×
UNCOV
59
    ld->flowvar[ld->flowvars++] = *flowvar_id;
×
60

UNCOV
61
    luaL_getmetatable(L, suricata_flowvar_mt);
×
UNCOV
62
    lua_setmetatable(L, -2);
×
63

UNCOV
64
    return 1;
×
UNCOV
65
}
×
66

67
static int LuaFlowvarGet(lua_State *L)
UNCOV
68
{
×
UNCOV
69
    const char *name = luaL_checkstring(L, 1);
×
UNCOV
70
    uint32_t *flowvar_id = lua_newuserdata(L, sizeof(*flowvar_id));
×
UNCOV
71
    *flowvar_id = VarNameStoreLookupByName(name, VAR_TYPE_FLOW_VAR);
×
UNCOV
72
    if (*flowvar_id == 0) {
×
73
        return luaL_error(L, "flowvar does not exist");
×
74
    }
×
75

UNCOV
76
    luaL_getmetatable(L, suricata_flowvar_mt);
×
UNCOV
77
    lua_setmetatable(L, -2);
×
78

UNCOV
79
    return 1;
×
UNCOV
80
}
×
81

82
static int LuaFlowvarValue(lua_State *L)
UNCOV
83
{
×
UNCOV
84
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowvar_mt);
×
UNCOV
85
    Flow *f = LuaStateGetFlow(L);
×
UNCOV
86
    if (f == NULL) {
×
87
        return LuaCallbackError(L, "flow is NULL");
×
88
    }
×
UNCOV
89
    FlowVar *fv = FlowVarGet(f, *flowvar_id);
×
UNCOV
90
    if (fv == NULL) {
×
UNCOV
91
        lua_pushnil(L);
×
UNCOV
92
    } else {
×
UNCOV
93
        LuaPushStringBuffer(
×
UNCOV
94
                L, (const uint8_t *)fv->data.fv_str.value, (size_t)fv->data.fv_str.value_len);
×
UNCOV
95
    }
×
UNCOV
96
    return 1;
×
UNCOV
97
}
×
98

99
static int LuaFlowvarSet(lua_State *L)
UNCOV
100
{
×
UNCOV
101
    const char *value = luaL_checkstring(L, 2);
×
UNCOV
102
    const int len = (int)luaL_checkinteger(L, 3);
×
UNCOV
103
    uint32_t *flowvar_id = luaL_checkudata(L, 1, suricata_flowvar_mt);
×
UNCOV
104
    Flow *f = LuaStateGetFlow(L);
×
UNCOV
105
    if (f == NULL) {
×
106
        return luaL_error(L, "no flow");
×
107
    }
×
108

UNCOV
109
    uint8_t *buf = SCMalloc(len + 1);
×
UNCOV
110
    if (buf == NULL) {
×
111
        return luaL_error(L, "alloc failure");
×
112
    }
×
UNCOV
113
    memcpy(buf, value, len);
×
UNCOV
114
    buf[len] = '\0';
×
UNCOV
115
    FlowVarAddIdValue(f, *flowvar_id, buf, (uint16_t)len);
×
116

UNCOV
117
    return 1;
×
UNCOV
118
}
×
119

120
static const luaL_Reg flowvarlib[] = {
121
    // clang-format off
122
    { "register", LuaFlowvarRegister, },
123
    { "get", LuaFlowvarGet },
124
    { NULL, NULL, },
125
    // clang-format on
126
};
127

128
static const luaL_Reg flowvarmt[] = {
129
    // clang-format off
130
    { "value", LuaFlowvarValue, },
131
    { "set", LuaFlowvarSet, },
132
    { NULL, NULL, },
133
    // clang-format on
134
};
135

136
int LuaLoadFlowvarLib(lua_State *L)
UNCOV
137
{
×
UNCOV
138
    luaL_newmetatable(L, suricata_flowvar_mt);
×
UNCOV
139
    lua_pushvalue(L, -1);
×
UNCOV
140
    lua_setfield(L, -2, "__index");
×
UNCOV
141
    luaL_setfuncs(L, flowvarmt, 0);
×
142

UNCOV
143
    luaL_newlib(L, flowvarlib);
×
UNCOV
144
    return 1;
×
UNCOV
145
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc