Files
rustdesk/src/update_hash.rs
T
fufesou 0921a7432f feat(update): verify signed update metadata (#15914)
* feat(update): verify signed update metadata

* fix(update): harden metadata publishing and app names

- share strict app-name validation across Windows and macOS
- preserve published sidecars until replacements are ready
- select macOS test features by target architecture
- update the signing dependency and URL validation coverage

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): prevent replacing published release assets

- block reruns after signed update metadata is published
- clarify the security rationale for app-name validation

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): cache TLS responses and avoid double translations

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact: reduce the diffs

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact(update): remove obsolete update paths

- remove unused FFI update branches
- delete legacy Windows and macOS update helpers
- simplify macOS daemon updates and redundant gating

Signed-off-by: fufesou <linlong1266@gmail.com>

* feat(update): secure same-identity macOS offline updates

- verify DMGs using signed metadata, size, and SHA-256
- enforce package and bundle identity plus macOS signature checks
- support custom release policies and propagate manual update failures

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): verify Windows installers with Authenticode

Reject unsigned or untrusted EXE/MSI updates
before elevation, clean up failed verified copies,
and fix the macOS manual updater logging import.

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): clear stale URL on response body errors

Keep body-read failures in the common cleanup path and document the
cache-only Authenticode verification policy.

Signed-off-by: fufesou <linlong1266@gmail.com>

* refactor(update): remove OS signature sanity checks

Remove Windows Authenticode and macOS codesign/Gatekeeper verification.
Keep metadata, artifact integrity, bundle identity, and transaction checks.

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact(update): move update scripts into res/update

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): secure Windows MSI handoff against junction races

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): secure Windows EXE handoff against junction races

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): preserve legacy custom client app names

- allow names starting with digits or underscores
- preserve leading hyphens on Windows for compatibility
- reject leading hyphens on macOS to avoid pgrep option parsing

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact(update): remove some simple wrappers

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact(update): simple refactor

* refact(update): comments on update.scpt

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact(update): comments

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(update): create download destinations exclusively

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(macos): roll back updates when final readiness checks fail

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(msi): propagate MSI installation failures

Signed-off-by: fufesou <linlong1266@gmail.com>

* chore

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(macos): reject stale IPC sockets during updates

Require open IPC sockets in addition to launchd running state before committing the privileged update transaction. Inspect sockets with lsof to avoid triggering IPC authentication.

Add native socket regression coverage to macOS CI. Remove the unused Windows helper, combine offline metadata exports, and restore unrelated formatting changed by the PR.

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix(macos): allow offline updates without an installed bundle

Validate app-only updates against the running client's bundle identifier and check the destination identity when it exists. Replace source-text assertions with behavioral identity tests and run the macOS update tests in CI.

Signed-off-by: fufesou <linlong1266@gmail.com>

* ci, gate test with PUBLISH_UPDATE_METADATA == 'true'

Signed-off-by: fufesou <linlong1266@gmail.com>

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
2026-10-12 10:56:50 +08:00

99 lines
2.8 KiB
Rust

use sha2::{Digest, Sha256};
use std::io::{self, Read, Seek, SeekFrom};
const SHA256_HEX_LENGTH: usize = 64;
const UPDATE_HASH_BUFFER_SIZE: usize = 8192;
#[derive(Debug)]
pub(crate) enum Sha256VerificationError {
InvalidExpected,
Mismatch {
expected_sha256: String,
actual_sha256: String,
},
Io(io::Error),
}
impl From<io::Error> for Sha256VerificationError {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
pub(crate) fn sha256_reader_hex<R: Read>(reader: &mut R) -> io::Result<String> {
let mut hasher = Sha256::new();
let mut buffer = [0_u8; UPDATE_HASH_BUFFER_SIZE];
loop {
let count = match reader.read(&mut buffer) {
Ok(count) => count,
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
Err(error) => return Err(error),
};
if count == 0 {
break;
}
hasher.update(&buffer[..count]);
}
Ok(format!("{:x}", hasher.finalize()))
}
pub(crate) fn verify_sha256_reader<R: Read + Seek>(
reader: &mut R,
expected_sha256: &str,
) -> Result<(), Sha256VerificationError> {
let expected_sha256 = expected_sha256.trim().to_ascii_lowercase();
if expected_sha256.len() != SHA256_HEX_LENGTH
|| !expected_sha256.chars().all(|c| c.is_ascii_hexdigit())
{
return Err(Sha256VerificationError::InvalidExpected);
}
reader.seek(SeekFrom::Start(0))?;
let actual_sha256 = match sha256_reader_hex(reader) {
Ok(actual_sha256) => actual_sha256,
Err(error) => {
reader.seek(SeekFrom::Start(0))?;
return Err(error.into());
}
};
reader.seek(SeekFrom::Start(0))?;
if actual_sha256 != expected_sha256 {
return Err(Sha256VerificationError::Mismatch {
expected_sha256,
actual_sha256,
});
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
const RUSTDESK_SHA256: &str =
"304ca1638c5effa6832e0e15b958a8f74847efe4df9c3f3187216e921c168fed";
#[test]
fn verifies_and_rejects_sha256() {
let mut reader = Cursor::new(b"rustdesk".to_vec());
reader.set_position(reader.get_ref().len() as u64);
verify_sha256_reader(
&mut reader,
&format!(" {} ", RUSTDESK_SHA256.to_uppercase()),
)
.unwrap();
assert_eq!(reader.position(), 0);
assert!(matches!(
verify_sha256_reader(&mut reader, "invalid"),
Err(Sha256VerificationError::InvalidExpected)
));
assert!(matches!(
verify_sha256_reader(&mut reader, &"0".repeat(64)),
Err(Sha256VerificationError::Mismatch { .. })
));
assert_eq!(reader.position(), 0);
}
}