Skip to content

Commit 778a5ed

Browse files
authored
Merge pull request #16 from devdiv-microsoft/connor4312/msrc/use-token-file
MSRC Fix for February 2024 Patch Tuesday
2 parents 1e790d7 + 8e32462 commit 778a5ed

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

cli/src/commands/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ pub struct ServeWebArgs {
201201
/// A secret that must be included with all requests.
202202
#[clap(long)]
203203
pub connection_token: Option<String>,
204+
/// A file containing a secret that must be included with all requests.
205+
#[clap(long)]
206+
pub connection_token_file: Option<String>,
204207
/// Run without a connection token. Only use this if the connection is secured by other means.
205208
#[clap(long)]
206209
pub without_connection_token: bool,

cli/src/commands/serve_web.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
use std::collections::HashMap;
77
use std::convert::Infallible;
8+
use std::fs;
9+
use std::io::{Read, Write};
810
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
9-
use std::path::PathBuf;
11+
use std::path::{Path, PathBuf};
1012
use std::sync::{Arc, Mutex};
1113
use std::time::{Duration, Instant};
1214

@@ -76,15 +78,14 @@ pub async fn serve_web(ctx: CommandContext, mut args: ServeWebArgs) -> Result<i3
7678
legal::require_consent(&ctx.paths, args.accept_server_license_terms)?;
7779

7880
let platform: crate::update_service::Platform = PreReqChecker::new().verify().await?;
79-
8081
if !args.without_connection_token {
8182
// Ensure there's a defined connection token, since if multiple server versions
8283
// are excuted, they will need to have a single shared token.
83-
args.connection_token = Some(
84-
args.connection_token
85-
.clone()
86-
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
87-
);
84+
let token_path = ctx.paths.root().join("serve-web-token");
85+
let token = mint_connection_token(&token_path, args.connection_token.clone())
86+
.map_err(CodeError::CouldNotCreateConnectionTokenFile)?;
87+
args.connection_token = Some(token);
88+
args.connection_token_file = Some(token_path.to_string_lossy().to_string());
8889
}
8990

9091
let cm = ConnectionManager::new(&ctx, platform, args.clone());
@@ -704,8 +705,10 @@ impl ConnectionManager {
704705
if args.args.without_connection_token {
705706
cmd.arg("--without-connection-token");
706707
}
707-
if let Some(ct) = &args.args.connection_token {
708-
cmd.arg("--connection-token");
708+
// Note: intentional that we don't pass --connection-token here, we always
709+
// convert it into the file variant.
710+
if let Some(ct) = &args.args.connection_token_file {
711+
cmd.arg("--connection-token-file");
709712
cmd.arg(ct);
710713
}
711714

@@ -779,3 +782,30 @@ struct StartArgs {
779782
release: Release,
780783
opener: BarrierOpener<Result<StartData, String>>,
781784
}
785+
786+
fn mint_connection_token(path: &Path, prefer_token: Option<String>) -> std::io::Result<String> {
787+
#[cfg(not(windows))]
788+
use std::os::unix::fs::OpenOptionsExt;
789+
790+
let mut f = fs::OpenOptions::new();
791+
f.create(true);
792+
f.write(true);
793+
f.read(true);
794+
#[cfg(not(windows))]
795+
f.mode(0o600);
796+
let mut f = f.open(path)?;
797+
798+
if prefer_token.is_none() {
799+
let mut t = String::new();
800+
f.read_to_string(&mut t)?;
801+
let t = t.trim();
802+
if !t.is_empty() {
803+
return Ok(t.to_string());
804+
}
805+
}
806+
807+
f.set_len(0)?;
808+
let prefer_token = prefer_token.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
809+
f.write_all(prefer_token.as_bytes())?;
810+
Ok(prefer_token)
811+
}

cli/src/util/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ pub enum CodeError {
512512
// todo: can be specialized when update service is moved to CodeErrors
513513
#[error("Could not check for update: {0}")]
514514
UpdateCheckFailed(String),
515+
#[error("Could not write connection token file: {0}")]
516+
CouldNotCreateConnectionTokenFile(std::io::Error)
515517
}
516518

517519
makeAnyError!(

0 commit comments

Comments
 (0)