Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: gvm_json_obj_str #877

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 13 additions & 46 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,13 +1094,7 @@
cJSON *result_obj = NULL;
const gchar *err = NULL;
openvasd_result_t result = NULL;
gchar *type = NULL;
gchar *ip_address = NULL;
gchar *hostname = NULL;
gchar *oid = NULL;
int port = 0;
gchar *protocol = NULL;
gchar *message = NULL;
gchar *detail_name = NULL;
gchar *detail_value = NULL;
gchar *detail_source_type = NULL;
Expand All @@ -1126,34 +1120,10 @@
// error
goto res_cleanup;

if ((item = cJSON_GetObjectItem (result_obj, "type")) != NULL
&& cJSON_IsString (item))
type = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (result_obj, "ip_address")) != NULL
&& cJSON_IsString (item))
ip_address = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (result_obj, "hostname")) != NULL
&& cJSON_IsString (item))
hostname = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (result_obj, "oid")) != NULL
&& cJSON_IsString (item))
oid = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (result_obj, "port")) != NULL
&& cJSON_IsNumber (item))
port = item->valueint;

if ((item = cJSON_GetObjectItem (result_obj, "protocol")) != NULL
&& cJSON_IsString (item))
protocol = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (result_obj, "message")) != NULL
&& cJSON_IsString (item))
message = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (result_obj, "detail")) != NULL
&& cJSON_IsObject (item))
{
Expand Down Expand Up @@ -1188,8 +1158,14 @@
}

result = openvasd_result_new (gvm_json_obj_double (result_obj, "id"),
type, ip_address, hostname, oid, port,
protocol, message, detail_name, detail_value,
gvm_json_obj_str (result_obj, "type"),
gvm_json_obj_str (result_obj, "ip_address"),
gvm_json_obj_str (result_obj, "hostname"),
gvm_json_obj_str (result_obj, "oid"),
port,
gvm_json_obj_str (result_obj, "protocol"),
gvm_json_obj_str (result_obj, "message"),
detail_name, detail_value,
detail_source_type, detail_source_name,
detail_source_description);

Expand Down Expand Up @@ -1811,23 +1787,11 @@

cJSON_ArrayForEach (param_obj, parser)
{
const gchar *id = NULL, *name = NULL, *desc = NULL;
gchar *defval = NULL, *param_type = NULL;
openvasd_param_t *param = NULL;
int val, mandatory = 0;
char buf[6];
cJSON *item = NULL;
if ((item = cJSON_GetObjectItem (param_obj, "id")) != NULL
&& cJSON_IsString (item))
id = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (param_obj, "name")) != NULL
&& cJSON_IsString (item))
name = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (param_obj, "description")) != NULL
&& cJSON_IsString (item))
desc = g_strdup (item->valuestring);

if ((item = cJSON_GetObjectItem (param_obj, "default")) != NULL)
{
Expand Down Expand Up @@ -1861,8 +1825,11 @@
}

