Fossil SCM

Refactor the safe-html interface names. Improved comments. No functional changes.

drh 2020-06-02 12:29 trunk
Commit 54c1fd6fbb5f8dd266344659829e222f7f83f94eadef6cc040400802b3a65832
2 files changed +2 -2 +32 -13
--- src/markdown_html.c
+++ src/markdown_html.c
@@ -45,11 +45,11 @@
4545
* the error is not overly explicit.
4646
*/
4747
4848
/* BLOB_APPEND_BLOB -- append blob contents to another */
4949
#define BLOB_APPEND_BLOB(dest, src) \
50
- blob_append_safe_html((dest), blob_buffer(src), blob_size(src))
50
+ safe_html_append((dest), blob_buffer(src), blob_size(src))
5151
5252
5353
/* HTML escapes
5454
**
5555
** html_escape() converts < to &lt;, > to &gt;, and & to &amp;.
@@ -147,11 +147,11 @@
147147
int nTag = html_tag_length(data);
148148
blob_append(title, data+nTag, size - nTag - 5);
149149
return;
150150
}
151151
INTER_BLOCK(ob);
152
- blob_append_safe_html(ob, data, size);
152
+ safe_html_append(ob, data, size);
153153
BLOB_APPEND_LITERAL(ob, "\n");
154154
}
155155
156156
static void html_blockcode(struct Blob *ob, struct Blob *text, void *opaque){
157157
INTER_BLOCK(ob);
158158
--- src/markdown_html.c
+++ src/markdown_html.c
@@ -45,11 +45,11 @@
45 * the error is not overly explicit.
46 */
47
48 /* BLOB_APPEND_BLOB -- append blob contents to another */
49 #define BLOB_APPEND_BLOB(dest, src) \
50 blob_append_safe_html((dest), blob_buffer(src), blob_size(src))
51
52
53 /* HTML escapes
54 **
55 ** html_escape() converts < to &lt;, > to &gt;, and & to &amp;.
@@ -147,11 +147,11 @@
147 int nTag = html_tag_length(data);
148 blob_append(title, data+nTag, size - nTag - 5);
149 return;
150 }
151 INTER_BLOCK(ob);
152 blob_append_safe_html(ob, data, size);
153 BLOB_APPEND_LITERAL(ob, "\n");
154 }
155
156 static void html_blockcode(struct Blob *ob, struct Blob *text, void *opaque){
157 INTER_BLOCK(ob);
158
--- src/markdown_html.c
+++ src/markdown_html.c
@@ -45,11 +45,11 @@
45 * the error is not overly explicit.
46 */
47
48 /* BLOB_APPEND_BLOB -- append blob contents to another */
49 #define BLOB_APPEND_BLOB(dest, src) \
50 safe_html_append((dest), blob_buffer(src), blob_size(src))
51
52
53 /* HTML escapes
54 **
55 ** html_escape() converts < to &lt;, > to &gt;, and & to &amp;.
@@ -147,11 +147,11 @@
147 int nTag = html_tag_length(data);
148 blob_append(title, data+nTag, size - nTag - 5);
149 return;
150 }
151 INTER_BLOCK(ob);
152 safe_html_append(ob, data, size);
153 BLOB_APPEND_LITERAL(ob, "\n");
154 }
155
156 static void html_blockcode(struct Blob *ob, struct Blob *text, void *opaque){
157 INTER_BLOCK(ob);
158
+32 -13
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -2400,36 +2400,51 @@
24002400
fossil_puts(blob_str(&out), 0);
24012401
blob_reset(&out);
24022402
}
24032403
}
24042404
2405
+/****************************************************************************
2406
+** safe-html:
2407
+**
2408
+** An interface for preventing HTML constructs (ex: <style>, <form>, etc)
2409
+** from being inserted into Wiki and Forum posts using Markdown. See the
2410
+** comment on safe_html_append() for additional information on what is meant
2411
+** by "safe".
2412
+**
2413
+** The safe-html restrictions only apply to Markdown, as Fossil-Wiki only
2414
+** allows safe-html by design - unsafe-HTML is never and has never been
2415
+** allowed in Fossil-Wiki.
2416
+**
2417
+** This code is in the wikiformat.c file so that it can have access to the
2418
+** white-list of acceptable HTML in the aMarkup[] array.
2419
+*/
2420
+
24052421
/*
24062422
** An instance of this object keeps track of the nesting of HTML
2407
-** elements for blob_append_safe_html().
2423
+** elements for safe_html_append().
24082424
*/
2409
-#if LOCAL_INTERFACE
2425
+typedef struct HtmlTagStack HtmlTagStack;
24102426
struct HtmlTagStack {
24112427
int n; /* Current tag stack depth */
24122428
int nAlloc; /* Space allocated for aStack[] */
24132429
int *aStack; /* The stack of tags */
24142430
int aSpace[10]; /* Initial static space, to avoid malloc() */
24152431
};
2416
-#endif /* LOCAL_INTERFACE */
24172432
24182433
/*
24192434
** Initialize bulk memory to a valid empty tagstack.
24202435
*/
2421
-void html_tagstack_init(HtmlTagStack *p){
2436
+static void html_tagstack_init(HtmlTagStack *p){
24222437
p->n = 0;
24232438
p->nAlloc = 0;
24242439
p->aStack = p->aSpace;
24252440
}
24262441
24272442
/*
24282443
** Push a new element onto the tag statk
24292444
*/
2430
-void html_tagstack_push(HtmlTagStack *p, int e){
2445
+static void html_tagstack_push(HtmlTagStack *p, int e){
24312446
if( p->n>=ArraySize(p->aSpace) && p->n>=p->nAlloc ){
24322447
if( p->nAlloc==0 ){
24332448
int *aNew;
24342449
p->nAlloc = 50;
24352450
aNew = fossil_malloc( sizeof(p->aStack[0])*p->nAlloc );
@@ -2444,11 +2459,11 @@
24442459
}
24452460
24462461
/*
24472462
** Clear a tag stack, reclaiming any memory allocations.
24482463
*/
2449
-void html_tagstack_clear(HtmlTagStack *p){
2464
+static void html_tagstack_clear(HtmlTagStack *p){
24502465
if( p->nAlloc ){
24512466
fossil_free(p->aStack);
24522467
p->nAlloc = 0;
24532468
p->aStack = p->aSpace;
24542469
}
@@ -2463,11 +2478,11 @@
24632478
** end-tags as you go.
24642479
**
24652480
** If there is no open-tag for eEnd on the stack, then this
24662481
** routine is a no-op.
24672482
*/
2468
-void html_tagstack_pop(HtmlTagStack *p, Blob *pBlob, int eEnd){
2483
+static void html_tagstack_pop(HtmlTagStack *p, Blob *pBlob, int eEnd){
24692484
int i, e;
24702485
if( eEnd!=0 ){
24712486
for(i=p->n-1; i>=0 && p->aStack[i]!=eEnd; i--){}
24722487
if( i<0 ){
24732488
blob_appendf(pBlob, "<span class='error'>&lt;/%s&gt;</span>",
@@ -2491,22 +2506,26 @@
24912506
**
24922507
** 1. Omit any elements that are not on the AllowedMarkup list.
24932508
**
24942509
** 2. Omit any attributes that are not on the AllowedMarkup list.
24952510
**
2496
-** 3. Omit any surplus close-tags.
2511
+** 3. Omit any surplus close-tags. (This prevents a surplus </div>
2512
+** or </body> or similar element from interferring with formatting
2513
+** of the outer context in which the HTML is being inserted.)
24972514
**
2498
-** 4. Insert additional close-tags as necessary so that all
2499
-** tag in the input that needs a close-tag has one.
2515
+** 4. Insert additional close-tags as necessary so that any
2516
+** tag in the input that needs a close-tag has one. (This prevents
2517
+** the inserted HTML from messing up the formatting of subsequent
2518
+** sections of the document into which it is being inserted.)
25002519
**
25012520
** The input must be writable. Temporary changes may be made to the
25022521
** input, but the input is restored to its original state prior to
25032522
** returning. If zHtml[nHtml] is not a zero character, then a zero
25042523
** might be written in that position temporarily, but that slot will
25052524
** also be restored before this routine returns.
25062525
*/
2507
-void blob_append_safe_html(Blob *pBlob, char *zHtml, int nHtml){
2526
+void safe_html_append(Blob *pBlob, char *zHtml, int nHtml){
25082527
char cLast;
25092528
int i, j, n;
25102529
HtmlTagStack s;
25112530
ParsedMarkup markup;
25122531
@@ -2560,11 +2579,11 @@
25602579
** COMMAND: test-safe-html
25612580
**
25622581
** Usage: %fossil test-safe-html FILE ...
25632582
**
25642583
** Read files named on the command-line. Send the text of each file
2565
-** through blob_append_safe_html() and then write the result on
2584
+** through safe_html_append() and then write the result on
25662585
** standard output.
25672586
*/
25682587
void test_safe_html_cmd(void){
25692588
int i;
25702589
Blob x;
@@ -2573,14 +2592,14 @@
25732592
char *z;
25742593
int n;
25752594
blob_read_from_file(&x, g.argv[i], ExtFILE);
25762595
blob_init(&y, 0, 0);
25772596
blob_terminate(&x);
2578
- blob_append_safe_html(&y, blob_buffer(&x), blob_size(&x));
2597
+ safe_html_append(&y, blob_buffer(&x), blob_size(&x));
25792598
blob_reset(&x);
25802599
z = blob_str(&y);
25812600
n = blob_size(&y);
25822601
while( n>0 && (z[n-1]=='\n' || z[n-1]=='\r') ) n--;
25832602
fossil_print("%.*s\n", n, z);
25842603
blob_reset(&y);
25852604
}
25862605
}
25872606
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -2400,36 +2400,51 @@
2400 fossil_puts(blob_str(&out), 0);
2401 blob_reset(&out);
2402 }
2403 }
2404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2405 /*
2406 ** An instance of this object keeps track of the nesting of HTML
2407 ** elements for blob_append_safe_html().
2408 */
2409 #if LOCAL_INTERFACE
2410 struct HtmlTagStack {
2411 int n; /* Current tag stack depth */
2412 int nAlloc; /* Space allocated for aStack[] */
2413 int *aStack; /* The stack of tags */
2414 int aSpace[10]; /* Initial static space, to avoid malloc() */
2415 };
2416 #endif /* LOCAL_INTERFACE */
2417
2418 /*
2419 ** Initialize bulk memory to a valid empty tagstack.
2420 */
2421 void html_tagstack_init(HtmlTagStack *p){
2422 p->n = 0;
2423 p->nAlloc = 0;
2424 p->aStack = p->aSpace;
2425 }
2426
2427 /*
2428 ** Push a new element onto the tag statk
2429 */
2430 void html_tagstack_push(HtmlTagStack *p, int e){
2431 if( p->n>=ArraySize(p->aSpace) && p->n>=p->nAlloc ){
2432 if( p->nAlloc==0 ){
2433 int *aNew;
2434 p->nAlloc = 50;
2435 aNew = fossil_malloc( sizeof(p->aStack[0])*p->nAlloc );
@@ -2444,11 +2459,11 @@
2444 }
2445
2446 /*
2447 ** Clear a tag stack, reclaiming any memory allocations.
2448 */
2449 void html_tagstack_clear(HtmlTagStack *p){
2450 if( p->nAlloc ){
2451 fossil_free(p->aStack);
2452 p->nAlloc = 0;
2453 p->aStack = p->aSpace;
2454 }
@@ -2463,11 +2478,11 @@
2463 ** end-tags as you go.
2464 **
2465 ** If there is no open-tag for eEnd on the stack, then this
2466 ** routine is a no-op.
2467 */
2468 void html_tagstack_pop(HtmlTagStack *p, Blob *pBlob, int eEnd){
2469 int i, e;
2470 if( eEnd!=0 ){
2471 for(i=p->n-1; i>=0 && p->aStack[i]!=eEnd; i--){}
2472 if( i<0 ){
2473 blob_appendf(pBlob, "<span class='error'>&lt;/%s&gt;</span>",
@@ -2491,22 +2506,26 @@
2491 **
2492 ** 1. Omit any elements that are not on the AllowedMarkup list.
2493 **
2494 ** 2. Omit any attributes that are not on the AllowedMarkup list.
2495 **
2496 ** 3. Omit any surplus close-tags.
 
 
2497 **
2498 ** 4. Insert additional close-tags as necessary so that all
2499 ** tag in the input that needs a close-tag has one.
 
 
2500 **
2501 ** The input must be writable. Temporary changes may be made to the
2502 ** input, but the input is restored to its original state prior to
2503 ** returning. If zHtml[nHtml] is not a zero character, then a zero
2504 ** might be written in that position temporarily, but that slot will
2505 ** also be restored before this routine returns.
2506 */
2507 void blob_append_safe_html(Blob *pBlob, char *zHtml, int nHtml){
2508 char cLast;
2509 int i, j, n;
2510 HtmlTagStack s;
2511 ParsedMarkup markup;
2512
@@ -2560,11 +2579,11 @@
2560 ** COMMAND: test-safe-html
2561 **
2562 ** Usage: %fossil test-safe-html FILE ...
2563 **
2564 ** Read files named on the command-line. Send the text of each file
2565 ** through blob_append_safe_html() and then write the result on
2566 ** standard output.
2567 */
2568 void test_safe_html_cmd(void){
2569 int i;
2570 Blob x;
@@ -2573,14 +2592,14 @@
2573 char *z;
2574 int n;
2575 blob_read_from_file(&x, g.argv[i], ExtFILE);
2576 blob_init(&y, 0, 0);
2577 blob_terminate(&x);
2578 blob_append_safe_html(&y, blob_buffer(&x), blob_size(&x));
2579 blob_reset(&x);
2580 z = blob_str(&y);
2581 n = blob_size(&y);
2582 while( n>0 && (z[n-1]=='\n' || z[n-1]=='\r') ) n--;
2583 fossil_print("%.*s\n", n, z);
2584 blob_reset(&y);
2585 }
2586 }
2587
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -2400,36 +2400,51 @@
2400 fossil_puts(blob_str(&out), 0);
2401 blob_reset(&out);
2402 }
2403 }
2404
2405 /****************************************************************************
2406 ** safe-html:
2407 **
2408 ** An interface for preventing HTML constructs (ex: <style>, <form>, etc)
2409 ** from being inserted into Wiki and Forum posts using Markdown. See the
2410 ** comment on safe_html_append() for additional information on what is meant
2411 ** by "safe".
2412 **
2413 ** The safe-html restrictions only apply to Markdown, as Fossil-Wiki only
2414 ** allows safe-html by design - unsafe-HTML is never and has never been
2415 ** allowed in Fossil-Wiki.
2416 **
2417 ** This code is in the wikiformat.c file so that it can have access to the
2418 ** white-list of acceptable HTML in the aMarkup[] array.
2419 */
2420
2421 /*
2422 ** An instance of this object keeps track of the nesting of HTML
2423 ** elements for safe_html_append().
2424 */
2425 typedef struct HtmlTagStack HtmlTagStack;
2426 struct HtmlTagStack {
2427 int n; /* Current tag stack depth */
2428 int nAlloc; /* Space allocated for aStack[] */
2429 int *aStack; /* The stack of tags */
2430 int aSpace[10]; /* Initial static space, to avoid malloc() */
2431 };
 
2432
2433 /*
2434 ** Initialize bulk memory to a valid empty tagstack.
2435 */
2436 static void html_tagstack_init(HtmlTagStack *p){
2437 p->n = 0;
2438 p->nAlloc = 0;
2439 p->aStack = p->aSpace;
2440 }
2441
2442 /*
2443 ** Push a new element onto the tag statk
2444 */
2445 static void html_tagstack_push(HtmlTagStack *p, int e){
2446 if( p->n>=ArraySize(p->aSpace) && p->n>=p->nAlloc ){
2447 if( p->nAlloc==0 ){
2448 int *aNew;
2449 p->nAlloc = 50;
2450 aNew = fossil_malloc( sizeof(p->aStack[0])*p->nAlloc );
@@ -2444,11 +2459,11 @@
2459 }
2460
2461 /*
2462 ** Clear a tag stack, reclaiming any memory allocations.
2463 */
2464 static void html_tagstack_clear(HtmlTagStack *p){
2465 if( p->nAlloc ){
2466 fossil_free(p->aStack);
2467 p->nAlloc = 0;
2468 p->aStack = p->aSpace;
2469 }
@@ -2463,11 +2478,11 @@
2478 ** end-tags as you go.
2479 **
2480 ** If there is no open-tag for eEnd on the stack, then this
2481 ** routine is a no-op.
2482 */
2483 static void html_tagstack_pop(HtmlTagStack *p, Blob *pBlob, int eEnd){
2484 int i, e;
2485 if( eEnd!=0 ){
2486 for(i=p->n-1; i>=0 && p->aStack[i]!=eEnd; i--){}
2487 if( i<0 ){
2488 blob_appendf(pBlob, "<span class='error'>&lt;/%s&gt;</span>",
@@ -2491,22 +2506,26 @@
2506 **
2507 ** 1. Omit any elements that are not on the AllowedMarkup list.
2508 **
2509 ** 2. Omit any attributes that are not on the AllowedMarkup list.
2510 **
2511 ** 3. Omit any surplus close-tags. (This prevents a surplus </div>
2512 ** or </body> or similar element from interferring with formatting
2513 ** of the outer context in which the HTML is being inserted.)
2514 **
2515 ** 4. Insert additional close-tags as necessary so that any
2516 ** tag in the input that needs a close-tag has one. (This prevents
2517 ** the inserted HTML from messing up the formatting of subsequent
2518 ** sections of the document into which it is being inserted.)
2519 **
2520 ** The input must be writable. Temporary changes may be made to the
2521 ** input, but the input is restored to its original state prior to
2522 ** returning. If zHtml[nHtml] is not a zero character, then a zero
2523 ** might be written in that position temporarily, but that slot will
2524 ** also be restored before this routine returns.
2525 */
2526 void safe_html_append(Blob *pBlob, char *zHtml, int nHtml){
2527 char cLast;
2528 int i, j, n;
2529 HtmlTagStack s;
2530 ParsedMarkup markup;
2531
@@ -2560,11 +2579,11 @@
2579 ** COMMAND: test-safe-html
2580 **
2581 ** Usage: %fossil test-safe-html FILE ...
2582 **
2583 ** Read files named on the command-line. Send the text of each file
2584 ** through safe_html_append() and then write the result on
2585 ** standard output.
2586 */
2587 void test_safe_html_cmd(void){
2588 int i;
2589 Blob x;
@@ -2573,14 +2592,14 @@
2592 char *z;
2593 int n;
2594 blob_read_from_file(&x, g.argv[i], ExtFILE);
2595 blob_init(&y, 0, 0);
2596 blob_terminate(&x);
2597 safe_html_append(&y, blob_buffer(&x), blob_size(&x));
2598 blob_reset(&x);
2599 z = blob_str(&y);
2600 n = blob_size(&y);
2601 while( n>0 && (z[n-1]=='\n' || z[n-1]=='\r') ) n--;
2602 fossil_print("%.*s\n", n, z);
2603 blob_reset(&y);
2604 }
2605 }
2606

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button