Skip to content

Modify parse_cookies to split on first occurrence of '=' only#9

Closed
rpkyle wants to merge 1 commit intothomasp85:masterfrom
plotly:fix-cookie-handling
Closed

Modify parse_cookies to split on first occurrence of '=' only#9
rpkyle wants to merge 1 commit intothomasp85:masterfrom
plotly:fix-cookie-handling

Conversation

@rpkyle
Copy link
Copy Markdown

@rpkyle rpkyle commented Aug 26, 2019

The parse_cookies function in request.R attempts to split cookies on each occurrence of =; this appears to generate errors such as the following when multiple instances of = occur within a cookie:

ERROR: 'names' attribute [11] must be the same length as the vector [10]

This may be reproduced by using curl or a similar tool to pass a cookie string containing two or more =, e.g.

curl -v -H "Cookie: a=1=2" 127.0.0.1:8050

The original lines are here:

reqres/R/request.R

Lines 437 to 445 in c69c053

parse_cookies = function() {
if (is.null(self$headers$Cookie)) return(list())
cookies <- trimws(strsplit(self$headers$Cookie, ';')[[1]])
cookies <- unlist(strsplit(cookies, '='))
structure(
as.list(url_decode(cookies[c(FALSE, TRUE)])),
names = cookies[c(TRUE, FALSE)]
)
},

This pull request proposes to modify line 440:

cookies <- unlist(strsplit(sub("=", ";", cookies), ";"))

Since sub will only replace the first matching =, strsplit will split the line only once.

@alexcjohnson
Copy link
Copy Markdown

FWIW we encountered this issue in conjunction with google analytics, which creates cookies like:
__utmz=123456789.1234567890.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);

@thomasp85
Copy link
Copy Markdown
Owner

I've opted to use stringi instead of substituting = with ; before the split. The outcome is the same though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants