forked from InternationalColorConsortium/iccDEV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIccIsapiHttp.cpp
More file actions
208 lines (182 loc) · 6.32 KB
/
Copy pathIccIsapiHttp.cpp
File metadata and controls
208 lines (182 loc) · 6.32 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** @file
* File: IccIsapiHttp.cpp
*
* Contains: HTTP response helpers with security headers for IIS ISAPI.
*
* Version: V1
*
* Copyright: (c) see ICC Software License
*
* See IccIsapiHttp.h for design rationale and header list.
*/
/*
* Copyright (c) International Color Consortium.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. In the absence of prior written permission, the names "ICC" and "The
* International Color Consortium" must not be used to imply that the
* ICC organization endorses or promotes products derived from this
* software.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE INTERNATIONAL COLOR CONSORTIUM OR
* ITS CONTRIBUTING MEMBERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "IccIsapiHttp.h"
#include "IccIsapiSanitize.h"
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
namespace iccIsapi {
bool SendResponse(LPEXTENSION_CONTROL_BLOCK ecb,
LPCSTR status,
LPCSTR contentType,
const std::string& body)
{
if (!ecb || !ecb->ServerSupportFunction || !ecb->WriteClient) {
return false;
}
std::string headers("Content-Type: ");
headers += contentType;
headers += "\r\nContent-Length: ";
headers += std::to_string(body.size());
headers += "\r\nCache-Control: no-store"
"\r\nX-Content-Type-Options: nosniff"
"\r\nX-Frame-Options: DENY"
"\r\nContent-Security-Policy: default-src 'none'"
"\r\nReferrer-Policy: no-referrer"
"\r\nX-XSS-Protection: 1; mode=block"
"\r\n\r\n";
HSE_SEND_HEADER_EX_INFO headerInfo{};
headerInfo.pszStatus = const_cast<LPSTR>(status);
headerInfo.cchStatus = static_cast<DWORD>(std::strlen(status));
headerInfo.pszHeader = const_cast<LPSTR>(headers.c_str());
headerInfo.cchHeader = static_cast<DWORD>(headers.size());
headerInfo.fKeepConn = FALSE;
if (!ecb->ServerSupportFunction(ecb->ConnID,
HSE_REQ_SEND_RESPONSE_HEADER_EX,
&headerInfo,
nullptr,
nullptr)) {
return false;
}
DWORD bytes = static_cast<DWORD>(body.size());
return ecb->WriteClient(ecb->ConnID,
const_cast<char*>(body.data()),
&bytes,
0) != FALSE;
}
bool Send405(LPEXTENSION_CONTROL_BLOCK ecb,
LPCSTR allowedMethod,
const std::string& message)
{
return SendResponse(ecb,
"405 Method Not Allowed",
"application/json; charset=utf-8",
std::string("{\"error\":\"") + JsonEscape(message) + "\"}");
}
bool Send400(LPEXTENSION_CONTROL_BLOCK ecb,
const std::string& rawMessage)
{
const std::string safe = SanitizeErrorMessage(rawMessage);
return SendResponse(ecb,
"400 Bad Request",
"application/json; charset=utf-8",
std::string("{\"error\":\"") + JsonEscape(safe) + "\"}");
}
bool Send500(LPEXTENSION_CONTROL_BLOCK ecb)
{
return SendResponse(ecb,
"500 Internal Server Error",
"application/json; charset=utf-8",
std::string("{\"error\":\"Internal server error.\"}"));
}
std::string GetLastErrorString(DWORD error)
{
LPSTR message = nullptr;
const DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
const DWORD length = FormatMessageA(flags,
nullptr,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&message),
0,
nullptr);
std::string text;
if (length && message) {
text.assign(message, length);
}
else {
text = "Win32 error ";
text += std::to_string(error);
}
if (message) {
LocalFree(message);
}
while (!text.empty() && (text.back() == '\r' || text.back() == '\n')) {
text.pop_back();
}
return text;
}
std::vector<unsigned char> ReadRequestBody(LPEXTENSION_CONTROL_BLOCK ecb,
size_t maxBytes)
{
if (!ecb) {
return {};
}
const DWORD totalBytes = std::max(ecb->cbTotalBytes, ecb->cbAvailable);
// Reject oversized uploads BEFORE allocation — CWE-789 prevention.
if (static_cast<size_t>(totalBytes) > maxBytes) {
return {};
}
std::vector<unsigned char> body(totalBytes);
DWORD copied = 0;
if (totalBytes == 0) {
return body;
}
if (ecb->cbAvailable && ecb->lpbData) {
copied = std::min(ecb->cbAvailable, totalBytes);
std::memcpy(body.data(), ecb->lpbData, copied);
}
while (copied < totalBytes) {
DWORD chunk = totalBytes - copied;
if (!ecb->ReadClient ||
!ecb->ReadClient(ecb->ConnID, body.data() + copied, &chunk) ||
chunk == 0) {
body.resize(copied);
return body;
}
copied += chunk;
}
return body;
}
} // namespace iccIsapi