| | @@ -2508,37 +2508,20 @@ |
| 2508 | 2508 | } |
| 2509 | 2509 | }while( e!=eEnd && p->n>0 ); |
| 2510 | 2510 | } |
| 2511 | 2511 | |
| 2512 | 2512 | /* |
| 2513 | | -** Append HTML text to a Blob object. |
| 2514 | | -** |
| 2515 | | -** If safe-html is enabled then the appended text is modified |
| 2516 | | -** changed in the following ways: |
| 2517 | | -** |
| 2518 | | -** 1. Omit any elements that are not on the AllowedMarkup list. |
| 2519 | | -** |
| 2520 | | -** 2. Omit any attributes that are not on the AllowedMarkup list. |
| 2521 | | -** |
| 2522 | | -** 3. Omit any surplus close-tags. |
| 2523 | | -** |
| 2524 | | -** 4. Insert additional close-tags as necessary so that any |
| 2525 | | -** tag in the input that needs a close-tag has one. |
| 2526 | | -** |
| 2527 | | -** This modifications are intended to make the generated HTML safe |
| 2528 | | -** to be embedded in a larger HTML document, such that the embedded |
| 2529 | | -** HTML has no influence on the formatting and operation of the |
| 2530 | | -** larger document. |
| 2531 | | -** |
| 2532 | | -** When safe-html is eanbled, the input to this routine must be writable. |
| 2513 | +** Append a safe translation of HTML text to a Blob object. |
| 2514 | +** |
| 2515 | +** Restriction: The input to this routine must be writable. |
| 2533 | 2516 | * Temporary changes may be made to the input, but the input is restored |
| 2534 | 2517 | ** to its original state prior to returning. If zHtml[nHtml] is not a |
| 2535 | 2518 | ** zero character, then a zero might be written in that position |
| 2536 | 2519 | ** temporarily, but that slot will also be restored before this routine |
| 2537 | 2520 | ** returns. |
| 2538 | 2521 | */ |
| 2539 | | -void safe_html_append(Blob *pBlob, char *zHtml, int nHtml){ |
| 2522 | +static void safe_html_append(Blob *pBlob, char *zHtml, int nHtml){ |
| 2540 | 2523 | char cLast; |
| 2541 | 2524 | int i, j, n; |
| 2542 | 2525 | HtmlTagStack s; |
| 2543 | 2526 | ParsedMarkup markup; |
| 2544 | 2527 | |
| | @@ -2588,21 +2571,45 @@ |
| 2588 | 2571 | html_tagstack_clear(&s); |
| 2589 | 2572 | zHtml[nHtml] = cLast; |
| 2590 | 2573 | } |
| 2591 | 2574 | |
| 2592 | 2575 | /* |
| 2593 | | -** The input blob consists of HTML. Convert it into "safe HTML". Safe |
| 2594 | | -** HTML has no potentially disruptive elements (ex: <script>, <style>) |
| 2595 | | -** and it is embeddable, meaning that it won't close any outer elements |
| 2596 | | -** from the script in which it is embedded, nor will it leave any open |
| 2597 | | -** elements to affect the tail of the outer script. |
| 2576 | +** The input blob contains HTML. If safe-html is enabled, then |
| 2577 | +** convert the input into "safe HTML". The following modifications |
| 2578 | +** are made: |
| 2579 | +** |
| 2580 | +** 1. Remove any elements that are not on the AllowedMarkup list. |
| 2581 | +** (ex: <script>, <form>, etc.) |
| 2582 | +** |
| 2583 | +** 2. Remove any attributes that are not on the AllowedMarkup list. |
| 2584 | +** (ex: onload=, id=, etc.) |
| 2585 | +** |
| 2586 | +** 3. Omit any surplus close-tags. This prevents the script from |
| 2587 | +** terminating an <div> or similar in the outer context. |
| 2588 | +** |
| 2589 | +** 4. Insert additional close-tags as necessary so that any |
| 2590 | +** tag in the input that needs a close-tag has one. This |
| 2591 | +** prevents tags in the embedded script from affecting the |
| 2592 | +** display of content that follows this script in the enclosing |
| 2593 | +** context. |
| 2594 | +** |
| 2595 | +** This modifications are intended to make the generated HTML safe |
| 2596 | +** to be embedded in a larger HTML document, such that the embedded |
| 2597 | +** HTML has no influence on the formatting and operation of the |
| 2598 | +** larger document. |
| 2599 | +** |
| 2600 | +** If safe-html is disabled, then this routine is a no-op. |
| 2598 | 2601 | */ |
| 2599 | 2602 | void safe_html(Blob *in){ |
| 2600 | | - Blob out; |
| 2601 | | - char *z = blob_str(in); |
| 2602 | | - int n = blob_size(in); |
| 2603 | + Blob out; /* Holding area for the revised text during construction */ |
| 2604 | + char *z; /* Original input text */ |
| 2605 | + int n; /* Number of bytes in the original input text */ |
| 2603 | 2606 | int k; |
| 2607 | + |
| 2608 | + /* if( safeHtml==0 ) return; TBD: Always used at this time */ |
| 2609 | + z = blob_str(in); |
| 2610 | + n = blob_size(in); |
| 2604 | 2611 | blob_init(&out, 0, 0); |
| 2605 | 2612 | while( fossil_isspace(z[0]) ){ z++; n--; } |
| 2606 | 2613 | for(k=n-1; k>5 && fossil_isspace(z[k]); k--){} |
| 2607 | 2614 | |
| 2608 | 2615 | if( fossil_strnicmp(z, "<div",4)==0 && !fossil_isalpha(z[4]) |
| 2609 | 2616 | |