1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// Copyright 2020 Google LLC
// Copyright 2025 Mikhail Khachaiants <mkhachaiants@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "uriparser/Uri.h"
#include "uriparser/UriIp4.h"
#include "FuzzingUtils.h"
#include <cstddef>
#include <cstring>
#include <vector>
class UriHolder {
public:
UriHolder() {
memset((void *)&uri_, 0, sizeof(uri_));
}
~UriHolder() {
URI_FUNC(FreeUriMembers)(&uri_);
}
URI_TYPE(Uri) * get() {
return &uri_;
}
private:
URI_TYPE(Uri) uri_;
};
void Escapes(const UriString & uri) {
const URI_CHAR * first = uri.c_str();
// Up to 6 bytes per character with normalizeBreaks enabled (\n -> %0D%0A)
std::vector<URI_CHAR> buf1(uri.size() * 6 + 1);
// and up to 3 bytes per character otherwise
std::vector<URI_CHAR> buf2(uri.size() * 3 + 1);
URI_FUNC(Escape)(first, &buf1[0], URI_TRUE, URI_TRUE);
URI_FUNC(Escape)(first, &buf1[0], URI_FALSE, URI_TRUE);
if (buf1.data()) {
URI_FUNC(UnescapeInPlace)(&buf1[0]);
}
URI_FUNC(Escape)(first, &buf2[0], URI_TRUE, URI_FALSE);
URI_FUNC(Escape)(first, &buf2[0], URI_FALSE, URI_FALSE);
if (buf2.data()) {
URI_FUNC(UnescapeInPlace)(&buf2[0]);
}
}
void FileNames(const UriString & uri) {
const size_t size = 8 + 3 * uri.size() + 1;
std::vector<URI_CHAR> buf(size);
URI_FUNC(UnixFilenameToUriString)(uri.c_str(), &buf[0]);
URI_FUNC(WindowsFilenameToUriString)(uri.c_str(), &buf[0]);
URI_FUNC(UriStringToUnixFilename)(uri.c_str(), &buf[0]);
URI_FUNC(UriStringToWindowsFilename)(uri.c_str(), &buf[0]);
}
void Ipv4(const UriString & s) {
const URI_CHAR * cstr = s.c_str();
unsigned char result[4] = {};
URI_FUNC(ParseIpFourAddress)(result, cstr, &cstr[s.size()]);
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) {
FuzzedDataProvider stream(data, size);
bool domainRelative = stream.ConsumeBool();
const UriString uri1 = consumeRandomLengthString(stream);
const UriString uri2 = consumeRemainingBytesAsString(stream);
Escapes(uri1);
Escapes(uri2);
FileNames(uri1);
FileNames(uri2);
Ipv4(uri1);
Ipv4(uri2);
UriHolder uriHolder1;
URI_TYPE(ParserState) state1;
state1.uri = uriHolder1.get();
if (URI_FUNC(ParseUri)(&state1, uri1.c_str()) != URI_SUCCESS) {
return 0;
}
URI_CHAR buf[1024 * 8] = {0};
int written = 0;
URI_FUNC(ToString)(buf, state1.uri, sizeof(buf) / sizeof(buf[0]), &written);
UriHolder uriHolder2;
if (URI_FUNC(ParseSingleUri)(uriHolder2.get(), uri2.c_str(), nullptr)
!= URI_SUCCESS) {
return 0;
}
URI_FUNC(EqualsUri)(state1.uri, uriHolder2.get());
unsigned int mask = 0;
URI_FUNC(NormalizeSyntaxMaskRequiredEx)(state1.uri, &mask);
URI_FUNC(NormalizeSyntax)(state1.uri);
URI_TYPE(Uri) absUri;
URI_FUNC(AddBaseUri)(&absUri, state1.uri, uriHolder2.get());
URI_FUNC(FreeUriMembers)(&absUri);
URI_TYPE(Uri) relUri;
URI_FUNC(RemoveBaseUri)(&relUri, state1.uri, uriHolder2.get(), domainRelative);
URI_FUNC(FreeUriMembers)(&relUri);
return 0;
}
|