diff options
| author | xXx-caillou-xXx | 2017-11-24 14:03:37 +0100 |
|---|---|---|
| committer | jvoisin | 2017-11-24 14:03:37 +0100 |
| commit | 5a224ee0c92d1639395d6a0c629316ae64226125 (patch) | |
| tree | 8925d27e2bbfa877e9fb1fc20868fbef3d009b04 /src/sp_config_keywords.c | |
| parent | 79304a29661476dc75bba07c5a83133122bbcb5c (diff) | |
Implement anti csrf measures
This is done by using the "samesite" cookie attribute.
Diffstat (limited to 'src/sp_config_keywords.c')
| -rw-r--r-- | src/sp_config_keywords.c | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/src/sp_config_keywords.c b/src/sp_config_keywords.c index 34b855a..077d78f 100644 --- a/src/sp_config_keywords.c +++ b/src/sp_config_keywords.c | |||
| @@ -105,12 +105,16 @@ int parse_global(char *line) { | |||
| 105 | return parse_keywords(sp_config_funcs_global, line); | 105 | return parse_keywords(sp_config_funcs_global, line); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | int parse_cookie_encryption(char *line) { | 108 | int parse_cookie(char *line) { |
| 109 | int ret = 0; | 109 | int ret = 0; |
| 110 | char *name = NULL; | 110 | char *samesite = NULL, *name = NULL; |
| 111 | sp_cookie *cookie = pecalloc(sizeof(sp_cookie), 1, 1); | ||
| 112 | zend_string *zend_name; | ||
| 111 | 113 | ||
| 112 | sp_config_functions sp_config_funcs_cookie_encryption[] = { | 114 | sp_config_functions sp_config_funcs_cookie_encryption[] = { |
| 113 | {parse_str, SP_TOKEN_NAME, &name}, | 115 | {parse_str, SP_TOKEN_NAME, &name}, |
| 116 | {parse_str, SP_TOKEN_SAMESITE, &samesite}, | ||
| 117 | {parse_empty, SP_TOKEN_ENCRYPT, &cookie->encrypt}, | ||
| 114 | {0}}; | 118 | {0}}; |
| 115 | 119 | ||
| 116 | ret = parse_keywords(sp_config_funcs_cookie_encryption, line); | 120 | ret = parse_keywords(sp_config_funcs_cookie_encryption, line); |
| @@ -118,25 +122,42 @@ int parse_cookie_encryption(char *line) { | |||
| 118 | return ret; | 122 | return ret; |
| 119 | } | 123 | } |
| 120 | 124 | ||
| 121 | if (0 == (SNUFFLEUPAGUS_G(config).config_snuffleupagus->cookies_env_var)) { | 125 | if (cookie->encrypt) { |
| 122 | sp_log_err("config", "You're trying to use the cookie encryption feature" | 126 | if (0 == (SNUFFLEUPAGUS_G(config).config_snuffleupagus->cookies_env_var)) { |
| 123 | "on line %zu without having set the `.cookie_env_var` option in" | 127 | sp_log_err("config", "You're trying to use the cookie encryption feature" |
| 124 | "`sp.global`: please set it first.", sp_line_no); | 128 | "on line %zu without having set the `.cookie_env_var` option in" |
| 125 | return -1; | 129 | "`sp.global`: please set it first.", sp_line_no); |
| 126 | } else if (0 == (SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key)) { | 130 | return -1; |
| 127 | sp_log_err("config", "You're trying to use the cookie encryption feature" | 131 | } else if (0 == (SNUFFLEUPAGUS_G(config).config_snuffleupagus->encryption_key)) { |
| 128 | "on line %zu without having set the `.encryption_key` option in" | 132 | sp_log_err("config", "You're trying to use the cookie encryption feature" |
| 129 | "`sp.global`: please set it first.", sp_line_no); | 133 | "on line %zu without having set the `.encryption_key` option in" |
| 134 | "`sp.global`: please set it first.", sp_line_no); | ||
| 135 | return -1; | ||
| 136 | } | ||
| 137 | } else if (!samesite) { | ||
| 138 | sp_log_err("config", "You must specify a at least one action to a cookie on line " | ||
| 139 | "%zu.", sp_line_no); | ||
| 130 | return -1; | 140 | return -1; |
| 131 | } else if (0 == strlen(name)) { | 141 | } |
| 132 | sp_log_err("config", "You must specify a cookie name to encrypt on line " | 142 | if (0 == strlen(name)) { |
| 143 | sp_log_err("config", "You must specify a cookie name on line " | ||
| 133 | "%zu.", sp_line_no); | 144 | "%zu.", sp_line_no); |
| 134 | return -1; | 145 | return -1; |
| 135 | } | 146 | } |
| 147 | if (samesite) { | ||
| 148 | if (0 == strcasecmp(samesite, SP_TOKEN_SAMESITE_LAX)) { | ||
| 149 | cookie->samesite = lax; | ||
| 150 | } else if (0 == strcasecmp(samesite, SP_TOKEN_SAMESITE_STRICT)) { | ||
| 151 | cookie->samesite = strict; | ||
| 152 | } else { | ||
| 153 | sp_log_err("config", "%s is an invalid value to samesite (expected %s or %s) on line " | ||
| 154 | "%zu.", samesite, SP_TOKEN_SAMESITE_LAX, SP_TOKEN_SAMESITE_STRICT, sp_line_no); | ||
| 155 | return -1; | ||
| 156 | } | ||
| 157 | } | ||
| 136 | 158 | ||
| 137 | zend_hash_str_add_empty_element( | 159 | zend_name = zend_string_init(name, strlen(name), 1); |
| 138 | SNUFFLEUPAGUS_G(config).config_cookie_encryption->names, name, | 160 | zend_hash_add_ptr(SNUFFLEUPAGUS_G(config).config_cookie->cookies, zend_name, cookie); |
| 139 | strlen(name)); | ||
| 140 | 161 | ||
| 141 | return SUCCESS; | 162 | return SUCCESS; |
| 142 | } | 163 | } |
