Dynamic (and secure) curl requests via sqlpage.exec()? #215
-
Hi SQLPagers, I'm looking for a secure way to pass dynamic content to the What I'm thinking so far is to create a For instance, here's a contact form endpoint that expects posted form values and passes them to the Postmark email service via curl request.
set token_header = 'X-Postmark-Server-Token: ' || sqlpage.environment_variable('postmark_token');
set data = json_object(
"To", "[email protected]",
"Subject", "New Inquiry: " || :subject,
"From", "[email protected]",
"ReplyTo", :email,
"TextBody", :message,
"HtmlBody", :message,
"MessageStream", "outbound"
);
set response = sqlpage.exec('curl',
'https://api.postmarkapp.com/email',
'-H', $token_header,
'--json', $data
);
select 'json' as component,
$response as contents; @lovasoa: would you say this is a good approach? If not, I wonder if it's feasible to construct a Dynamic curl requests would open many doors 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I'm not sure what your requirements are and what you mean by secure: what is the user expected to be able to do and what should they not be able to do? In the snippet you shared, the user can send any email to any address they want, which I would usually qualify as a vulnerability, but it seems that it's on purpose. Is it? You mention it being a "contact form". If it's a contact form, then the destination address should be hardcoded (or come from an environment variable), shouldn't it ? If the question was about a malicious user being able to add or remove fields to the json object, or otherwise manipulate the request body, then that's not a problem. |
Beta Was this translation helpful? Give feedback.
-
🎉You can do that natively, faster, and more securely in sqlpage without curl as of v0.20.3. |
Beta Was this translation helpful? Give feedback.
Yes, that's a fair assumption. Your previous example is not only guaranteed not to call anything other than curl, but also to pass only valid json to it. It is safe from shell injection.
Even
sqlpage.exec('curl', $attacker_controlled)
would be safe from shell injection (in that it would execute nothing other than curl with a single argument, whatever the attacker does).As a general rule of thumb, the first argument to sqlpage.exec should always be hardcoded to a known safe program. It is in your case.
The only security issue I see in your example, as I said, is the lack of validation of the destination address (and maybe the subject line).