src/http/modules/ngx_http_secure_link_module.c - nginx-1.31.3 nginx/ @ 42f8df65b

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8. #include <ngx_md5.h>


  9. typedef struct {
  10.     ngx_http_complex_value_t  *variable;
  11.     ngx_http_complex_value_t  *md5;
  12.     ngx_str_t                  secret;
  13. } ngx_http_secure_link_conf_t;


  14. typedef struct {
  15.     ngx_str_t                  expires;
  16. } ngx_http_secure_link_ctx_t;


  17. static ngx_int_t ngx_http_secure_link_old_variable(ngx_http_request_t *r,
  18.     ngx_http_secure_link_conf_t *conf, ngx_http_variable_value_t *v,
  19.     uintptr_t data);
  20. static ngx_int_t ngx_http_secure_link_expires_variable(ngx_http_request_t *r,
  21.     ngx_http_variable_value_t *v, uintptr_t data);
  22. static void *ngx_http_secure_link_create_conf(ngx_conf_t *cf);
  23. static char *ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent,
  24.     void *child);
  25. static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf);


  26. static ngx_command_t  ngx_http_secure_link_commands[] = {

  27.     { ngx_string("secure_link"),
  28.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  29.       ngx_http_set_complex_value_slot,
  30.       NGX_HTTP_LOC_CONF_OFFSET,
  31.       offsetof(ngx_http_secure_link_conf_t, variable),
  32.       NULL },

  33.     { ngx_string("secure_link_md5"),
  34.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  35.       ngx_http_set_complex_value_slot,
  36.       NGX_HTTP_LOC_CONF_OFFSET,
  37.       offsetof(ngx_http_secure_link_conf_t, md5),
  38.       NULL },

  39.     { ngx_string("secure_link_secret"),
  40.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  41.       ngx_conf_set_str_slot,
  42.       NGX_HTTP_LOC_CONF_OFFSET,
  43.       offsetof(ngx_http_secure_link_conf_t, secret),
  44.       NULL },

  45.       ngx_null_command
  46. };


  47. static ngx_http_module_t  ngx_http_secure_link_module_ctx = {
  48.     ngx_http_secure_link_add_variables,    /* preconfiguration */
  49.     NULL,                                  /* postconfiguration */

  50.     NULL,                                  /* create main configuration */
  51.     NULL,                                  /* init main configuration */

  52.     NULL,                                  /* create server configuration */
  53.     NULL,                                  /* merge server configuration */

  54.     ngx_http_secure_link_create_conf,      /* create location configuration */
  55.     ngx_http_secure_link_merge_conf        /* merge location configuration */
  56. };


  57. ngx_module_t  ngx_http_secure_link_module = {
  58.     NGX_MODULE_V1,
  59.     &ngx_http_secure_link_module_ctx,      /* module context */
  60.     ngx_http_secure_link_commands,         /* module directives */
  61.     NGX_HTTP_MODULE,                       /* module type */
  62.     NULL,                                  /* init master */
  63.     NULL,                                  /* init module */
  64.     NULL,                                  /* init process */
  65.     NULL,                                  /* init thread */
  66.     NULL,                                  /* exit thread */
  67.     NULL,                                  /* exit process */
  68.     NULL,                                  /* exit master */
  69.     NGX_MODULE_V1_PADDING
  70. };


  71. static ngx_str_t  ngx_http_secure_link_name = ngx_string("secure_link");
  72. static ngx_str_t  ngx_http_secure_link_expires_name =
  73.     ngx_string("secure_link_expires");


  74. static ngx_int_t
  75. ngx_http_secure_link_variable(ngx_http_request_t *r,
  76.     ngx_http_variable_value_t *v, uintptr_t data)
  77. {
  78.     u_char                       *p, *last, ch;
  79.     time_t                        expires;
  80.     ngx_str_t                     val, hash;
  81.     ngx_md5_t                     md5;
  82.     ngx_uint_t                    i;
  83.     ngx_http_secure_link_ctx_t   *ctx;
  84.     ngx_http_secure_link_conf_t  *conf;
  85.     u_char                        hash_buf[18], md5_buf[16];

  86.     conf = ngx_http_get_module_loc_conf(r, ngx_http_secure_link_module);

  87.     if (conf->secret.data) {
  88.         return ngx_http_secure_link_old_variable(r, conf, v, data);
  89.     }

  90.     if (conf->variable == NULL || conf->md5 == NULL) {
  91.         goto not_found;
  92.     }

  93.     if (ngx_http_complex_value(r, conf->variable, &val) != NGX_OK) {
  94.         return NGX_ERROR;
  95.     }

  96.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  97.                    "secure link: \"%V\"", &val);

  98.     last = val.data + val.len;

  99.     p = ngx_strlchr(val.data, last, ',');
  100.     expires = 0;

  101.     if (p) {
  102.         val.len = p++ - val.data;

  103.         expires = ngx_atotm(p, last - p);
  104.         if (expires <= 0) {
  105.             goto not_found;
  106.         }

  107.         ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_secure_link_ctx_t));
  108.         if (ctx == NULL) {
  109.             return NGX_ERROR;
  110.         }

  111.         ngx_http_set_ctx(r, ctx, ngx_http_secure_link_module);

  112.         ctx->expires.len = last - p;
  113.         ctx->expires.data = p;
  114.     }

  115.     if (val.len > 24) {
  116.         goto not_found;
  117.     }

  118.     hash.data = hash_buf;

  119.     if (ngx_decode_base64url(&hash, &val) != NGX_OK) {
  120.         goto not_found;
  121.     }

  122.     if (hash.len != 16) {
  123.         goto not_found;
  124.     }

  125.     if (ngx_http_complex_value(r, conf->md5, &val) != NGX_OK) {
  126.         return NGX_ERROR;
  127.     }

  128.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  129.                    "secure link md5: \"%V\"", &val);

  130.     ngx_md5_init(&md5);
  131.     ngx_md5_update(&md5, val.data, val.len);
  132.     ngx_md5_final(md5_buf, &md5);

  133.     /* constant time comparison */

  134.     for (ch = 0, i = 0; i < 16; i++) {
  135.         ch |= (hash_buf[i] ^ md5_buf[i]);
  136.     }

  137.     if (ch) {
  138.         goto not_found;
  139.     }

  140.     v->data = (u_char *) ((expires && expires < ngx_time()) ? "0" : "1");
  141.     v->len = 1;
  142.     v->valid = 1;
  143.     v->no_cacheable = 0;
  144.     v->not_found = 0;

  145.     return NGX_OK;

  146. not_found:

  147.     v->not_found = 1;

  148.     return NGX_OK;
  149. }


  150. static ngx_int_t
  151. ngx_http_secure_link_old_variable(ngx_http_request_t *r,
  152.     ngx_http_secure_link_conf_t *conf, ngx_http_variable_value_t *v,
  153.     uintptr_t data)
  154. {
  155.     u_char      *p, *start, *end, *last, ch;
  156.     size_t       len;
  157.     ngx_int_t    n;
  158.     ngx_md5_t    md5;
  159.     ngx_uint_t   i;
  160.     u_char       hash[16];

  161.     p = &r->unparsed_uri.data[1];
  162.     last = r->unparsed_uri.data + r->unparsed_uri.len;

  163.     while (p < last) {
  164.         if (*p++ == '/') {
  165.             start = p;
  166.             goto md5_start;
  167.         }
  168.     }

  169.     goto not_found;

  170. md5_start:

  171.     while (p < last) {
  172.         if (*p++ == '/') {
  173.             end = p - 1;
  174.             goto url_start;
  175.         }
  176.     }

  177.     goto not_found;

  178. url_start:

  179.     len = last - p;

  180.     if (end - start != 32 || len == 0) {
  181.         goto not_found;
  182.     }

  183.     ngx_md5_init(&md5);
  184.     ngx_md5_update(&md5, p, len);
  185.     ngx_md5_update(&md5, conf->secret.data, conf->secret.len);
  186.     ngx_md5_final(hash, &md5);

  187.     for (ch = 0, i = 0; i < 16; i++) {
  188.         n = ngx_hextoi(&start[2 * i], 2);
  189.         if (n == NGX_ERROR) {
  190.             goto not_found;
  191.         }

  192.         /* constant time comparison */

  193.         ch |= (u_char) n ^ hash[i];
  194.     }

  195.     if (ch) {
  196.         goto not_found;
  197.     }

  198.     v->len = len;
  199.     v->valid = 1;
  200.     v->no_cacheable = 0;
  201.     v->not_found = 0;
  202.     v->data = p;

  203.     return NGX_OK;

  204. not_found:

  205.     v->not_found = 1;

  206.     return NGX_OK;
  207. }


  208. static ngx_int_t
  209. ngx_http_secure_link_expires_variable(ngx_http_request_t *r,
  210.     ngx_http_variable_value_t *v, uintptr_t data)
  211. {
  212.     ngx_http_secure_link_ctx_t  *ctx;

  213.     ctx = ngx_http_get_module_ctx(r, ngx_http_secure_link_module);

  214.     if (ctx) {
  215.         v->len = ctx->expires.len;
  216.         v->valid = 1;
  217.         v->no_cacheable = 0;
  218.         v->not_found = 0;
  219.         v->data = ctx->expires.data;

  220.     } else {
  221.         v->not_found = 1;
  222.     }

  223.     return NGX_OK;
  224. }


  225. static void *
  226. ngx_http_secure_link_create_conf(ngx_conf_t *cf)
  227. {
  228.     ngx_http_secure_link_conf_t  *conf;

  229.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_secure_link_conf_t));
  230.     if (conf == NULL) {
  231.         return NULL;
  232.     }

  233.     /*
  234.      * set by ngx_pcalloc():
  235.      *
  236.      *     conf->secret = { 0, NULL };
  237.      */

  238.     conf->variable = NGX_CONF_UNSET_PTR;
  239.     conf->md5 = NGX_CONF_UNSET_PTR;

  240.     return conf;
  241. }


  242. static char *
  243. ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  244. {
  245.     ngx_http_secure_link_conf_t *prev = parent;
  246.     ngx_http_secure_link_conf_t *conf = child;

  247.     if (conf->secret.data) {
  248.         ngx_conf_init_ptr_value(conf->variable, NULL);
  249.         ngx_conf_init_ptr_value(conf->md5, NULL);

  250.         if (conf->variable || conf->md5) {
  251.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  252.                                "\"secure_link_secret\" cannot be mixed with "
  253.                                "\"secure_link\" and \"secure_link_md5\"");
  254.             return NGX_CONF_ERROR;
  255.         }

  256.         return NGX_CONF_OK;
  257.     }

  258.     ngx_conf_merge_ptr_value(conf->variable, prev->variable, NULL);
  259.     ngx_conf_merge_ptr_value(conf->md5, prev->md5, NULL);

  260.     if (conf->variable == NULL && conf->md5 == NULL) {
  261.         conf->secret = prev->secret;
  262.     }

  263.     return NGX_CONF_OK;
  264. }


  265. static ngx_int_t
  266. ngx_http_secure_link_add_variables(ngx_conf_t *cf)
  267. {
  268.     ngx_http_variable_t  *var;

  269.     var = ngx_http_add_variable(cf, &ngx_http_secure_link_name, 0);
  270.     if (var == NULL) {
  271.         return NGX_ERROR;
  272.     }

  273.     var->get_handler = ngx_http_secure_link_variable;

  274.     var = ngx_http_add_variable(cf, &ngx_http_secure_link_expires_name, 0);
  275.     if (var == NULL) {
  276.         return NGX_ERROR;
  277.     }

  278.     var->get_handler = ngx_http_secure_link_expires_variable;

  279.     return NGX_OK;
  280. }