1
votes

I have this function that takes a full url and splits it into a host and an endpoint to be used in a https request

void parser(char * result, char * host, char * endpoint)
{
    int i=0,j=0;
    int size = strlen(result);
    if (strstr(result , "https://"))
    {
        for (i=8; i < size; i++)
        {
            if (result[i] == '/') break;
            host[j] = result[i];
            j++;
        }
        host[j] = '\0';
        j=0;
        while ( i < strlen(result))
        {
            endpoint[j] = result[i];
            i++,j++;
        }
        endpoint[j] = '\0';
    }
}

the host and endpoint are copied to the relevant buffer correctly but when using the endpoint it results in a 400 status code (bad request) screenshot as you can see in the picture the request is malformed

this is how the request string is created

char * request = (char *)malloc(8192);
sprintf(request , "%s %s HTTP/1.1\n%s\r\n\r\n%s", "GET", endpoint, "","");

as you can see in the picture the HTTP/1.1 is inserted in the endpoint after filename%3 which causes the request to be invalid and return 400 status code

Any idea why is this happening and how to fix this problem?

https://objects.githubusercontent.com
/github-production-release-asset-2e65be/21611723/f7cfd480-a7ea-11ea-852c-52df94ee4644?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220728%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220728T104250Z&X-Amz-Expires=300&X-Amz-Signature=7dbec7d58625f02f8b4dba25f74a9b3cc688184704c228f256a6a855ab41f19c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=21611723&response-content-disposition=attachment%3B%20filename%3DQuasar.v1.4.0.zip&response-content-type=application%2Foctet-stream
URL parsed https://objects.githubusercontent.com ===> objects.githubusercontent.com:80
GET /github-production-release-asset-2e65be/21611723/f7cfd480-a7ea-11ea-852c-52df94ee4644?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220728%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220728T104250Z&X-Amz-Expires=300&X-Amz-Signature=7dbec7d58625f02f8b4dba25f74a9b3cc688184704c228f256a6a855ab41f19c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=21611723&response-content-disposition=attachment%3B%20filename%3DQuasa HTTP/1.1 ip&response-content-type=application%2Foctet-stream
Host: objects.githubusercontent.com
Connection: close

HTTP/1.1 400 Bad Request
Connection: close
Content-Length: 11
content-type: text/plain; charset=utf-8

Bad Request
Please do not post images of text because they are hard to use. Text should be posted directly as text in your question. - MikeCAT
@MikeCAT check the added text please - sinister
HTTP/1.1\n should be HTTP/1.1\r\n in order to comply with HTTP. - Andreas Wenzel
Couldn't reproduce. Please post a Minimal, Reproducible Example instead of posting only snippets that are not suitable to feed to compilers. - MikeCAT
The problem is in the caller of parser (probably endpoint is allocated too small and someone overwrites it at the end), so please show that code. Best is a minimal reproducible example. Side notes: a) strstr also finds "https://" in the middle of result, b) result is a strange name for the input parameter, c) strlen(...) in every loop run results in bad performance. - Werner Henze