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 1 commit
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
74 changes: 22 additions & 52 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 Down