Skip to content

Commit e80e1fc

Browse files
committed
Don't require passwords to be strings in protocol
1 parent aa1e587 commit e80e1fc

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

postgres-protocol/src/message/frontend.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub enum Message<'a> {
4545
param_types: &'a [Oid],
4646
},
4747
PasswordMessage {
48-
password: &'a str,
48+
password: &'a [u8],
4949
},
5050
Query {
5151
query: &'a str,
@@ -200,8 +200,8 @@ where
200200
buf.push(b'B');
201201

202202
write_body(buf, |buf| {
203-
buf.write_cstr(portal)?;
204-
buf.write_cstr(statement)?;
203+
buf.write_cstr(portal.as_bytes())?;
204+
buf.write_cstr(statement.as_bytes())?;
205205
write_counted(formats, |f, buf| buf.write_i16::<BigEndian>(f), buf)?;
206206
write_counted(
207207
values,
@@ -249,7 +249,7 @@ pub fn close(variant: u8, name: &str, buf: &mut Vec<u8>) -> io::Result<()> {
249249
buf.push(b'C');
250250
write_body(buf, |buf| {
251251
buf.push(variant);
252-
buf.write_cstr(name)
252+
buf.write_cstr(name.as_bytes())
253253
})
254254
}
255255

@@ -272,23 +272,23 @@ pub fn copy_done(buf: &mut Vec<u8>) {
272272
#[inline]
273273
pub fn copy_fail(message: &str, buf: &mut Vec<u8>) -> io::Result<()> {
274274
buf.push(b'f');
275-
write_body(buf, |buf| buf.write_cstr(message))
275+
write_body(buf, |buf| buf.write_cstr(message.as_bytes()))
276276
}
277277

278278
#[inline]
279279
pub fn describe(variant: u8, name: &str, buf: &mut Vec<u8>) -> io::Result<()> {
280280
buf.push(b'D');
281281
write_body(buf, |buf| {
282282
buf.push(variant);
283-
buf.write_cstr(name)
283+
buf.write_cstr(name.as_bytes())
284284
})
285285
}
286286

287287
#[inline]
288288
pub fn execute(portal: &str, max_rows: i32, buf: &mut Vec<u8>) -> io::Result<()> {
289289
buf.push(b'E');
290290
write_body(buf, |buf| {
291-
buf.write_cstr(portal)?;
291+
buf.write_cstr(portal.as_bytes())?;
292292
buf.write_i32::<BigEndian>(max_rows).unwrap();
293293
Ok(())
294294
})
@@ -301,30 +301,30 @@ where
301301
{
302302
buf.push(b'P');
303303
write_body(buf, |buf| {
304-
buf.write_cstr(name)?;
305-
buf.write_cstr(query)?;
304+
buf.write_cstr(name.as_bytes())?;
305+
buf.write_cstr(query.as_bytes())?;
306306
write_counted(param_types, |t, buf| buf.write_u32::<BigEndian>(t), buf)?;
307307
Ok(())
308308
})
309309
}
310310

311311
#[inline]
312-
pub fn password_message(password: &str, buf: &mut Vec<u8>) -> io::Result<()> {
312+
pub fn password_message(password: &[u8], buf: &mut Vec<u8>) -> io::Result<()> {
313313
buf.push(b'p');
314314
write_body(buf, |buf| buf.write_cstr(password))
315315
}
316316

317317
#[inline]
318318
pub fn query(query: &str, buf: &mut Vec<u8>) -> io::Result<()> {
319319
buf.push(b'Q');
320-
write_body(buf, |buf| buf.write_cstr(query))
320+
write_body(buf, |buf| buf.write_cstr(query.as_bytes()))
321321
}
322322

323323
#[inline]
324324
pub fn sasl_initial_response(mechanism: &str, data: &[u8], buf: &mut Vec<u8>) -> io::Result<()> {
325325
buf.push(b'p');
326326
write_body(buf, |buf| {
327-
buf.write_cstr(mechanism)?;
327+
buf.write_cstr(mechanism.as_bytes())?;
328328
let len = i32::from_usize(data.len())?;
329329
buf.write_i32::<BigEndian>(len)?;
330330
buf.extend_from_slice(data);
@@ -354,8 +354,8 @@ where
354354
write_body(buf, |buf| {
355355
buf.write_i32::<BigEndian>(196_608).unwrap();
356356
for (key, value) in parameters {
357-
buf.write_cstr(key)?;
358-
buf.write_cstr(value)?;
357+
buf.write_cstr(key.as_bytes())?;
358+
buf.write_cstr(value.as_bytes())?;
359359
}
360360
buf.push(0);
361361
Ok(())
@@ -375,19 +375,19 @@ pub fn terminate(buf: &mut Vec<u8>) {
375375
}
376376

377377
trait WriteCStr {
378-
fn write_cstr(&mut self, s: &str) -> Result<(), io::Error>;
378+
fn write_cstr(&mut self, s: &[u8]) -> Result<(), io::Error>;
379379
}
380380

381381
impl WriteCStr for Vec<u8> {
382382
#[inline]
383-
fn write_cstr(&mut self, s: &str) -> Result<(), io::Error> {
384-
if s.as_bytes().contains(&0) {
383+
fn write_cstr(&mut self, s: &[u8]) -> Result<(), io::Error> {
384+
if s.contains(&0) {
385385
return Err(io::Error::new(
386386
io::ErrorKind::InvalidInput,
387387
"string contains embedded null",
388388
));
389389
}
390-
self.extend_from_slice(s.as_bytes());
390+
self.extend_from_slice(s);
391391
self.push(0);
392392
Ok(())
393393
}

tokio-postgres/src/proto/handshake.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ where
134134
Some(Message::AuthenticationCleartextPassword) => {
135135
let pass = state.password.ok_or_else(Error::missing_password)?;
136136
let mut buf = vec![];
137-
frontend::password_message(&pass, &mut buf).map_err(Error::encode)?;
137+
frontend::password_message(pass.as_bytes(), &mut buf).map_err(Error::encode)?;
138138
transition!(SendingPassword {
139139
future: state.stream.send(buf)
140140
})
@@ -144,7 +144,7 @@ where
144144
let output =
145145
authentication::md5_hash(state.user.as_bytes(), pass.as_bytes(), body.salt());
146146
let mut buf = vec![];
147-
frontend::password_message(&output, &mut buf).map_err(Error::encode)?;
147+
frontend::password_message(output.as_bytes(), &mut buf).map_err(Error::encode)?;
148148
transition!(SendingPassword {
149149
future: state.stream.send(buf)
150150
})

0 commit comments

Comments
 (0)