Compare commits

..

2 commits

Author SHA1 Message Date
1fe6d01978
https://github.com/iv-org/invidious/issues/5108#issuecomment-2529137289
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 2m28s
2024-12-09 16:46:23 -03:00
c35adbcc0a
apply patch from @inedia:ahimsa.chat
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m26s
Tracking issue at https://github.com/iv-org/invidious/issues/5108
2024-12-09 16:09:54 -03:00
4 changed files with 104 additions and 46 deletions

75
X7dZ.patch Normal file
View file

@ -0,0 +1,75 @@
diff --git a/src/player.rs b/src/player.rs
index 23333b3..a3d79af 100644
--- a/src/player.rs
+++ b/src/player.rs
@@ -18,8 +18,10 @@ pub enum FetchUpdateStatus {
CannotFetchPlayerJS,
NsigRegexCompileFailed,
PlayerAlreadyUpdated,
+ SignatureExtractionFailed,
}
+
pub async fn fetch_update(state: Arc<GlobalState>) -> Result<(), FetchUpdateStatus> {
let global_state = state.clone();
let response = match reqwest::get(TEST_YOUTUBE_VIDEO).await {
@@ -145,33 +147,39 @@ pub async fn fetch_update(state: Arc<GlobalState>) -> Result<(), FetchUpdateStat
}
// Extract signature function name
- let sig_function_name = REGEX_SIGNATURE_FUNCTION
- .captures(&player_javascript)
- .unwrap()
- .get(1)
- .unwrap()
- .as_str();
+ let sig_function_name = match REGEX_SIGNATURE_FUNCTION.captures(&player_javascript) {
+ Some(captures) => match captures.get(1) {
+ Some(m) => m.as_str(),
+ None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ },
+ None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ };
let mut sig_function_body_regex_str: String = String::new();
sig_function_body_regex_str += &sig_function_name.replace("$", "\\$");
sig_function_body_regex_str += "=function\\([a-zA-Z0-9_]+\\)\\{.+?\\}";
- let sig_function_body_regex = Regex::new(&sig_function_body_regex_str).unwrap();
+ let sig_function_body_regex = match Regex::new(&sig_function_body_regex_str) {
+ Ok(r) => r,
+ Err(_) => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ };
- let sig_function_body = sig_function_body_regex
- .captures(&player_javascript)
- .unwrap()
- .get(0)
- .unwrap()
- .as_str();
+ let sig_function_body = match sig_function_body_regex.captures(&player_javascript) {
+ Some(captures) => match captures.get(0) {
+ Some(m) => m.as_str(),
+ None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ },
+ None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ };
- // Get the helper object
- let helper_object_name = REGEX_HELPER_OBJ_NAME
- .captures(sig_function_body)
- .unwrap()
- .get(1)
- .unwrap()
- .as_str();
+ // Get the helper object
+ let helper_object_name = match REGEX_HELPER_OBJ_NAME.captures(sig_function_body) {
+ Some(captures) => match captures.get(1) {
+ Some(m) => m.as_str(),
+ None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ },
+ None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
+ };
let mut helper_object_body_regex_str = String::new();
helper_object_body_regex_str += "(var ";

View file

@ -20,20 +20,9 @@ pub static NSIG_FUNCTION_ENDINGS: &[&str] = &[
pub static REGEX_SIGNATURE_TIMESTAMP: &Lazy<Regex> = regex!("signatureTimestamp[=:](\\d+)");
pub static REGEX_SIGNATURE_FUNCTION: Lazy<Regex> = Lazy::new(|| {
Regex::new(concat!(
r#"(?:"#,
// Pattern 1
r#"\b[a-zA-Z0-9$]+&&\([a-zA-Z0-9$]+=([a-zA-Z0-9$]{2,})\(decodeURIComponent\([a-zA-Z0-9$]+\)\)\)"#,
r#"|"#,
// Pattern 2
r#"([a-zA-Z0-9$]+)\s*=\s*function\(\s*[a-zA-Z0-9$]+\s*\)\s*\{\s*[^}]+?\.split\(\s*""\s*\)[^}]+?\.join\(\s*""\s*\)"#,
r#"|"#,
// Pattern 3
r#"(?:\b|[^a-zA-Z0-9$])([a-zA-Z0-9$]{2,})\s*=\s*function\(\s*a\s*\)\s*\{\s*a\s*=\s*a\.split\(\s*""\s*\)"#,
r#")"#
)).unwrap()
});
pub static REGEX_SIGNATURE_FUNCTION: &Lazy<Regex> =
regex!("\\b[a-zA-Z]+&&\\([a-zA-Z]+=([a-zA-Z0-9$]{2,})\\(decodeURIComponent\\([a-zA-Z]+\\)\\)");
pub static REGEX_HELPER_OBJ_NAME: &Lazy<Regex> = regex!(";([A-Za-z0-9_\\$]{2,})\\...\\(");
pub static NSIG_FUNCTION_NAME: &str = "decrypt_nsig";

View file

@ -18,13 +18,9 @@ pub enum FetchUpdateStatus {
CannotFetchPlayerJS,
NsigRegexCompileFailed,
PlayerAlreadyUpdated,
CannotMatchSignature
SignatureExtractionFailed,
}
fn fixup_n_function_code(code: &str) -> String {
let re = regex::Regex::new(r#";\s*if\s*\(\s*typeof\s+[a-zA-Z0-9_$]+\s*===?\s*["']undefined["']\s*\)\s*return\s+[a-zA-Z0-9_$]+;"#).unwrap();
re.replace_all(code, ";").to_string()
}
pub async fn fetch_update(state: Arc<GlobalState>) -> Result<(), FetchUpdateStatus> {
let global_state = state.clone();
@ -146,46 +142,44 @@ pub async fn fetch_update(state: Arc<GlobalState>) -> Result<(), FetchUpdateStat
i.get(1).unwrap().as_str()
}
};
nsig_function_code = fixup_n_function_code(&nsig_function_code);
debug!("got nsig fn code: {}", nsig_function_code);
break;
}
// Extract signature function name
let sig_function_name = match REGEX_SIGNATURE_FUNCTION.captures(&player_javascript) {
Some(captures) => {
// Try groups 1, 2, and 3 which contain the signature function name
[1, 2, 3].iter()
.find_map(|&i| captures.get(i))
.map(|m| m.as_str())
.ok_or(FetchUpdateStatus::CannotMatchSignature)?
}
None => {
error!("Could not match signature function pattern");
return Err(FetchUpdateStatus::CannotMatchSignature);
}
Some(captures) => match captures.get(1) {
Some(m) => m.as_str(),
None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
},
None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
};
debug!("sig function name: {}", sig_function_name);
let mut sig_function_body_regex_str: String = String::new();
sig_function_body_regex_str += &sig_function_name.replace("$", "\\$");
sig_function_body_regex_str += "=function\\([a-zA-Z0-9_]+\\)\\{.+?\\}";
let sig_function_body_regex = Regex::new(&sig_function_body_regex_str).unwrap();
let sig_function_body_regex = match Regex::new(&sig_function_body_regex_str) {
Ok(r) => r,
Err(_) => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
};
let sig_function_body = sig_function_body_regex
.captures(&player_javascript)
.unwrap()
.get(0)
.unwrap()
.as_str();
let sig_function_body = match sig_function_body_regex.captures(&player_javascript) {
Some(captures) => match captures.get(0) {
Some(m) => m.as_str(),
None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
},
None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
};
// Get the helper object
let helper_object_name = REGEX_HELPER_OBJ_NAME
.captures(sig_function_body)
.unwrap()
.get(1)
.unwrap()
.as_str();
// Get the helper object
let helper_object_name = match REGEX_HELPER_OBJ_NAME.captures(sig_function_body) {
Some(captures) => match captures.get(1) {
Some(m) => m.as_str(),
None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
},
None => return Err(FetchUpdateStatus::NsigRegexCompileFailed),
};
let mut helper_object_body_regex_str = String::new();
helper_object_body_regex_str += "(var ";