]> gitweb.ps.run Git - ps-cgit/blob - html.c
ui-shared: prevent malicious filename from injecting headers
[ps-cgit] / html.c
1 /* html.c: helper functions for html output
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "html.h"
11
12 /* Percent-encoding of each character, except: a-zA-Z0-9!$()*,./:;@- */
13 static const char* url_escape_table[256] = {
14         "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
15         "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
16         "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
17         "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
18         "%20", NULL,  "%22", "%23", NULL,  "%25", "%26", "%27",
19         NULL,  NULL,  NULL,  "%2b", NULL,  NULL,  NULL,  NULL,
20         NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
21         NULL,  NULL,  NULL,  NULL,  "%3c", "%3d", "%3e", "%3f",
22         NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
23         NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
24         NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
25         NULL,  NULL,  NULL,  NULL,  "%5c", NULL,  "%5e", NULL,
26         "%60", NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
27         NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
28         NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,  NULL,
29         NULL,  NULL,  NULL,  "%7b", "%7c", "%7d", NULL,  "%7f",
30         "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
31         "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
32         "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
33         "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
34         "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
35         "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
36         "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
37         "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
38         "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
39         "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
40         "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
41         "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
42         "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
43         "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
44         "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
45         "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
46 };
47
48 char *fmt(const char *format, ...)
49 {
50         static char buf[8][1024];
51         static int bufidx;
52         int len;
53         va_list args;
54
55         bufidx++;
56         bufidx &= 7;
57
58         va_start(args, format);
59         len = vsnprintf(buf[bufidx], sizeof(buf[bufidx]), format, args);
60         va_end(args);
61         if (len > sizeof(buf[bufidx])) {
62                 fprintf(stderr, "[html.c] string truncated: %s\n", format);
63                 exit(1);
64         }
65         return buf[bufidx];
66 }
67
68 char *fmtalloc(const char *format, ...)
69 {
70         struct strbuf sb = STRBUF_INIT;
71         va_list args;
72
73         va_start(args, format);
74         strbuf_vaddf(&sb, format, args);
75         va_end(args);
76
77         return strbuf_detach(&sb, NULL);
78 }
79
80 void html_raw(const char *data, size_t size)
81 {
82         if (write(STDOUT_FILENO, data, size) != size)
83                 die_errno("write error on html output");
84 }
85
86 void html(const char *txt)
87 {
88         html_raw(txt, strlen(txt));
89 }
90
91 void htmlf(const char *format, ...)
92 {
93         va_list args;
94         struct strbuf buf = STRBUF_INIT;
95
96         va_start(args, format);
97         strbuf_vaddf(&buf, format, args);
98         va_end(args);
99         html(buf.buf);
100         strbuf_release(&buf);
101 }
102
103 void html_txtf(const char *format, ...)
104 {
105         va_list args;
106
107         va_start(args, format);
108         html_vtxtf(format, args);
109         va_end(args);
110 }
111
112 void html_vtxtf(const char *format, va_list ap)
113 {
114         va_list cp;
115         struct strbuf buf = STRBUF_INIT;
116
117         va_copy(cp, ap);
118         strbuf_vaddf(&buf, format, cp);
119         va_end(cp);
120         html_txt(buf.buf);
121         strbuf_release(&buf);
122 }
123
124 void html_txt(const char *txt)
125 {
126         const char *t = txt;
127         while (t && *t) {
128                 int c = *t;
129                 if (c == '<' || c == '>' || c == '&') {
130                         html_raw(txt, t - txt);
131                         if (c == '>')
132                                 html("&gt;");
133                         else if (c == '<')
134                                 html("&lt;");
135                         else if (c == '&')
136                                 html("&amp;");
137                         txt = t + 1;
138                 }
139                 t++;
140         }
141         if (t != txt)
142                 html(txt);
143 }
144
145 void html_ntxt(int len, const char *txt)
146 {
147         const char *t = txt;
148         while (t && *t && len--) {
149                 int c = *t;
150                 if (c == '<' || c == '>' || c == '&') {
151                         html_raw(txt, t - txt);
152                         if (c == '>')
153                                 html("&gt;");
154                         else if (c == '<')
155                                 html("&lt;");
156                         else if (c == '&')
157                                 html("&amp;");
158                         txt = t + 1;
159                 }
160                 t++;
161         }
162         if (t != txt)
163                 html_raw(txt, t - txt);
164         if (len < 0)
165                 html("...");
166 }
167
168 void html_attrf(const char *fmt, ...)
169 {
170         va_list ap;
171         struct strbuf sb = STRBUF_INIT;
172
173         va_start(ap, fmt);
174         strbuf_vaddf(&sb, fmt, ap);
175         va_end(ap);
176
177         html_attr(sb.buf);
178         strbuf_release(&sb);
179 }
180
181 void html_attr(const char *txt)
182 {
183         const char *t = txt;
184         while (t && *t) {
185                 int c = *t;
186                 if (c == '<' || c == '>' || c == '\'' || c == '\"' || c == '&') {
187                         html_raw(txt, t - txt);
188                         if (c == '>')
189                                 html("&gt;");
190                         else if (c == '<')
191                                 html("&lt;");
192                         else if (c == '\'')
193                                 html("&#x27;");
194                         else if (c == '"')
195                                 html("&quot;");
196                         else if (c == '&')
197                                 html("&amp;");
198                         txt = t + 1;
199                 }
200                 t++;
201         }
202         if (t != txt)
203                 html(txt);
204 }
205
206 void html_url_path(const char *txt)
207 {
208         const char *t = txt;
209         while (t && *t) {
210                 unsigned char c = *t;
211                 const char *e = url_escape_table[c];
212                 if (e && c != '+' && c != '&') {
213                         html_raw(txt, t - txt);
214                         html(e);
215                         txt = t + 1;
216                 }
217                 t++;
218         }
219         if (t != txt)
220                 html(txt);
221 }
222
223 void html_url_arg(const char *txt)
224 {
225         const char *t = txt;
226         while (t && *t) {
227                 unsigned char c = *t;
228                 const char *e = url_escape_table[c];
229                 if (c == ' ')
230                         e = "+";
231                 if (e) {
232                         html_raw(txt, t - txt);
233                         html(e);
234                         txt = t + 1;
235                 }
236                 t++;
237         }
238         if (t != txt)
239                 html(txt);
240 }
241
242 void html_header_arg_in_quotes(const char *txt)
243 {
244         const char *t = txt;
245         while (t && *t) {
246                 unsigned char c = *t;
247                 const char *e = NULL;
248                 if (c == '\\')
249                         e = "\\\\";
250                 else if (c == '\r')
251                         e = "\\r";
252                 else if (c == '\n')
253                         e = "\\n";
254                 else if (c == '"')
255                         e = "\\\"";
256                 if (e) {
257                         html_raw(txt, t - txt);
258                         html(e);
259                         txt = t + 1;
260                 }
261                 t++;
262         }
263         if (t != txt)
264                 html(txt);
265
266 }
267
268 void html_hidden(const char *name, const char *value)
269 {
270         html("<input type='hidden' name='");
271         html_attr(name);
272         html("' value='");
273         html_attr(value);
274         html("'/>");
275 }
276
277 void html_option(const char *value, const char *text, const char *selected_value)
278 {
279         html("<option value='");
280         html_attr(value);
281         html("'");
282         if (selected_value && !strcmp(selected_value, value))
283                 html(" selected='selected'");
284         html(">");
285         html_txt(text);
286         html("</option>\n");
287 }
288
289 void html_intoption(int value, const char *text, int selected_value)
290 {
291         htmlf("<option value='%d'%s>", value,
292               value == selected_value ? " selected='selected'" : "");
293         html_txt(text);
294         html("</option>");
295 }
296
297 void html_link_open(const char *url, const char *title, const char *class)
298 {
299         html("<a href='");
300         html_attr(url);
301         if (title) {
302                 html("' title='");
303                 html_attr(title);
304         }
305         if (class) {
306                 html("' class='");
307                 html_attr(class);
308         }
309         html("'>");
310 }
311
312 void html_link_close(void)
313 {
314         html("</a>");
315 }
316
317 void html_fileperm(unsigned short mode)
318 {
319         htmlf("%c%c%c", (mode & 4 ? 'r' : '-'),
320               (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
321 }
322
323 int html_include(const char *filename)
324 {
325         FILE *f;
326         char buf[4096];
327         size_t len;
328
329         if (!(f = fopen(filename, "r"))) {
330                 fprintf(stderr, "[cgit] Failed to include file %s: %s (%d).\n",
331                         filename, strerror(errno), errno);
332                 return -1;
333         }
334         while ((len = fread(buf, 1, 4096, f)) > 0)
335                 html_raw(buf, len);
336         fclose(f);
337         return 0;
338 }
339
340 static int hextoint(char c)
341 {
342         if (c >= 'a' && c <= 'f')
343                 return 10 + c - 'a';
344         else if (c >= 'A' && c <= 'F')
345                 return 10 + c - 'A';
346         else if (c >= '0' && c <= '9')
347                 return c - '0';
348         else
349                 return -1;
350 }
351
352 static char *convert_query_hexchar(char *txt)
353 {
354         int d1, d2, n;
355         n = strlen(txt);
356         if (n < 3) {
357                 *txt = '\0';
358                 return txt-1;
359         }
360         d1 = hextoint(*(txt + 1));
361         d2 = hextoint(*(txt + 2));
362         if (d1 < 0 || d2 < 0) {
363                 memmove(txt, txt + 3, n - 2);
364                 return txt-1;
365         } else {
366                 *txt = d1 * 16 + d2;
367                 memmove(txt + 1, txt + 3, n - 2);
368                 return txt;
369         }
370 }
371
372 int http_parse_querystring(const char *txt_, void (*fn)(const char *name, const char *value))
373 {
374         char *o, *t, *txt, *value = NULL, c;
375
376         if (!txt_)
377                 return 0;
378
379         o = t = txt = xstrdup(txt_);
380         while ((c=*t) != '\0') {
381                 if (c == '=') {
382                         *t = '\0';
383                         value = t + 1;
384                 } else if (c == '+') {
385                         *t = ' ';
386                 } else if (c == '%') {
387                         t = convert_query_hexchar(t);
388                 } else if (c == '&') {
389                         *t = '\0';
390                         (*fn)(txt, value);
391                         txt = t + 1;
392                         value = NULL;
393                 }
394                 t++;
395         }
396         if (t != txt)
397                 (*fn)(txt, value);
398         free(o);
399         return 0;
400 }