| | @@ -25,10 +25,53 @@ |
| 25 | 25 | */ |
| 26 | 26 | #include <assert.h> |
| 27 | 27 | #include "config.h" |
| 28 | 28 | #include "wiki.h" |
| 29 | 29 | |
| 30 | +/* |
| 31 | +** Return true if the input string is a well-formed wiki page name. |
| 32 | +** |
| 33 | +** Well-formed wiki page names do not begin or end with whitespace, |
| 34 | +** and do not contain tabs or other control characters and do not |
| 35 | +** contain more than a single space character in a row. Well-formed |
| 36 | +** names must be between 3 and 100 chracters in length, inclusive. |
| 37 | +*/ |
| 38 | +int wiki_name_is_wellformed(const char *z){ |
| 39 | + int i; |
| 40 | + if( z[0]<=0x20 ){ |
| 41 | + return 0; |
| 42 | + } |
| 43 | + for(i=1; z[i]; i++){ |
| 44 | + if( z[i]<0x20 ) return 0; |
| 45 | + if( z[i]==0x20 && z[i-1]==0x20 ) return 0; |
| 46 | + } |
| 47 | + if( z[i-1]==' ' ) return 0; |
| 48 | + if( i<3 || i>100 ) return 0; |
| 49 | + return 1; |
| 50 | +} |
| 51 | + |
| 52 | +/* |
| 53 | +** Check a wiki name. If it is not well-formed, then issue an error |
| 54 | +** and return true. If it is well-formed, return false. |
| 55 | +*/ |
| 56 | +static int check_name(const char *z){ |
| 57 | + if( !wiki_name_is_wellformed(z) ){ |
| 58 | + style_header("Wiki Page Name Error"); |
| 59 | + @ The wiki name "<b>%h(z)</b>" is not well-formed. Rules for |
| 60 | + @ wiki page names: |
| 61 | + @ <ul> |
| 62 | + @ <li> Must not begin or end with a space. |
| 63 | + @ <li> Must not contain any control characters, including tab or |
| 64 | + @ newline. |
| 65 | + @ <li> Must not have two or more spaces in a row internally. |
| 66 | + @ <li> Must be between 3 and 100 characters in length. |
| 67 | + @ </ul> |
| 68 | + style_footer(); |
| 69 | + return 1; |
| 70 | + } |
| 71 | + return 0; |
| 72 | +} |
| 30 | 73 | |
| 31 | 74 | /* |
| 32 | 75 | ** WEBPAGE: wiki |
| 33 | 76 | ** URL: /wiki/PAGENAME |
| 34 | 77 | */ |
| | @@ -43,10 +86,11 @@ |
| 43 | 86 | |
| 44 | 87 | login_check_credentials(); |
| 45 | 88 | if( !g.okRdWiki ){ login_needed(); return; } |
| 46 | 89 | zPageName = mprintf("%s", g.zExtra); |
| 47 | 90 | dehttpize(zPageName); |
| 91 | + if( check_name(zPageName) ) return; |
| 48 | 92 | zTag = mprintf("wiki-%s", zPageName); |
| 49 | 93 | rid = db_int(0, |
| 50 | 94 | "SELECT rid FROM tagxref" |
| 51 | 95 | " WHERE tagid=(SELECT tagid FROM tag WHERE tagname=%Q)" |
| 52 | 96 | " ORDER BY mtime DESC", zTag |
| | @@ -66,11 +110,11 @@ |
| 66 | 110 | style_header(zHtmlPageName); |
| 67 | 111 | blob_init(&wiki, zBody, -1); |
| 68 | 112 | wiki_convert(&wiki, 0); |
| 69 | 113 | blob_reset(&wiki); |
| 70 | 114 | manifest_clear(&m); |
| 71 | | - if( zPageName[0] && ((rid && g.okWrWiki) || (!rid && g.okNewWiki)) ){ |
| 115 | + if( (rid && g.okWrWiki) || (!rid && g.okNewWiki) ){ |
| 72 | 116 | @ <hr> |
| 73 | 117 | @ [<a href="%s(g.zBaseURL)/wikiedit/%s(g.zExtra)">Edit</a>] |
| 74 | 118 | } |
| 75 | 119 | style_footer(); |
| 76 | 120 | } |
| | @@ -94,10 +138,11 @@ |
| 94 | 138 | zBody = mprintf("%s", zBody); |
| 95 | 139 | } |
| 96 | 140 | login_check_credentials(); |
| 97 | 141 | zPageName = mprintf("%s", g.zExtra); |
| 98 | 142 | dehttpize(zPageName); |
| 143 | + if( check_name(zPageName) ) return; |
| 99 | 144 | zTag = mprintf("wiki-%s", zPageName); |
| 100 | 145 | rid = db_int(0, |
| 101 | 146 | "SELECT rid FROM tagxref" |
| 102 | 147 | " WHERE tagid=(SELECT tagid FROM tag WHERE tagname=%Q)" |
| 103 | 148 | " ORDER BY mtime DESC", zTag |
| | @@ -146,11 +191,11 @@ |
| 146 | 191 | blob_reset(&wiki); |
| 147 | 192 | content_deltify(rid, nrid, 0); |
| 148 | 193 | db_end_transaction(0); |
| 149 | 194 | cgi_redirect(mprintf("wiki/%s", g.zExtra)); |
| 150 | 195 | } |
| 151 | | - if( P("cancel")!=0 || zPageName[0]==0 ){ |
| 196 | + if( P("cancel")!=0 ){ |
| 152 | 197 | cgi_redirect(mprintf("wiki/%s", g.zExtra)); |
| 153 | 198 | return; |
| 154 | 199 | } |
| 155 | 200 | if( zBody==0 ){ |
| 156 | 201 | zBody = mprintf("<i>Empty Page</i>"); |
| 157 | 202 | |