Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curl module may accidentally reuse URL #601

Open
juntuu opened this issue Jul 9, 2024 · 0 comments · May be fixed by #602
Open

Curl module may accidentally reuse URL #601

juntuu opened this issue Jul 9, 2024 · 0 comments · May be fixed by #602

Comments

@juntuu
Copy link
Contributor

juntuu commented Jul 9, 2024

The URL from a previous call can accidentally be reused for later, misconfigured, call.

Reproduction:

local curl = require "plenary.curl"
-- 1. Call any method (even `request`) with the URL string as the first argument
curl.get("localhost", { dry_run = true }) -- { ..., "-X", "GET", "localhost" }
-- 2. Call any method (except `request`) with table as the first argument, with `url` key missing
curl.get({ dry_run = true })              -- { ..., "-X", "GET", "localhost" }

Observed behaviour:

The second call uses the URL from the previous call.

Expected behaviour:

The second call should either:

  • raise an error because the url is missing from the options, or
  • at least not use the URL from previous call
The error on missing URL could also be better... (Should this be another issue?)

I would expect either an explicit error from the library, or some error from curl itself. (Although I'm not sure if there is any meaningful case where curl could be used without the URL?)

However, now the error happens at

table.insert(result, parse.url(opts.url, opts.query))
because parse.url(nil) returns zero values.

$ lua -e 'table.insert({})'
lua: (command line):1: wrong number of arguments to 'insert'

Cause:

The spec table is shared between all the methods and calls, but the url field is not always set. This can lead to unintentional reuse of the field.

local spec = {}
local partial = function(method)
return function(url, opts)
opts = opts or {}
if type(url) == "table" then
opts = url
spec.method = method
else
spec.url = url
spec.method = method
end


I could see some use case for this behaviour, but I think it should be an explicit API. (The request method is also inconsistent with the others and bit surprising how it may end up ignoring the first argument.)

So, am I correct assuming this is a bug, not a feature?

@juntuu juntuu linked a pull request Jul 9, 2024 that will close this issue
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 a pull request may close this issue.

1 participant