aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Usernames and passwords in connection strings aren't escaped

Open DamianEdwards opened this issue 1 year ago • 11 comments
trafficstars

While investigating #2247 I used a parameter to create a stable password for a RabbitMQ resource, only to find that the password I'd generated contained a character that broke the connection string. RabbitMQ connection strings are actually URIs, so if the username or password contains restricted characters (e.g. @) then the RabbitMQ client fails due to the URI being invalid.

I updated ReferenceExpression to support custom value escaping and a convenience method for getting a URI escaped version of an existing ReferenceExpression and that solves the issue for runtime, but this issue will occur during manifest processing too. Resolving that is a much bigger challenge that will need some thought.

DamianEdwards avatar Mar 23 '24 00:03 DamianEdwards

I think in order to be able to solve this generically, and in all the places we need to fix it, we will need to:

  1. Have a set of "well known" escape strategies/algorithms.
  2. Encode which escape strategy to use inside the ReferenceExpression, so it gets written to the manifest.

RabbitMQ uses URIs, but PostgreSQL uses DbConnectionString. To properly escape any string in a DbConnectionString, we would need to add single quotes ' around the value, and then escape any real single quotes in the value by doubling them: ''.

So at a minimum we would need 2 strategies: URI escaping and single-quote escaping.

As you pointed out above, we would need to put this in the manifest and have any deployment tools (like azd) understand these strategies.

cc @davidfowl @mitchdenny @ellismg @vhvb1989

eerhardt avatar Mar 25 '24 15:03 eerhardt

So we'd represent this in the manifest something like this? "amqp://{urlencode(resources.rabbitusr.value)}:{urlencode(resources.rabbitpw.value)}@{resources.rabbitmq.bindings.tcp.host}:{resources.rabbitmq.bindings.tcp.port}"

DamianEdwards avatar Mar 25 '24 16:03 DamianEdwards

So we'd represent this in the manifest something like this? "amqp://{urlencode(resources.rabbitusr.value)}:{urlencode(resources.rabbitpw.value)}@{resources.rabbitmq.bindings.tcp.host}:{resources.rabbitmq.bindings.tcp.port}"

Yep - something like that is what I was thinking.

eerhardt avatar Mar 25 '24 19:03 eerhardt

@davidfowl we thinking punt this until post-GA now? Or do we think this will be common enough when considering values in URIs and connection strings that we need to mitigate it earlier?

DamianEdwards avatar Mar 26 '24 01:03 DamianEdwards

yes I think we can do something specifically for URLs and connection strings in azd without putting functions in the manifest (work needs to happen in azd anyways). I’m thinking for now, they can detect a url and do this magic encoding.

davidfowl avatar Mar 26 '24 04:03 davidfowl

So you're thinking we have AZD recognize URIs and encode themselves for GA?

DamianEdwards avatar Mar 26 '24 15:03 DamianEdwards

we have AZD recognize URIs and encode themselves for GA?

And connection strings. (at least that's what I understand from the above)

eerhardt avatar Mar 26 '24 15:03 eerhardt

So you're thinking we have AZD recognize URIs and encode themselves for GA?

Yes, that's how we address this for GA.

And connection strings. (at least that's what I understand from the above)

Yes

davidfowl avatar Mar 26 '24 15:03 davidfowl

OK I've put in preview.6 and opened https://github.com/Azure/azure-dev/issues/3598

DamianEdwards avatar Mar 26 '24 16:03 DamianEdwards

@DamianEdwards I think that Postgres containers have a similar issue.

oskardudycz avatar May 16 '24 06:05 oskardudycz

Now that we are past GA ... how do we want to handle this?

mitchdenny avatar May 24 '24 01:05 mitchdenny

@DamianEdwards is this something we want to fix for 8.1?

joperezr avatar Jun 17 '24 18:06 joperezr

@joperezr if we have a ready-to-go plan to fix it at both runtime and deployment time (i.e. in AZD and Aspir8) otherwise I'd move it out.

DamianEdwards avatar Jun 17 '24 19:06 DamianEdwards