Skip to content

Commit 9366430

Browse files
ColinKinlochrefi64
authored andcommitted
dsc: Discard PGP from dsc files
It's common for dsc files to be signed with PGP clearsign. This results in `rfc822-like` throwing with the error `Line 1 doesn't contain a colon`.
1 parent dc5a044 commit 9366430

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/dsc.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,55 @@ pub struct Dsc {
9898
pub files: Vec<FileEntry>,
9999
}
100100

101+
pub fn discard_pgp(dsc: &str) -> &str {
102+
const PGP_HEADER_START: &str = "-----BEGIN PGP SIGNED MESSAGE-----\n";
103+
const PGP_HEADER_END: &str = "\n\n";
104+
const PGP_FOOTER_START: &str = "-----BEGIN PGP SIGNATURE-----\n";
105+
106+
if dsc.starts_with(PGP_HEADER_START) {
107+
if let Some((_gpg_header, payload_and_sig)) = dsc.split_once(PGP_HEADER_END) {
108+
if let Some((payload, _sig)) = payload_and_sig.split_once(PGP_FOOTER_START) {
109+
return payload;
110+
}
111+
}
112+
}
113+
114+
dsc
115+
}
116+
101117
#[cfg(test)]
102118
mod tests {
103119
use claims::*;
104120

105121
use super::*;
106122

123+
const TEST_GPG_DSC: &str = "-----BEGIN PGP SIGNED MESSAGE-----
124+
Hash: SHA512
125+
126+
Source: abc
127+
SomeUnknownAttribute: 10
128+
Files:
129+
hash1 10 file1
130+
hash2 27 file2
131+
-----BEGIN PGP SIGNATURE-----
132+
133+
iQEzBAEBCgAdFiEEQxKqmcM1tb5xMnn0axtijDBFQh4FAmebtGQACgkQaxtijDBF
134+
Qh6iAwf+NfOEM4+DbA8PPZnVz12bBqBNgMdaOx8CisQtd9xTmOMECaF3u2Vpfcha
135+
zWRVtVh7Js2UidlHEwdzVJuNwrkneBoIHJEyOd/X2EXI8hOlU71OJGCyx1fayDNp
136+
zf9Fe9kmlF9PJZRpB33YcTDSf5fYMNG2b4osa0ICOKssXoIbNVjaEPDdx3h/gsVm
137+
x/JPxsxWjuM98ALa3ncn4UUPrn4QQfbu73qFEKyOLqhjCZxb52LG5/w96bXQodPS
138+
Zhy+ZtJTpPJA9kuz9vZimQMPVimxUhYQQlBTl+3Bcg2Afw1X57H4MpkS+UPi16id
139+
+DdRlEyxB4frFnYXK84u3VYR3Ml+8A==
140+
=wcHv
141+
-----END PGP SIGNATURE-----
142+
";
143+
107144
const TEST_DSC: &str = "Source: abc
108145
SomeUnknownAttribute: 10
109146
Files:
110147
hash1 10 file1
111-
hash2 27 file2";
148+
hash2 27 file2
149+
";
112150

113151
#[test]
114152
fn test_de() {
@@ -125,4 +163,9 @@ Files:
125163
assert_eq!(dsc.files[1].size, 27);
126164
assert_eq!(dsc.files[1].filename, "file2");
127165
}
166+
167+
#[test]
168+
fn test_gpg_de() {
169+
assert_eq!(discard_pgp(TEST_GPG_DSC), TEST_DSC);
170+
}
128171
}

src/upload.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ use md5::{Digest, Md5};
1010
use open_build_service_api as obs;
1111
use tracing::{Instrument, debug, info_span, instrument, trace};
1212

13-
use crate::{artifacts::ArtifactDirectory, dsc::Dsc, retry_request};
13+
use crate::{
14+
artifacts::ArtifactDirectory,
15+
dsc::{Dsc, discard_pgp},
16+
retry_request,
17+
};
1418

1519
type Md5String = String;
1620

@@ -83,8 +87,8 @@ impl ObsDscUploader {
8387
.read_string(dsc_path.as_str())
8488
.await
8589
.wrap_err("Failed to read dsc")?;
86-
let dsc: Dsc = rfc822_like::from_str(&dsc_contents).wrap_err("Failed to parse dsc")?;
87-
90+
let dsc: Dsc =
91+
rfc822_like::from_str(discard_pgp(&dsc_contents)).wrap_err("Failed to parse dsc")?;
8892
let package = dsc.source.to_owned();
8993

9094
if let Some(branch_to) = branch_to {
@@ -188,7 +192,8 @@ impl ObsDscUploader {
188192
)?;
189193

190194
let _span = info_span!("find_files_to_remove:parse", %file);
191-
let dsc: Dsc = rfc822_like::from_bytes(&contents[..])?;
195+
let dsc: Dsc =
196+
rfc822_like::from_str(discard_pgp(std::str::from_utf8(&contents[..])?))?;
192197

193198
to_remove.extend(dsc.files.into_iter().map(|f| f.filename));
194199
} else if file.ends_with(".changes") {

0 commit comments

Comments
 (0)