netrc: replace fgets with Curl_get_line

Make the parser only accept complete lines and avoid problems with
overly long lines.

Reported-by: Hiroki Kurosawa

Closes #9789
This commit is contained in:
Daniel Stenberg 2022-10-04 14:37:24 +02:00
parent 84c6b1a02f
commit c97ec984fb
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 6 additions and 5 deletions

View File

@ -25,7 +25,7 @@
#include "curl_setup.h" #include "curl_setup.h"
#if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \ #if !defined(CURL_DISABLE_COOKIES) || !defined(CURL_DISABLE_ALTSVC) || \
!defined(CURL_DISABLE_HSTS) !defined(CURL_DISABLE_HSTS) || !defined(CURL_DISABLE_NETRC)
#include "curl_get_line.h" #include "curl_get_line.h"
#include "curl_memory.h" #include "curl_memory.h"
@ -33,8 +33,8 @@
#include "memdebug.h" #include "memdebug.h"
/* /*
* get_line() makes sure to only return complete whole lines that fit in 'len' * Curl_get_line() makes sure to only return complete whole lines that fit in
* bytes and end with a newline. * 'len' bytes and end with a newline.
*/ */
char *Curl_get_line(char *buf, int len, FILE *input) char *Curl_get_line(char *buf, int len, FILE *input)
{ {

View File

@ -33,6 +33,7 @@
#include "netrc.h" #include "netrc.h"
#include "strtok.h" #include "strtok.h"
#include "strcase.h" #include "strcase.h"
#include "curl_get_line.h"
/* The last 3 #include files should be in this order */ /* The last 3 #include files should be in this order */
#include "curl_printf.h" #include "curl_printf.h"
@ -82,7 +83,7 @@ static int parsenetrc(const char *host,
char netrcbuffer[4096]; char netrcbuffer[4096];
int netrcbuffsize = (int)sizeof(netrcbuffer); int netrcbuffsize = (int)sizeof(netrcbuffer);
while(!done && fgets(netrcbuffer, netrcbuffsize, file)) { while(!done && Curl_get_line(netrcbuffer, netrcbuffsize, file)) {
char *tok; char *tok;
char *tok_end; char *tok_end;
bool quoted; bool quoted;
@ -241,7 +242,7 @@ static int parsenetrc(const char *host,
} /* switch (state) */ } /* switch (state) */
tok = ++tok_end; tok = ++tok_end;
} }
} /* while fgets() */ } /* while Curl_get_line() */
out: out:
if(!retcode) { if(!retcode) {