simplefiles

A lightweight C CGI web application for managing files.
git clone https://scm.kuandu.systems/git-raw/simplefiles.git
Log | Files | Refs | README | LICENSE

common.c (3746B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 
      6 #include "common.h"
      7 
      8 static int
      9 write_config(const char *path, const char *content)
     10 {
     11 	FILE *fp;
     12 
     13 	fp = fopen(path, "w");
     14 	if (fp == NULL)
     15 		return -1;
     16 
     17 	if (fputs(content, fp) == EOF) {
     18 		fclose(fp);
     19 		return -1;
     20 	}
     21 
     22 	fclose(fp);
     23 	return 0;
     24 }
     25 
     26 static int
     27 test_csrf_missing_config(void)
     28 {
     29 	char path[] = "/tmp/simplefiles-common-test-XXXXXX";
     30 	int fd;
     31 	int rc;
     32 
     33 	fd = mkstemp(path);
     34 	if (fd == -1) {
     35 		fprintf(stderr,
     36 		    "FAIL: csrf missing config: mkstemp failed\n");
     37 		return 1;
     38 	}
     39 
     40 	close(fd);
     41 
     42 	/*
     43 	 * Empty config file means CSRF_ORIGIN is not configured.
     44 	 */
     45 	setenv("SIMPLEFILES_CONFIG", path, 1);
     46 	unsetenv("HTTP_ORIGIN");
     47 
     48 	rc = check_csrf_origin();
     49 
     50 	unlink(path);
     51 	unsetenv("SIMPLEFILES_CONFIG");
     52 
     53 	if (rc != -2) {
     54 		fprintf(stderr,
     55 		    "FAIL: csrf missing config returned %d, expected -2\n",
     56 		    rc);
     57 		return 1;
     58 	}
     59 
     60 	printf("ok: csrf missing config\n");
     61 	return 0;
     62 }
     63 
     64 static int
     65 test_csrf_missing_origin(void)
     66 {
     67 	char path[] = "/tmp/simplefiles-common-test-XXXXXX";
     68 	int fd;
     69 	int rc;
     70 
     71 	fd = mkstemp(path);
     72 	if (fd == -1) {
     73 		fprintf(stderr,
     74 		    "FAIL: csrf missing origin: mkstemp failed\n");
     75 		return 1;
     76 	}
     77 
     78 	close(fd);
     79 
     80 	if (write_config(path,
     81 	    "CSRF_ORIGIN=http://localhost:8000\n") == -1) {
     82 		fprintf(stderr,
     83 		    "FAIL: csrf missing origin: could not write config\n");
     84 		unlink(path);
     85 		return 1;
     86 	}
     87 
     88 	setenv("SIMPLEFILES_CONFIG", path, 1);
     89 	unsetenv("HTTP_ORIGIN");
     90 
     91 	rc = check_csrf_origin();
     92 
     93 	unlink(path);
     94 	unsetenv("SIMPLEFILES_CONFIG");
     95 
     96 	if (rc != -1) {
     97 		fprintf(stderr,
     98 		    "FAIL: csrf missing origin returned %d, expected -1\n",
     99 		    rc);
    100 		return 1;
    101 	}
    102 
    103 	printf("ok: csrf missing origin\n");
    104 	return 0;
    105 }
    106 
    107 static int
    108 test_csrf_wrong_origin(void)
    109 {
    110 	char path[] = "/tmp/simplefiles-common-test-XXXXXX";
    111 	int fd;
    112 	int rc;
    113 
    114 	fd = mkstemp(path);
    115 	if (fd == -1) {
    116 		fprintf(stderr,
    117 		    "FAIL: csrf wrong origin: mkstemp failed\n");
    118 		return 1;
    119 	}
    120 
    121 	close(fd);
    122 
    123 	if (write_config(path,
    124 	    "CSRF_ORIGIN=http://localhost:8000\n") == -1) {
    125 		fprintf(stderr,
    126 		    "FAIL: csrf wrong origin: could not write config\n");
    127 		unlink(path);
    128 		return 1;
    129 	}
    130 
    131 	setenv("SIMPLEFILES_CONFIG", path, 1);
    132 	setenv("HTTP_ORIGIN",
    133 	    "http://evil.example.com",
    134 	    1);
    135 
    136 	rc = check_csrf_origin();
    137 
    138 	unlink(path);
    139 	unsetenv("SIMPLEFILES_CONFIG");
    140 	unsetenv("HTTP_ORIGIN");
    141 
    142 	if (rc != -1) {
    143 		fprintf(stderr,
    144 		    "FAIL: csrf wrong origin returned %d, expected -1\n",
    145 		    rc);
    146 		return 1;
    147 	}
    148 
    149 	printf("ok: csrf wrong origin\n");
    150 	return 0;
    151 }
    152 
    153 static int
    154 test_csrf_correct_origin(void)
    155 {
    156 	char path[] = "/tmp/simplefiles-common-test-XXXXXX";
    157 	int fd;
    158 	int rc;
    159 
    160 	fd = mkstemp(path);
    161 	if (fd == -1) {
    162 		fprintf(stderr,
    163 		    "FAIL: csrf correct origin: mkstemp failed\n");
    164 		return 1;
    165 	}
    166 
    167 	close(fd);
    168 
    169 	if (write_config(path,
    170 	    "CSRF_ORIGIN=http://localhost:8000\n") == -1) {
    171 		fprintf(stderr,
    172 		    "FAIL: csrf correct origin: could not write config\n");
    173 		unlink(path);
    174 		return 1;
    175 	}
    176 
    177 	setenv("SIMPLEFILES_CONFIG", path, 1);
    178 	setenv("HTTP_ORIGIN",
    179 	    "http://localhost:8000",
    180 	    1);
    181 
    182 	rc = check_csrf_origin();
    183 
    184 	unlink(path);
    185 	unsetenv("SIMPLEFILES_CONFIG");
    186 	unsetenv("HTTP_ORIGIN");
    187 
    188 	if (rc != 0) {
    189 		fprintf(stderr,
    190 		    "FAIL: csrf correct origin returned %d, expected 0\n",
    191 		    rc);
    192 		return 1;
    193 	}
    194 
    195 	printf("ok: csrf correct origin\n");
    196 	return 0;
    197 }
    198 
    199 int
    200 main(void)
    201 {
    202 	int failures = 0;
    203 
    204 	failures += test_csrf_missing_config();
    205 	failures += test_csrf_missing_origin();
    206 	failures += test_csrf_wrong_origin();
    207 	failures += test_csrf_correct_origin();
    208 
    209 	if (failures != 0) {
    210 		fprintf(stderr,
    211 		    "%d test(s) failed\n",
    212 		    failures);
    213 		return EXIT_FAILURE;
    214 	}
    215 
    216 	printf("all tests passed\n");
    217 	return EXIT_SUCCESS;
    218 }