Skip to content

Commit

Permalink
Merge pull request #1855 from hxy7yx/main-1
Browse files Browse the repository at this point in the history
chore: improve parameter validation for API
  • Loading branch information
fengzeroz authored Feb 22, 2024
2 parents 91b847d + cb4d477 commit 0c80221
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 129 deletions.
4 changes: 2 additions & 2 deletions ft/tag_test.robot
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ Update tag, it should return success.
Delete tag from non-existent group, it should return success
${res}= Del Tags modbus-node group1 "tag1"

Check Response Status ${res} 200
Check Error Code ${res} ${NEU_ERR_SUCCESS}
Check Response Status ${res} 404
Check Error Code ${res} ${NEU_ERR_GROUP_NOT_EXIST}

Update non-existent tag, it should return failure.
${res}= Update Tags modbus-node group ${tag4}
Expand Down
29 changes: 29 additions & 0 deletions include/neuron/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@
#define NEU_LOG_LEVEL_ERROR "error"
#define NEU_LOG_LEVEL_FATAL "fatal"

#define CHECK_NODE_NAME_LENGTH_ERR \
do { \
NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NAME_TOO_LONG, { \
neu_http_response(aio, NEU_ERR_NODE_NAME_TOO_LONG, result_error); \
}); \
} while (0)

#define CHECK_GROUP_NAME_LENGTH_ERR \
do { \
NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, { \
neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG, result_error); \
}); \
} while (0)

#define CHECK_TAG_NAME_LENGTH_ERR \
do { \
NEU_JSON_RESPONSE_ERROR(NEU_ERR_TAG_NAME_TOO_LONG, { \
neu_http_response(aio, NEU_ERR_TAG_NAME_TOO_LONG, result_error); \
}); \
} while (0)

#define CHECK_GROUP_INTERVAL_ERR \
do { \
NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_PARAMETER_INVALID, { \
neu_http_response(aio, NEU_ERR_GROUP_PARAMETER_INVALID, \
result_error); \
}); \
} while (0)

extern int default_log_level;
extern bool disable_jwt;
extern char host_port[32];
Expand Down
94 changes: 51 additions & 43 deletions plugins/restful/adapter_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ void handle_add_adapter(nng_aio *aio)
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_add_node_req_t, neu_json_decode_add_node_req, {
if (strlen(req->name) >= NEU_NODE_NAME_LEN) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_NODE_NAME_TOO_LONG, {
neu_http_response(aio, NEU_ERR_NODE_NAME_TOO_LONG,
result_error);
});

CHECK_NODE_NAME_LENGTH_ERR;
} else {
int ret = 0;
neu_reqresp_head_t header = { 0 };
Expand Down Expand Up @@ -121,18 +117,22 @@ void handle_del_adapter(nng_aio *aio)

NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_del_node_req_t, neu_json_decode_del_node_req, {
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_del_node_t cmd = { 0 };

header.ctx = aio;
header.type = NEU_REQ_DEL_NODE;
strcpy(cmd.node, req->name);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
if (strlen(req->name) >= NEU_NODE_NAME_LEN) {
CHECK_NODE_NAME_LENGTH_ERR;
} else {
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_del_node_t cmd = { 0 };

header.ctx = aio;
header.type = NEU_REQ_DEL_NODE;
strcpy(cmd.node, req->name);
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
}
}
})
}
Expand Down Expand Up @@ -210,20 +210,24 @@ void handle_set_node_setting(nng_aio *aio)

NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_node_setting_req_t, neu_json_decode_node_setting_req, {
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_node_setting_t cmd = { 0 };

header.ctx = aio;
header.type = NEU_REQ_NODE_SETTING;
strcpy(cmd.node, req->node);
cmd.setting = req->setting;
req->setting = NULL; // ownership moved
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
CHECK_NODE_NAME_LENGTH_ERR;
} else {
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_node_setting_t cmd = { 0 };

header.ctx = aio;
header.type = NEU_REQ_NODE_SETTING;
strcpy(cmd.node, req->node);
cmd.setting = req->setting;
req->setting = NULL; // ownership moved
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
}
}
})
}
Expand Down Expand Up @@ -287,20 +291,24 @@ void handle_node_ctl(nng_aio *aio)

NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_node_ctl_req_t, neu_json_decode_node_ctl_req, {
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_node_ctl_t cmd = { 0 };
if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
CHECK_NODE_NAME_LENGTH_ERR;
} else {
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_node_ctl_t cmd = { 0 };

header.ctx = aio;
header.type = NEU_REQ_NODE_CTL;
strcpy(cmd.node, req->node);
cmd.ctl = req->cmd;
header.ctx = aio;
header.type = NEU_REQ_NODE_CTL;
strcpy(cmd.node, req->node);
cmd.ctl = req->cmd;

ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
ret = neu_plugin_op(plugin, header, &cmd);
if (ret != 0) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
}
}
})
}
Expand Down
54 changes: 41 additions & 13 deletions plugins/restful/datatag_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ void handle_add_tags(nng_aio *aio)

NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_add_tags_req_t, neu_json_decode_add_tags_req, {
if (strlen(req->group) >= NEU_GROUP_NAME_LEN) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_GROUP_NAME_TOO_LONG, {
neu_http_response(aio, NEU_ERR_GROUP_NAME_TOO_LONG,
result_error);
});
if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
CHECK_NODE_NAME_LENGTH_ERR;
} else if (strlen(req->group) >= NEU_GROUP_NAME_LEN) {
CHECK_GROUP_NAME_LENGTH_ERR;
} else {
int ret = 0;
neu_reqresp_head_t header = { 0 };
Expand Down Expand Up @@ -110,6 +109,11 @@ void handle_add_gtags(nng_aio *aio)
header.ctx = aio;
header.type = NEU_REQ_ADD_GTAG;

if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
err_type = NEU_ERR_NODE_NAME_TOO_LONG;
goto error;
}

for (int i = 0; i < req->n_group; i++) {
if (strlen(req->groups[i].group) >= NEU_GROUP_NAME_LEN) {
err_type = NEU_ERR_GROUP_NAME_TOO_LONG;
Expand All @@ -123,8 +127,8 @@ void handle_add_gtags(nng_aio *aio)

strcpy(cmd.driver, req->node);
cmd.n_group = req->n_group;
cmd.groups = calloc(req->n_group, sizeof(neu_gdatatag_t));

cmd.groups = calloc(req->n_group, sizeof(neu_gdatatag_t));
for (int i = 0; i < req->n_group; i++) {
strcpy(cmd.groups[i].group, req->groups[i].group);
cmd.groups[i].n_tag = req->groups[i].n_tag;
Expand Down Expand Up @@ -166,11 +170,9 @@ void handle_add_gtags(nng_aio *aio)
});
}
goto success;

error:
NEU_JSON_RESPONSE_ERROR(
err_type, { neu_http_response(aio, err_type, result_error); });

success:;
})
}
Expand All @@ -179,9 +181,8 @@ void handle_add_gtags_resp(nng_aio *aio, neu_resp_add_tag_t *resp)
{
neu_json_add_gtag_res_t res = { 0 };
char * result = NULL;

res.error = resp->error;
res.index = resp->index;
res.error = resp->error;
res.index = resp->index;

neu_json_encode_by_fn(&res, neu_json_encode_au_gtags_resp, &result);
NEU_JSON_RESPONSE_ERROR(resp->error,
Expand All @@ -198,13 +199,33 @@ void handle_del_tags(nng_aio *aio)
int ret = 0;
neu_reqresp_head_t header = { 0 };
neu_req_del_tag_t cmd = { 0 };
int err_type;

header.ctx = aio;
header.type = NEU_REQ_DEL_TAG;

if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
err_type = NEU_ERR_NODE_NAME_TOO_LONG;
goto error;
}

if (strlen(req->group) >= NEU_GROUP_NAME_LEN) {
err_type = NEU_ERR_GROUP_NAME_TOO_LONG;
goto error;
}

strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
cmd.n_tag = req->n_tags;
cmd.tags = calloc(req->n_tags, sizeof(char *));

for (int i = 0; i < req->n_tags; i++) {
if (strlen(req->tags[i]) >= NEU_TAG_ADDRESS_LEN) {
err_type = NEU_ERR_TAG_NAME_TOO_LONG;
goto error;
}
}

cmd.tags = calloc(req->n_tags, sizeof(char *));

for (int i = 0; i < req->n_tags; i++) {
cmd.tags[i] = strdup(req->tags[i]);
Expand All @@ -216,6 +237,12 @@ void handle_del_tags(nng_aio *aio)
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
}
goto success;

error:
NEU_JSON_RESPONSE_ERROR(
err_type, { neu_http_response(aio, err_type, result_error); });
success:;
})
}

Expand All @@ -231,6 +258,7 @@ void handle_update_tags(nng_aio *aio)

header.ctx = aio;
header.type = NEU_REQ_UPDATE_TAG;

strcpy(cmd.driver, req->node);
strcpy(cmd.group, req->group);
cmd.n_tag = req->n_tag;
Expand Down Expand Up @@ -345,4 +373,4 @@ void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags)
free(result);
free(tags_res.tags);
utarray_free(tags->tags);
}
}
Loading

0 comments on commit 0c80221

Please sign in to comment.