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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define apr_isalnum(c) (isalnum(((unsigned char)(c))))
20 #define apr_isalpha(c) (isalpha(((unsigned char)(c))))
21 #define apr_iscntrl(c) (iscntrl(((unsigned char)(c))))
22 #define apr_isprint(c) (isprint(((unsigned char)(c))))
24 #define APR_HAVE_STDIO_H 1
25 #define APR_HAVE_STRING_H 1
34 #if defined(WIN32) || defined(OS2)
35 #define NEED_ENHANCED_ESCAPES
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.
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)
57 int main(int argc, char *argv[])
62 printf("/* this file is automatically generated by gen_test_char, "
64 "#define T_ESCAPE_SHELL_CMD (%u)\n"
65 "#define T_ESCAPE_PATH_SEGMENT (%u)\n"
66 "#define T_OS_ESCAPE_PATH (%u)\n"
67 "#define T_HTTP_TOKEN_STOP (%u)\n"
68 "#define T_ESCAPE_LOGITEM (%u)\n"
69 "#define T_ESCAPE_FORENSIC (%u)\n"
70 "#define T_ESCAPE_URLENCODED (%u)\n"
71 "#define T_HTTP_CTRLS (%u)\n"
73 "static const unsigned char test_char_table[256] = {",
75 T_ESCAPE_PATH_SEGMENT,
83 for (c = 0; c < 256; ++c) {
88 /* escape_shell_cmd */
89 #ifdef NEED_ENHANCED_ESCAPES
90 /* Win32/OS2 have many of the same vulnerable characters
91 * as Unix sh, plus the carriage return and percent char.
92 * The proper escaping of these characters varies from unix
93 * since Win32/OS2 use carets or doubled-double quotes,
94 * and neither lf nor cr can be escaped. We escape unix
95 * specific as well, to assure that cross-compiled unix
96 * applications behave similiarly when invoked on win32/os2.
98 * Rem please keep in-sync with apr's list in win32/filesys.c
100 if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n\r%", c)) {
101 flags |= T_ESCAPE_SHELL_CMD;
104 if (c && strchr("&;`'\"|*?~<>^()[]{}$\\\n", c)) {
105 flags |= T_ESCAPE_SHELL_CMD;
109 if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) {
110 flags |= T_ESCAPE_PATH_SEGMENT;
113 if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=/~", c)) {
114 flags |= T_OS_ESCAPE_PATH;
117 if (!apr_isalnum(c) && !strchr(".-*_ ", c)) {
118 flags |= T_ESCAPE_URLENCODED;
121 /* Stop for any non-'token' character, including ctrls, obs-text,
122 * and "tspecials" (RFC2068) a.k.a. "separators" (RFC2616)
123 * XXX: We need to build a specific table for EBCDIC values with
124 * ASCII equivilants here
126 if (!c || apr_iscntrl(c) || strchr(" \t()<>@,;:\\\"/[]?={}", c)) {
127 flags |= T_HTTP_TOKEN_STOP;
130 /* Catch CTRLs other than VCHAR, HT and SP, and obs-text (RFC7230 3.2)
131 * This includes only the C0 plane, not C1 (which is obs-text itself.)
132 * XXX: Need to constrain iscntrl to C0 equivilants in ASCII,
133 * even on EBCDIC architecture
135 if (!c || (apr_iscntrl(c) && c != '\t')) {
136 flags |= T_HTTP_CTRLS;
139 /* For logging, escape all control characters,
140 * double quotes (because they delimit the request in the log file)
141 * backslashes (because we use backslash for escaping)
142 * and 8-bit chars with the high bit set
144 if (c && (!apr_isprint(c) || c == '"' || c == '\\' || apr_iscntrl(c))) {
145 flags |= T_ESCAPE_LOGITEM;
148 /* For forensic logging, escape all control characters, top bit set,
149 * :, | (used as delimiters) and % (used for escaping).
151 if (!apr_isprint(c) || c == ':' || c == '|' || c == '%'
152 || apr_iscntrl(c) || !c) {
153 flags |= T_ESCAPE_FORENSIC;
156 printf("0x%02x%c", flags, (c < 255) ? ',' : ' ');