Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions iis/mymodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@
//
if ( (pszTextUTF16 == NULL) || (*pszTextUTF16 == L'\0') || cchUTF16 == 0 )
{
// Return empty string
return "";
// Return empty string. It comes from the pool, not a string literal,
// so callers may modify it in place like any other result.
return apr_pstrdup(pool, "");
}

//
Expand All @@ -206,7 +207,7 @@

if ( cbUTF8 == 0 )
{
return "";
return apr_pstrdup(pool, "");
}

//
Expand Down Expand Up @@ -300,7 +301,7 @@
}


HRESULT CMyHttpModule::ReadFileChunk(HTTP_DATA_CHUNK *chunk, char *buf)

Check failure on line 304 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 33 to the 25 allowed.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBNz&open=AaIsOUQMCrK2ivZDdBNz&pullRequest=3788
{
OVERLAPPED ovl;
DWORD dwDataStartOffset;
Expand Down Expand Up @@ -329,7 +330,7 @@
goto Done;
}

while(bytesTotal < chunk->FromFileHandle.ByteRange.Length.QuadPart)

Check warning on line 333 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reduce the number of nested "break" and "goto" statements from 3 to 1 authorized.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN0&open=AaIsOUQMCrK2ivZDdBN0&pullRequest=3788
{
DWORD bytesRead = 0;
int was_eof = 0;
Expand Down Expand Up @@ -391,8 +392,27 @@
}
}

//
// The read started at the page-aligned offset, so the first
// dwDataStartOffset bytes are before the data we want. If the read
// returned no more than that (the file ended before the requested
// range, e.g. it got shorter after its size was taken), there is
// nothing left to copy: treat it as EOF. Without this check the
// subtraction below wraps around and memcpy reads past pIoBuffer,
// and a net read of 0 bytes would make the loop spin forever.
//
if (bytesRead <= dwDataStartOffset)
{
chunk->FromFileHandle.ByteRange.Length.QuadPart = bytesTotal;
break;
}

bytesRead -= dwDataStartOffset;

if (bytesRead > m_dwPageSize - dwDataStartOffset)
{
bytesRead = m_dwPageSize - dwDataStartOffset;
}
if (bytesRead > chunk->FromFileHandle.ByteRange.Length.QuadPart)
{
bytesRead = (DWORD)chunk->FromFileHandle.ByteRange.Length.QuadPart;
Expand Down Expand Up @@ -723,7 +743,7 @@
}

REQUEST_NOTIFICATION_STATUS
CMyHttpModule::OnBeginRequest(

Check failure on line 746 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 52 to the 25 allowed.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN1&open=AaIsOUQMCrK2ivZDdBN1&pullRequest=3788
IN IHttpContext * pHttpContext,
IN IHttpEventProvider * pProvider
)
Expand Down Expand Up @@ -848,7 +868,8 @@
r->hostname = ConvertUTF16ToUTF8(req->CookedUrl.pHost, req->CookedUrl.HostLength / sizeof(WCHAR), r->pool);
r->path_info = ConvertUTF16ToUTF8(req->CookedUrl.pAbsPath, req->CookedUrl.AbsPathLength / sizeof(WCHAR), r->pool);

if(r->hostname == NULL || r->hostname[0] == '\0')
// ConvertUTF16ToUTF8() and ZeroTerminate() never return NULL
if(r->hostname[0] == '\0')
{
if(req->Headers.KnownHeaders[HttpHeaderHost].pRawValue != NULL)
r->hostname = ZeroTerminate(req->Headers.KnownHeaders[HttpHeaderHost].pRawValue,
Expand All @@ -860,11 +881,32 @@

if(r->hostname != NULL)
{
int k = 0;
char *ptr = (char *)r->hostname;

while(*ptr != 0 && *ptr != ':')
ptr++;
if(*ptr == '[')
{
// IPv6 literal such as [2001:db8::1]:8080. The address itself
// contains ':', so the port separator can only come after the
// closing bracket. Drop the brackets like Apache httpd does.
char *end = strchr(ptr, ']');

if(end != NULL)

Check failure on line 893 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the "nullptr" literal.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN2&open=AaIsOUQMCrK2ivZDdBN2&pullRequest=3788
{
*end = 0;
r->hostname = ptr + 1;
ptr = end + 1;
}
else
{
// malformed, keep the value as it is and do not look for a port
ptr += strlen(ptr);

Check warning on line 902 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make sure use of "strlen" is safe here.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN3&open=AaIsOUQMCrK2ivZDdBN3&pullRequest=3788
}
}
else
{
while(*ptr != 0 && *ptr != ':')
ptr++;
}

if(*ptr == ':')
{
Expand Down Expand Up @@ -1141,6 +1183,20 @@
IHttpContext *pHttpContext = rsc->m_pHttpContext;
IHttpRequest *pHttpRequest = pHttpContext->GetRequest();

// since we clean the APR pool at the end of OnSendRequest, we must get IIS-managed memory chunk
//
void *reqbuf = pHttpContext->AllocateRequestMemory(length);

if(reqbuf == NULL && length > 0)

Check failure on line 1190 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the "nullptr" literal.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN4&open=AaIsOUQMCrK2ivZDdBN4&pullRequest=3788
{
// The original body has already been read, so it cannot be kept.
// Fail the request instead of crashing on a NULL copy.
if(rsc->m_pProvider != NULL)
rsc->m_pProvider->SetErrorStatus(E_OUTOFMEMORY);

return APR_ENOMEM;
}

CHAR szLength[21]; //Max length for a 64 bit int is 20

ZeroMemory(szLength, sizeof(szLength));
Expand All @@ -1166,10 +1222,6 @@
// possible, but there's nothing we can do
}

// since we clean the APR pool at the end of OnSendRequest, we must get IIS-managed memory chunk
//
void *reqbuf = pHttpContext->AllocateRequestMemory(length);

memcpy(reqbuf, buf, length);

pHttpRequest->InsertEntityBody(reqbuf, length);
Expand Down Expand Up @@ -1217,12 +1269,17 @@
HTTP_RESPONSE *pRawHttpResponse = pHttpResponse->GetRawHttpResponse();
HTTP_DATA_CHUNK *pDataChunk = (HTTP_DATA_CHUNK *)apr_palloc(rsc->m_pRequestRec->pool, sizeof(HTTP_DATA_CHUNK));

pRawHttpResponse->EntityChunkCount = 0;

// since we clean the APR pool at the end of OnSendRequest, we must get IIS-managed memory chunk
//
void *reqbuf = pHttpContext->AllocateRequestMemory(length);

// Allocate before changing the response, so that on failure the
// original body is left untouched and the caller sees the error.
if(pDataChunk == NULL || (reqbuf == NULL && length > 0))

Check failure on line 1278 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the "nullptr" literal.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN5&open=AaIsOUQMCrK2ivZDdBN5&pullRequest=3788

Check failure on line 1278 in iis/mymodule.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use the "nullptr" literal.

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaIsOUQMCrK2ivZDdBN6&open=AaIsOUQMCrK2ivZDdBN6&pullRequest=3788
return APR_ENOMEM;

pRawHttpResponse->EntityChunkCount = 0;

memcpy(reqbuf, buf, length);

pDataChunk->DataChunkType = HttpDataChunkFromMemory;
Expand Down