comparison src/c/urweb.c @ 770:c125df6fabfc

Runtime URL and MIME type filtering
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 May 2009 18:20:15 -0400
parents 21f6d2e65685
children eac1974924bb
comparison
equal deleted inserted replaced
769:efceae06df17 770:c125df6fabfc
2435 } 2435 }
2436 2436
2437 return r; 2437 return r;
2438 } 2438 }
2439 2439
2440 extern int uw_check_url(const char *);
2441 extern int uw_check_mime(const char *);
2442
2440 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) { 2443 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
2441 return s; 2444 if (uw_check_url(s))
2445 return s;
2446 else
2447 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
2448 }
2449
2450 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
2451 if (uw_check_url(s))
2452 return s;
2453 else
2454 return NULL;
2455 }
2456
2457 int mime_format(const char *s) {
2458 for (; *s; ++s)
2459 if (!isalnum(*s) && *s != '/' && *s != '-' && *s != '.')
2460 return 0;
2461
2462 return 1;
2442 } 2463 }
2443 2464
2444 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) { 2465 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
2445 char *s2; 2466 if (!mime_format(s))
2446 2467 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
2447 for (s2 = s; *s2; ++s2) 2468
2448 if (!isalnum(*s2) && *s2 != '/' && *s2 != '-' && *s2 != '.') 2469 if (uw_check_mime(s))
2449 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character %c\n", s, *s2); 2470 return s;
2450 2471 else
2451 return s; 2472 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
2473 }
2474
2475 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
2476 if (!mime_format(s))
2477 return NULL;
2478
2479 if (uw_check_mime(s))
2480 return s;
2481 else
2482 return NULL;
2452 } 2483 }
2453 2484
2454 uw_Basis_string uw_unnull(uw_Basis_string s) { 2485 uw_Basis_string uw_unnull(uw_Basis_string s) {
2455 return s ? s : ""; 2486 return s ? s : "";
2456 } 2487 }