param =
openvasd_param_new (g_strdup (id), g_strdup (name), g_strdup (defval),
g_strdup (desc), g_strdup (param_type), mandatory);
openvasd_param_new (g_strdup (gvm_json_obj_str (param_obj, "id")),
g_strdup (gvm_json_obj_str (param_obj, "name")),

Check warning on line 1829 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1828-L1829

Added lines #L1828 - L1829 were not covered by tests
g_strdup (defval),
g_strdup (gvm_json_obj_str (param_obj, "description")),

Check warning on line 1831 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1831

Added line #L1831 was not covered by tests
g_strdup (param_type), mandatory);
g_free (defval);
g_free (param_type);
*params = g_slist_append (*params, param);
Expand Down
27 changes: 8 additions & 19 deletions openvasd/vtparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
{
if (cJSON_IsObject (tag_obj))
{
cJSON *item = NULL;
cJSON *item;
gchar *severity_vector;

if ((item = cJSON_GetObjectItem (tag_obj, "affected")) != NULL
&& cJSON_IsString (item))
nvti_set_affected (nvt, item->valuestring);
Expand Down Expand Up @@ -128,24 +130,14 @@
nvti_set_detection (nvt, item->valuestring);

// Parse severity
gchar *severity_vector = NULL;

if ((item = cJSON_GetObjectItem (tag_obj, "severity_vector")) != NULL
&& cJSON_IsString (item))
severity_vector = item->valuestring;

severity_vector = gvm_json_obj_str (tag_obj, "severity_vector");

Check warning on line 134 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L134

Added line #L134 was not covered by tests
if (!severity_vector)
{
if ((item = cJSON_GetObjectItem (tag_obj, "cvss_base_vector")) != NULL
&& cJSON_IsString (item))
severity_vector = item->valuestring;
}
severity_vector = gvm_json_obj_str (tag_obj, "cvss_base_vector");

Check warning on line 136 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L136

Added line #L136 was not covered by tests

if (severity_vector)
{
gchar *severity_origin = NULL, *severity_type = NULL;
gchar *cvss_base;

gchar *severity_type, *cvss_base;
double cvss_base_dbl;

if (g_strrstr (severity_vector, "CVSS:3"))
Expand All @@ -155,12 +147,9 @@

cvss_base_dbl = get_cvss_score_from_base_metrics (severity_vector);

if ((item = cJSON_GetObjectItem (tag_obj, "severity_origin")) != NULL
&& cJSON_IsString (item))
severity_origin = item->valuestring;

nvti_add_vtseverity (
nvt, vtseverity_new (severity_type, severity_origin,
nvt, vtseverity_new (severity_type,
gvm_json_obj_str (tag_obj, "severity_origin"),

Check warning on line 152 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L152

Added line #L152 was not covered by tests
gvm_json_obj_double (tag_obj, "severity_date"),
cvss_base_dbl, severity_vector));

Expand Down
20 changes: 20 additions & 0 deletions util/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,23 @@ gvm_json_obj_double (cJSON *obj, const gchar *key)

return 0;
}

/**
* @brief Get a string field from a JSON object.
*
* @param[in] obj Object
* @param[in] key Field name.
*
* @return A string. Will be freed by cJSON_Delete.
*/
gchar *
gvm_json_obj_str (cJSON *obj, const gchar *key)
{
cJSON *item;

item = cJSON_GetObjectItem (obj, key);
if (item && cJSON_IsString (item))
return item->valuestring;

return 0;
}
3 changes: 3 additions & 0 deletions util/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ gvm_json_string_escape (const char *, gboolean);
double
gvm_json_obj_double (cJSON *, const gchar *);

gchar *
gvm_json_obj_str (cJSON *, const gchar *);

#endif /* _GVM_JSON_H */
29 changes: 29 additions & 0 deletions util/json_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ Ensure (json, gvm_json_obj_double_0_when_missing)
assert_that_double (d, is_equal_to_double (0));
}

/* gvm_json_obj_str */

Ensure (json, gvm_json_obj_str_gets_value)
{
cJSON *json;
const gchar *s;

json = cJSON_Parse ("{ \"eg\": \"abc\" }");
assert_that (json, is_not_null);
s = gvm_json_obj_str (json, "eg");
assert_that (s, is_equal_to_string ("abc"));
cJSON_Delete (json);
}

Ensure (json, gvm_json_obj_str_null_when_missing)
{
cJSON *json;
const gchar *s;

json = cJSON_Parse ("{ \"eg\": \"abc\" }");
assert_that (json, is_not_null);
s = gvm_json_obj_str (json, "err");
assert_that (s, is_null);
cJSON_Delete (json);
}

int
main (int argc, char **argv)
{
Expand All @@ -74,6 +100,9 @@ main (int argc, char **argv)
add_test_with_context (suite, json, gvm_json_obj_double_gets_value);
add_test_with_context (suite, json, gvm_json_obj_double_0_when_missing);

add_test_with_context (suite, json, gvm_json_obj_str_gets_value);
add_test_with_context (suite, json, gvm_json_obj_str_null_when_missing);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());
return run_test_suite (suite, create_text_reporter ());
Expand Down
Loading