YoutubeThrottlingDecrypter: Check if returned string is a valid JavaScript function

This commit is contained in:
litetex 2022-08-21 20:16:45 +02:00
parent ed0a07af4e
commit 9e93d6b193
2 changed files with 26 additions and 4 deletions

View File

@ -5,12 +5,13 @@ import org.schabi.newpipe.extractor.utils.JavaScript;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.StringUtils;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
/**
* YouTube's streaming URLs of HTML5 clients are protected with a cipher, which modifies their
* {@code n} query parameter.
@ -154,8 +155,9 @@ public class YoutubeThrottlingDecrypter {
private static String parseWithParenthesisMatching(final String playerJsCode,
final String functionName) {
final String functionBase = functionName + "=function";
return functionBase + StringUtils.matchToClosingParenthesis(playerJsCode, functionBase)
+ ";";
return validateFunction(functionBase
+ StringUtils.matchToClosingParenthesis(playerJsCode, functionBase)
+ ";");
}
@Nonnull
@ -163,7 +165,15 @@ public class YoutubeThrottlingDecrypter {
throws Parser.RegexException {
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?};)\n",
Pattern.DOTALL);
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
return validateFunction("function "
+ functionName
+ Parser.matchGroup1(functionPattern, playerJsCode));
}
@Nonnull
private static String validateFunction(@Nonnull final String function) {
JavaScript.compileOrThrow(function);
return function;
}
@Deprecated

View File

@ -9,6 +9,18 @@ public final class JavaScript {
private JavaScript() {
}
public static void compileOrThrow(final String function) {
try {
final Context context = Context.enter();
context.setOptimizationLevel(-1);
// If it doesn't compile it throws an exception here
context.compileString(function, null, 1, null);
} finally {
Context.exit();
}
}
public static String run(final String function,
final String functionName,
final String... parameters) {