Skip to content

Added SameSite=none #13

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 3 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 @@ -52,11 +52,11 @@ 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]];
**Syntax** | **set_cookie_flag** \<cookie_name\|*\> [HttpOnly] [secure] [SameSite\|SameSite=[Lax\|Strict|none]];
**Default** | -
**Context** | server, location

Description: Add flag to desired cookie.

## Author
Anton Saraykin [<[email protected]>]
Anton Saraykin [<[email protected]>]
14 changes: 14 additions & 0 deletions ngx_http_cookie_flag_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef struct {
ngx_flag_t samesite;
ngx_flag_t samesite_lax;
ngx_flag_t samesite_strict;
ngx_flag_t samesite_none;
} ngx_http_cookie_t;

typedef struct {
Expand Down Expand Up @@ -165,6 +166,7 @@ ngx_http_cookie_flag_filter_cmd(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
cookie->samesite = 0;
cookie->samesite_lax = 0;
cookie->samesite_strict = 0;
cookie->samesite_none = 0;

// normalize and check 2nd and 3rd parameters
for (i = 2; i < cf->args->nelts; i++) {
Expand All @@ -178,6 +180,8 @@ ngx_http_cookie_flag_filter_cmd(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
cookie->samesite_lax = 1;
} else if (ngx_strncasecmp(value[i].data, (u_char *) "samesite=strict", 15) == 0 && value[i].len == 15) {
cookie->samesite_strict = 1;
} else if (ngx_strncasecmp(value[i].data, (u_char *) "samesite=none", 13) == 0 && value[i].len == 13) {
cookie->samesite_none = 1;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "The parameter value \"%V\" is incorrect", &value[i]);
return NGX_CONF_ERROR;
Expand Down Expand Up @@ -287,6 +291,16 @@ ngx_http_cookie_flag_filter_append(ngx_http_request_t *r, ngx_http_cookie_t *coo
header->value.len = tmp.len;
}

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

return NGX_OK;
}

Expand Down