Apache HTTPD
gen_test_char.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifdef CROSS_COMPILE
18
19#include <ctype.h>
20#define apr_isalnum(c) (isalnum(((unsigned char)(c))))
21#define apr_isalpha(c) (isalpha(((unsigned char)(c))))
22#define apr_iscntrl(c) (iscntrl(((unsigned char)(c))))
23#define apr_isprint(c) (isprint(((unsigned char)(c))))
24#define APR_HAVE_STDIO_H 1
25#define APR_HAVE_STRING_H 1
26
27#else
28
29#include "apr.h"
30#include "apr_lib.h"
31
32#endif
33
34#if defined(WIN32) || defined(OS2)
35#define NEED_ENHANCED_ESCAPES
36#endif
37
38#if APR_HAVE_STDIO_H
39#include <stdio.h>
40#endif
41#if APR_HAVE_STRING_H
42#include <string.h>
43#endif
44
45/* A bunch of functions in util.c scan strings looking for certain characters.
46 * To make that more efficient we encode a lookup table.
47 */
48#define T_ESCAPE_SHELL_CMD (0x01)
49#define T_ESCAPE_PATH_SEGMENT (0x02)
50#define T_OS_ESCAPE_PATH (0x04)
51#define T_HTTP_TOKEN_STOP (0x08)
52#define T_ESCAPE_LOGITEM (0x10)
53#define T_ESCAPE_FORENSIC (0x20)
54#define T_ESCAPE_URLENCODED (0x40)
55#define T_HTTP_CTRLS (0x80)
56#define T_VCHAR_OBSTEXT (0x100)
57#define T_URI_UNRESERVED (0x200)
58
59int main(int argc, char *argv[])
60{
61 unsigned c;
62 unsigned short flags;
63
64 printf("/* this file is automatically generated by gen_test_char, "
65 "do not edit */\n"
66 "#define T_ESCAPE_SHELL_CMD (%u)\n"
67 "#define T_ESCAPE_PATH_SEGMENT (%u)\n"
68 "#define T_OS_ESCAPE_PATH (%u)\n"
69 "#define T_HTTP_TOKEN_STOP (%u)\n"
70 "#define T_ESCAPE_LOGITEM (%u)\n"
71 "#define T_ESCAPE_FORENSIC (%u)\n"
72 "#define T_ESCAPE_URLENCODED (%u)\n"
73 "#define T_HTTP_CTRLS (%u)\n"
74 "#define T_VCHAR_OBSTEXT (%u)\n"
75 "#define T_URI_UNRESERVED (%u)\n"
76 "\n"
77 "static const unsigned short test_char_table[256] = {",
88 );
89
90 for (c = 0; c < 256; ++c) {
91 flags = 0;
92 if (c % 8 == 0)
93 printf("\n ");
94
95 /* escape_shell_cmd */
96#ifdef NEED_ENHANCED_ESCAPES
97 /* Win32/OS2 have many of the same vulnerable characters
98 * as Unix sh, plus the carriage return and percent char.
99 * The proper escaping of these characters varies from unix
100 * since Win32/OS2 use carets or doubled-double quotes,
101 * and neither lf nor cr can be escaped. We escape unix
102 * specific as well, to assure that cross-compiled unix
103 * applications behave similarly when invoked on win32/os2.
104 *
105 * Rem please keep in-sync with apr's list in win32/filesys.c
106 */
107 if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n\r%", c)) {
109 }
110#else
111 if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n", c)) {
113 }
114#endif
115
116 if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) {
118 }
119
120 if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:;@&=/~", c)) {
122 }
123
124 if (!apr_isalnum(c) && !strchr(".-*_ ", c)) {
126 }
127
128 /* Stop for any non-'token' character, including ctrls, obs-text,
129 * and "tspecials" (RFC2068) a.k.a. "separators" (RFC2616), which
130 * is easier to express as characters remaining in the ASCII token set
131 */
132 if (!c || !(apr_isalnum(c) || strchr("!#$%&'*+-.^_`|~", c))) {
134 }
135
136 /* Catch CTRLs other than VCHAR, HT and SP, and obs-text (RFC7230 3.2)
137 * This includes only the C0 plane, not C1 (which is obs-text itself.)
138 * XXX: We should verify that all ASCII C0 ctrls/DEL corresponding to
139 * the current EBCDIC translation are captured, and ASCII C1 ctrls
140 * corresponding are all permitted (as they fall under obs-text rule)
141 */
142 if (!c || (apr_iscntrl(c) && c != '\t')) {
144 }
145
146 /* From RFC3986, the specific sets of gen-delims, sub-delims (2.2),
147 * and unreserved (2.3) that are possible somewhere within a URI.
148 * Spec requires all others to be %XX encoded, including obs-text.
149 */
150 if (c && !apr_iscntrl(c) && c != ' ') {
152 }
153
154 /* For logging, escape all control characters,
155 * double quotes (because they delimit the request in the log file)
156 * backslashes (because we use backslash for escaping)
157 * and 8-bit chars with the high bit set
158 */
159 if (c && (!apr_isprint(c) || c == '"' || c == '\\' || apr_iscntrl(c))) {
161 }
162
163 /* For forensic logging, escape all control characters, top bit set,
164 * :, | (used as delimiters) and % (used for escaping).
165 */
166 if (!apr_isprint(c) || c == ':' || c == '|' || c == '%'
167 || apr_iscntrl(c) || !c) {
169 }
170
171 /* Characters in the RFC 3986 "unreserved" set.
172 * https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 */
173 if (c && (apr_isalnum(c) || strchr("-._~", c))) {
175 }
176
177 printf("0x%03x%c", flags, (c < 255) ? ',' : ' ');
178 }
179
180 printf("\n};\n\n");
181
182 printf(
183 "/* we assume the folks using this ensure 0 <= c < 256... which means\n"
184 " * you need a cast to (unsigned char) first, you can't just plug a\n"
185 " * char in here and get it to work, because if char is signed then it\n"
186 " * will first be sign extended.\n"
187 " */\n"
188 "#define TEST_CHAR(c, f) (test_char_table[(unsigned char)(c)] & (f))\n"
189 );
190
191 return 0;
192}
APR general purpose library routines.
const char apr_ssize_t int flags
Definition apr_encode.h:168
apr_size_t size
#define apr_isprint(c)
Definition apr_lib.h:221
#define apr_isalnum(c)
Definition apr_lib.h:203
#define apr_iscntrl(c)
Definition apr_lib.h:207
apr_pool_t int argc
Definition apr_getopt.h:104
apr_vformatter_buff_t * c
Definition apr_lib.h:175
const char * argv[3]
int main(void)
Definition occhild.c:9
#define T_HTTP_TOKEN_STOP
#define T_VCHAR_OBSTEXT
#define T_ESCAPE_LOGITEM
#define T_OS_ESCAPE_PATH
#define T_HTTP_CTRLS
#define T_ESCAPE_URLENCODED
#define T_ESCAPE_FORENSIC
#define T_URI_UNRESERVED
#define T_ESCAPE_PATH_SEGMENT
#define T_ESCAPE_SHELL_CMD