Skip to content

Commit f8865ba

Browse files
committed
Enable token prefix masking
If the features passed by the Gitlab server indicate that there are token prefixes that should be masked, this adds those prefixes to the Masker. This is fully transparent to users of this crate; the correct thing will happen provided they correctly identify which artifacts they believe should be masked. The trace will always be masked correctly.
1 parent 1ea6feb commit f8865ba

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

gitlab-runner/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tracing-subscriber = "0.3.8"
2929
tracing = "0.1.30"
3030
doc-comment = "0.3.3"
3131
tokio-util = { version = "0.7", features = [ "io" ] }
32-
masker = "0.0.2"
32+
masker = "0.0.3"
3333

3434
[dev-dependencies]
3535
tokio = { version = "1.5.0", features = [ "full", "test-util" ] }

gitlab-runner/src/run.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bytes::Bytes;
2-
use masker::Masker;
2+
use masker::{Masker, MatchData};
33
use std::future::Future;
44
use std::path::PathBuf;
55
use std::sync::Arc;
@@ -17,6 +17,8 @@ use crate::CancellableJobHandler;
1717
use crate::{JobResult, Phase};
1818

1919
const GITLAB_MASK: &str = "[MASKED]";
20+
const GITLAB_TOKEN_SUFFIX_CHARS: &str =
21+
"-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz=";
2022

2123
async fn run<F, J, Ret>(
2224
job: Job,
@@ -195,7 +197,22 @@ impl Run {
195197
.filter(|(_, v)| v.masked)
196198
.map(|(_, v)| v.value.as_str())
197199
.collect::<Vec<_>>();
198-
let masker = Masker::new(&masked_variables, GITLAB_MASK);
200+
let prefixes = self
201+
.response
202+
.features
203+
.iter()
204+
.flat_map(|x| x.token_mask_prefixes.iter())
205+
// This matches the behaviour of the gitlab runner, which
206+
// explicitly supports a maximum of 10 prefixes.
207+
.take(10)
208+
.map(|p| MatchData {
209+
prefix: p.trim().as_bytes(),
210+
suffix: GITLAB_TOKEN_SUFFIX_CHARS.as_bytes(),
211+
mask_prefix: false,
212+
})
213+
.collect::<Vec<_>>();
214+
215+
let masker = Masker::new_with_match_data(&masked_variables, &prefixes, GITLAB_MASK);
199216

200217
let job = Job::new(
201218
self.client.clone(),

0 commit comments

Comments
 (0)