Coverage Report

Created: 2025-08-24 06:42

/src/httpd/server/apreq_parser_urlencoded.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
**  Licensed to the Apache Software Foundation (ASF) under one or more
3
** contributor license agreements.  See the NOTICE file distributed with
4
** this work for additional information regarding copyright ownership.
5
** The ASF licenses this file to You under the Apache License, Version 2.0
6
** (the "License"); you may not use this file except in compliance with
7
** the License.  You may obtain a copy of the License at
8
**
9
**      http://www.apache.org/licenses/LICENSE-2.0
10
**
11
**  Unless required by applicable law or agreed to in writing, software
12
**  distributed under the License is distributed on an "AS IS" BASIS,
13
**  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
**  See the License for the specific language governing permissions and
15
**  limitations under the License.
16
*/
17
18
#include "apreq_parser.h"
19
#include "apreq_util.h"
20
#include "apreq_error.h"
21
22
23
149
#define PARSER_STATUS_CHECK(PREFIX)   do {         \
24
149
    if (ctx->status == PREFIX##_ERROR)             \
25
149
        return APREQ_ERROR_GENERAL;                \
26
149
    else if (ctx->status == PREFIX##_COMPLETE)     \
27
149
        return APR_SUCCESS;                        \
28
149
    else if (bb == NULL)                           \
29
149
        return APR_INCOMPLETE;                     \
30
149
} while (0);
31
32
33
34
struct url_ctx {
35
    apr_bucket_brigade *bb;
36
    apr_size_t          nlen;
37
    apr_size_t          vlen;
38
    enum {
39
        URL_NAME,
40
        URL_VALUE,
41
        URL_COMPLETE,
42
        URL_ERROR
43
    }                   status;
44
};
45
46
47
/******************** application/x-www-form-urlencoded ********************/
48
49
static apr_status_t split_urlword(apreq_param_t **p, apr_pool_t *pool,
50
                                  apr_bucket_brigade *bb,
51
                                  apr_size_t nlen,
52
                                  apr_size_t vlen)
53
515
{
54
515
    apreq_param_t *param;
55
515
    apreq_value_t *v;
56
515
    apr_bucket *e, *f;
57
515
    apr_status_t s;
58
515
    struct iovec vec[APREQ_DEFAULT_NELTS];
59
515
    apr_array_header_t arr;
60
515
    apr_size_t mark;
61
515
    apreq_charset_t charset;
62
63
515
    if (nlen == 0)
64
2
        return APR_EBADARG;
65
66
513
    param = apreq_param_make(pool, NULL, nlen, NULL, vlen);
67
513
    if (param == NULL)
68
0
        return APR_ENOMEM;
69
513
    *(const apreq_value_t **)&v = &param->v;
70
71
513
    arr.pool     = pool;
72
513
    arr.elt_size = sizeof(struct iovec);
73
513
    arr.nelts    = 0;
74
513
    arr.nalloc   = APREQ_DEFAULT_NELTS;
75
513
    arr.elts     = (char *)vec;
76
77
513
    ++nlen, ++vlen;
78
513
    e = APR_BRIGADE_FIRST(bb);
79
80
513
    while (!APR_BUCKET_IS_EOS(e)) {
81
513
        struct iovec *iov = apr_array_push(&arr);
82
513
        apr_size_t len;
83
513
        s = apr_bucket_read(e, (const char **)&iov->iov_base,
84
513
                            &len, APR_BLOCK_READ);
85
513
        if (s != APR_SUCCESS)
86
0
            return s;
87
88
513
        iov->iov_len = len;
89
513
        nlen -= len;
90
91
513
        e = APR_BUCKET_NEXT(e);
92
93
513
        if (nlen == 0) {
94
513
            iov->iov_len--;
95
513
            break;
96
513
        }
97
513
    }
98
99
513
    mark = arr.nelts;
100
101
513
    while (!APR_BUCKET_IS_EOS(e)) {
102
513
        struct iovec *iov = apr_array_push(&arr);
103
513
        apr_size_t len;
104
513
        s = apr_bucket_read(e, (const char **)&iov->iov_base,
105
513
                            &len, APR_BLOCK_READ);
106
513
        if (s != APR_SUCCESS)
107
0
            return s;
108
109
513
        iov->iov_len = len;
110
513
        vlen -= len;
111
112
513
        e = APR_BUCKET_NEXT(e);
113
114
513
        if (vlen == 0) {
115
513
            iov->iov_len--;
116
513
            break;
117
513
        }
118
119
513
    }
120
121
513
    s = apreq_decodev(v->data, &vlen,
122
513
                      (struct iovec *)arr.elts + mark, arr.nelts - mark);
123
513
    if (s != APR_SUCCESS)
124
19
        return s;
125
126
494
    charset = apreq_charset_divine(v->data, vlen);
127
128
494
    v->name = v->data + vlen + 1;
129
494
    v->dlen = vlen;
130
131
494
    s = apreq_decodev(v->name, &nlen, (struct iovec *)arr.elts, mark);
132
494
    if (s != APR_SUCCESS)
133
38
        return s;
134
135
456
    switch (apreq_charset_divine(v->name, nlen)) {
136
61
    case APREQ_CHARSET_UTF8:
137
61
        if (charset == APREQ_CHARSET_ASCII)
138
34
            charset = APREQ_CHARSET_UTF8;
139
221
    case APREQ_CHARSET_ASCII:
140
221
        break;
141
142
166
    case APREQ_CHARSET_LATIN1:
143
166
        if (charset != APREQ_CHARSET_CP1252)
144
139
            charset = APREQ_CHARSET_LATIN1;
145
166
        break;
146
69
    case APREQ_CHARSET_CP1252:
147
69
        charset = APREQ_CHARSET_CP1252;
148
456
    }
149
150
456
    v->nlen = nlen;
151
152
1.36k
    while ((f = APR_BRIGADE_FIRST(bb)) != e)
153
912
        apr_bucket_delete(f);
154
155
456
    apreq_param_tainted_on(param);
156
456
    apreq_param_charset_set(param, charset);
157
456
    *p = param;
158
456
    return APR_SUCCESS;
159
456
}
160
161
APREQ_DECLARE_PARSER(apreq_parse_urlencoded)
162
149
{
163
149
    apr_pool_t *pool = parser->pool;
164
149
    apr_bucket *e;
165
149
    struct url_ctx *ctx;
166
167
149
    if (parser->ctx == NULL) {
168
149
        ctx = apr_pcalloc(pool, sizeof *ctx);
169
149
        ctx->bb = apr_brigade_create(pool, parser->bucket_alloc);
170
149
        parser->ctx = ctx;
171
149
        ctx->status = URL_NAME;
172
149
    }
173
0
    else
174
0
        ctx = parser->ctx;
175
176
149
    PARSER_STATUS_CHECK(URL);
177
149
    e = APR_BRIGADE_LAST(ctx->bb);
178
149
    APR_BRIGADE_CONCAT(ctx->bb, bb);
179
180
605
 parse_url_brigade:
181
182
605
    for (e  = APR_BUCKET_NEXT(e);
183
695
         e != APR_BRIGADE_SENTINEL(ctx->bb);
184
605
         e  = APR_BUCKET_NEXT(e))
185
605
    {
186
605
        apreq_param_t *param;
187
605
        apr_size_t off = 0, dlen;
188
605
        const char *data;
189
605
        apr_status_t s;
190
191
605
        if (APR_BUCKET_IS_EOS(e)) {
192
0
            if (ctx->status == URL_NAME) {
193
0
                s = APR_SUCCESS;
194
0
            }
195
0
            else {
196
0
                s = split_urlword(&param, pool, ctx->bb, ctx->nlen, ctx->vlen);
197
0
                if (parser->hook != NULL && s == APR_SUCCESS)
198
0
                    s = apreq_hook_run(parser->hook, param, NULL);
199
200
0
                if (s == APR_SUCCESS) {
201
0
                    apreq_value_table_add(&param->v, t);
202
0
                    ctx->status = URL_COMPLETE;
203
0
                }
204
0
                else {
205
0
                    ctx->status = URL_ERROR;
206
0
                }
207
0
            }
208
209
0
            APR_BRIGADE_CONCAT(bb, ctx->bb);
210
0
            return s;
211
0
        }
212
213
605
        s = apr_bucket_read(e, &data, &dlen, APR_BLOCK_READ);
214
605
        if ( s != APR_SUCCESS ) {
215
0
            ctx->status = URL_ERROR;
216
0
            return s;
217
0
        }
218
219
1.14k
    parse_url_bucket:
220
221
1.14k
        switch (ctx->status) {
222
223
605
        case URL_NAME:
224
6.05k
            while (off < dlen) {
225
5.98k
                switch (data[off++]) {
226
535
                case '=':
227
535
                    apr_bucket_split(e, off);
228
535
                    dlen -= off;
229
535
                    data += off;
230
535
                    off = 0;
231
535
                    e = APR_BUCKET_NEXT(e);
232
535
                    ctx->status = URL_VALUE;
233
535
                    goto parse_url_bucket;
234
5.45k
                default:
235
5.45k
                    ++ctx->nlen;
236
5.98k
                }
237
5.98k
            }
238
70
            break;
239
240
535
        case URL_VALUE:
241
2.89k
            while (off < dlen) {
242
243
2.87k
                switch (data[off++]) {
244
162
                case '&':
245
515
                case ';':
246
515
                    apr_bucket_split(e, off);
247
515
                    s = split_urlword(&param, pool, ctx->bb,
248
515
                                      ctx->nlen, ctx->vlen);
249
515
                    if (parser->hook != NULL && s == APR_SUCCESS)
250
456
                        s = apreq_hook_run(parser->hook, param, NULL);
251
252
515
                    if (s != APR_SUCCESS) {
253
59
                        ctx->status = URL_ERROR;
254
59
                        return s;
255
59
                    }
256
257
456
                    apreq_value_table_add(&param->v, t);
258
456
                    ctx->status = URL_NAME;
259
456
                    ctx->nlen = 0;
260
456
                    ctx->vlen = 0;
261
456
                    e = APR_BRIGADE_SENTINEL(ctx->bb);
262
456
                    goto parse_url_brigade;
263
264
2.36k
                default:
265
2.36k
                    ++ctx->vlen;
266
2.87k
                }
267
2.87k
            }
268
20
            break;
269
20
        default:
270
0
            ; /* not reached */
271
1.14k
        }
272
1.14k
    }
273
90
    apreq_brigade_setaside(ctx->bb, pool);
274
90
    return APR_INCOMPLETE;
275
605
}
276
277