Skip to content

Commit 56595bd

Browse files
gechotoStypox
authored andcommitted
[YouTube] Fix extraction of n param deobfuscation function name and fixup function to prevent early return
1 parent ea40163 commit 56595bd

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ final class YoutubeThrottlingParameterUtils {
3030
private static final Pattern[] DEOBFUSCATION_FUNCTION_NAME_REGEXES = {
3131

3232
/*
33-
* The first regex matches the following text, where we want Wma and the array index
33+
* The first regex matches the following text, where we want SDa and the array index
3434
* accessed:
3535
*
36-
* a.D&&(b="nn"[+a.D],WL(a),c=a.j[b]||null)&&(c=SDa[0](c),a.set(b,c),SDa.length||Wma("")
36+
* WL(a),c=a.j[b]||null)&&(c=SDa[0](
3737
*/
38-
Pattern.compile(SINGLE_CHAR_VARIABLE_REGEX + "=\"nn\"\\[\\+" + MULTIPLE_CHARS_REGEX
39-
+ "\\." + MULTIPLE_CHARS_REGEX + "]," + MULTIPLE_CHARS_REGEX + "\\("
38+
Pattern.compile(MULTIPLE_CHARS_REGEX + "\\("
4039
+ MULTIPLE_CHARS_REGEX + "\\)," + MULTIPLE_CHARS_REGEX + "="
4140
+ MULTIPLE_CHARS_REGEX + "\\." + MULTIPLE_CHARS_REGEX + "\\["
42-
+ MULTIPLE_CHARS_REGEX + "]\\|\\|null\\).+\\|\\|(" + MULTIPLE_CHARS_REGEX
43-
+ ")\\(\"\"\\)"),
41+
+ MULTIPLE_CHARS_REGEX + "]\\|\\|null\\)&&\\(" + MULTIPLE_CHARS_REGEX + "=("
42+
+ MULTIPLE_CHARS_REGEX + ")" + ARRAY_ACCESS_REGEX + "\\("),
43+
44+
/*
45+
* This regex matches the following text, where we want Wma:
46+
*
47+
* a.D&&(b="nn"[+a.D],WL(a),c=a.j[b]||null)&&(c=SDa[0](c),a.set(b,c),SDa.length||Wma("")
48+
*
49+
* UPDATE: This is broken and Wma is not the function needed anymore.
50+
* We need the function which is in SDa[0] instead.
51+
*/
52+
// Pattern.compile(SINGLE_CHAR_VARIABLE_REGEX + "=\"nn\"\\[\\+" + MULTIPLE_CHARS_REGEX
53+
// + "\\." + MULTIPLE_CHARS_REGEX + "]," + MULTIPLE_CHARS_REGEX + "\\("
54+
// + MULTIPLE_CHARS_REGEX + "\\)," + MULTIPLE_CHARS_REGEX + "="
55+
// + MULTIPLE_CHARS_REGEX + "\\." + MULTIPLE_CHARS_REGEX + "\\["
56+
// + MULTIPLE_CHARS_REGEX + "]\\|\\|null\\).+\\|\\|(" + MULTIPLE_CHARS_REGEX
57+
// + ")\\(\"\"\\)"),
4458

4559
/*
4660
* The second regex matches the following text, where we want SDa and the array index
@@ -112,6 +126,12 @@ final class YoutubeThrottlingParameterUtils {
112126
private static final String FUNCTION_NAMES_IN_DEOBFUSCATION_ARRAY_REGEX =
113127
"\\s*=\\s*\\[(.+?)][;,]";
114128

129+
private static final String FUNCTION_ARGUMENTS_REGEX =
130+
"=\\s*function\\s*\\(\\s*([^)]*)\\s*\\)";
131+
132+
private static final String EARLY_RETURN_REGEX =
133+
";\\s*if\\s*\\(\\s*typeof\\s+" + MULTIPLE_CHARS_REGEX + "+\\s*===?\\s*([\"'])undefined\\1\\s*\\)\\s*return\\s+";
134+
115135
private YoutubeThrottlingParameterUtils() {
116136
}
117137

@@ -162,11 +182,13 @@ static String getDeobfuscationFunctionName(@Nonnull final String javaScriptPlaye
162182
static String getDeobfuscationFunction(@Nonnull final String javaScriptPlayerCode,
163183
@Nonnull final String functionName)
164184
throws ParsingException {
185+
String function;
165186
try {
166-
return parseFunctionWithLexer(javaScriptPlayerCode, functionName);
187+
function = parseFunctionWithLexer(javaScriptPlayerCode, functionName);
167188
} catch (final Exception e) {
168-
return parseFunctionWithRegex(javaScriptPlayerCode, functionName);
189+
function = parseFunctionWithRegex(javaScriptPlayerCode, functionName);
169190
}
191+
return fixupFunction(function);
170192
}
171193

172194
/**
@@ -214,4 +236,15 @@ private static String validateFunction(@Nonnull final String function) {
214236
JavaScript.compileOrThrow(function);
215237
return function;
216238
}
239+
240+
@Nonnull
241+
private static String fixupFunction(@Nonnull final String function)
242+
throws Parser.RegexException {
243+
final String firstArgName = Parser.matchGroup1(FUNCTION_ARGUMENTS_REGEX, function).split(",")[0].trim();
244+
final Pattern earlyReturnPattern = Pattern.compile(
245+
EARLY_RETURN_REGEX + firstArgName + ";",
246+
Pattern.DOTALL);
247+
final Matcher earlyReturnCodeMatcher = earlyReturnPattern.matcher(function);
248+
return earlyReturnCodeMatcher.replaceFirst(";");
249+
}
217250
}

0 commit comments

Comments
 (0)