Skip to content

fix bug in patten search; enable set in http scope; enable work with … #8

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ It is possible to set a default value using symbol "*". In this case flags will
--- | ---
**Syntax** | **set_cookie_flag** \<cookie_name\|*\> [HttpOnly] [secure] [SameSite\|SameSite=[Lax\|Strict]];
**Default** | -
**Context** | server, location
**Context** | http, server, location

Description: Add flag to desired cookie.

## Author
Anton Saraykin [<Airisenator@gmail.com>]
Anton Saraykin [<Airisenator@gmail.com>]
1 change: 1 addition & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ if test -n "$ngx_module_link"; then
ngx_module_type=HTTP_FILTER
ngx_module_name=$ngx_addon_name
ngx_module_srcs="$ngx_addon_dir/$ngx_addon_name.c"
ngx_module_order="$ngx_module_name ngx_http_userid_filter_module"

. auto/module
else
Expand Down
110 changes: 41 additions & 69 deletions ngx_http_cookie_flag_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static ngx_command_t ngx_http_cookie_flag_filter_commands[] = {

/* set cookie flag directive */
{ ngx_string("set_cookie_flag"),
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_cookie_flag_filter_cmd,
NGX_HTTP_LOC_CONF_OFFSET,
0,
Expand Down Expand Up @@ -235,57 +235,27 @@ ngx_http_cookie_flag_filter_init(ngx_conf_t *cf)
static ngx_int_t
ngx_http_cookie_flag_filter_append(ngx_http_request_t *r, ngx_http_cookie_t *cookie, ngx_table_elt_t *header)
{
ngx_str_t tmp;

if (cookie->httponly == 1 && ngx_strcasestrn(header->value.data, "; HttpOnly", 10 - 1) == NULL) {
tmp.data = ngx_pnalloc(r->pool, header->value.len + sizeof("; HttpOnly") - 1);
if (tmp.data == NULL) {
return NGX_ERROR;
}
tmp.len = ngx_sprintf(tmp.data, "%V; HttpOnly", &header->value) - tmp.data;
header->value.data = tmp.data;
header->value.len = tmp.len;
}

if (cookie->secure == 1 && ngx_strcasestrn(header->value.data, "; secure", 8 - 1) == NULL) {
tmp.data = ngx_pnalloc(r->pool, header->value.len + sizeof("; secure") - 1);
if (tmp.data == NULL) {
return NGX_ERROR;
}
tmp.len = ngx_sprintf(tmp.data, "%V; secure", &header->value) - tmp.data;
header->value.data = tmp.data;
header->value.len = tmp.len;
}

if (cookie->samesite == 1 && ngx_strcasestrn(header->value.data, "; SameSite", 10 - 1) == NULL) {
tmp.data = ngx_pnalloc(r->pool, header->value.len + sizeof("; SameSite") - 1);
if (tmp.data == NULL) {
return NGX_ERROR;
}
tmp.len = ngx_sprintf(tmp.data, "%V; SameSite", &header->value) - tmp.data;
header->value.data = tmp.data;
header->value.len = tmp.len;
}

if (cookie->samesite_lax == 1 && ngx_strcasestrn(header->value.data, "; SameSite=Lax", 14 - 1) == NULL) {
tmp.data = ngx_pnalloc(r->pool, header->value.len + sizeof("; SameSite=Lax") - 1);
if (tmp.data == NULL) {
return NGX_ERROR;
}
tmp.len = ngx_sprintf(tmp.data, "%V; SameSite=Lax", &header->value) - tmp.data;
header->value.data = tmp.data;
header->value.len = tmp.len;
}

if (cookie->samesite_strict == 1 && ngx_strcasestrn(header->value.data, "; SameSite=Strict", 17 - 1) == NULL) {
tmp.data = ngx_pnalloc(r->pool, header->value.len + sizeof("; SameSite=Strict") - 1);
if (tmp.data == NULL) {
return NGX_ERROR;
}
tmp.len = ngx_sprintf(tmp.data, "%V; SameSite=Strict", &header->value) - tmp.data;
header->value.data = tmp.data;
header->value.len = tmp.len;
}
u_char *p, *last;

#define NGX_CHECK_APPEND_COOKIE_FLAG(a, b) do { \
last = header->value.data + header->value.len; \
if (cookie->a == 1 && ngx_strlcasestrn(header->value.data, last, \
(u_char *)b, sizeof(b) - 1) == NULL) { \
p = ngx_pnalloc(r->pool, header->value.len + sizeof(b)); \
if (p == NULL) { \
return NGX_ERROR; \
} \
header->value.len = ngx_sprintf(p, "%V" b, &header->value) - p; \
header->value.data = p; \
p[header->value.len] = '\0'; \
} \
} while(0)
Copy link

@edrandall edrandall Feb 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the 'do..while' loop here, the original code has no such loop?
It's hard to follow how the original code maps into the macro refactoring.
Personally I would avoid large #define's and refactor the repeated-code-blocks into a function call, then add a unit test.


NGX_CHECK_APPEND_COOKIE_FLAG(httponly, "; HttpOnly");
NGX_CHECK_APPEND_COOKIE_FLAG(secure, "; secure");
NGX_CHECK_APPEND_COOKIE_FLAG(samesite, "; SameSite");
NGX_CHECK_APPEND_COOKIE_FLAG(samesite_lax, "; SameSite=Lax");
NGX_CHECK_APPEND_COOKIE_FLAG(samesite_strict, "; SameSite=Strict");

return NGX_OK;
}
Expand All @@ -296,6 +266,7 @@ ngx_http_cookie_flag_filter_handler(ngx_http_request_t *r)
ngx_http_cookie_flag_filter_loc_conf_t *flcf;
ngx_http_cookie_t *cookie;
ngx_uint_t i, j;
ngx_str_t *name;
ngx_list_part_t *part;
ngx_table_elt_t *header;

Expand Down Expand Up @@ -331,26 +302,27 @@ ngx_http_cookie_flag_filter_handler(ngx_http_request_t *r)

// for each security cookie we check whether preset it within Set-Cookie value. If not then we append.
for (j = 0; j < flcf->cookies->nelts; j++) {
name = &cookie[j].cookie_name;

if (ngx_strncasecmp(cookie[j].cookie_name.data, (u_char *) "*", 1) != 0) {
// append "=" to the security cookie name. The result will be something like "cookie_name="
char *cookie_name = ngx_pnalloc(r->pool, sizeof("=") - 1 + cookie[j].cookie_name.len);
if (cookie_name == NULL) {
return NGX_ERROR;
if (name->len != 1 || name->data[0] != '*') {
if (header[i].value.len <= name->len + sizeof("=") - 1) {
continue;
}
strcpy(cookie_name, (char *) cookie[j].cookie_name.data);
strcat(cookie_name, "=");

// if Set-Cookie contains a cookie from settings
if (ngx_strcasestrn(header[i].value.data, cookie_name, strlen(cookie_name) - 1) != NULL) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "filter http_cookie_flag - add flags for cookie \"%V\"", &cookie[j].cookie_name);
ngx_int_t res = ngx_http_cookie_flag_filter_append(r, &cookie[j], &header[i]);
if (res != NGX_OK) {
return NGX_ERROR;
}
break; // otherwise default value will be added

if (ngx_strncasecmp(name->data, header[i].value.data,
name->len) != 0 ||
header[i].value.data[name->len] != '=')
{
continue;
}

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "filter http_cookie_flag - add flags for cookie \"%V\"", name);
ngx_int_t res = ngx_http_cookie_flag_filter_append(r, &cookie[j], &header[i]);
if (res != NGX_OK) {
return NGX_ERROR;
}
} else if (ngx_strncasecmp(cookie[j].cookie_name.data, (u_char *) "*", 1) == 0) {
break; // otherwise default value will be added
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "filter http_cookie_flag - add default cookie flags");
ngx_int_t res = ngx_http_cookie_flag_filter_append(r, &cookie[j], &header[i]);
if (res != NGX_OK) {
Expand Down