Skip to content

Commit 9e93d6b

Browse files
committed
YoutubeThrottlingDecrypter: Check if returned string is a valid JavaScript function
1 parent ed0a07a commit 9e93d6b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import org.schabi.newpipe.extractor.utils.Parser;
66
import org.schabi.newpipe.extractor.utils.StringUtils;
77

8-
import javax.annotation.Nonnull;
98
import java.util.HashMap;
109
import java.util.Map;
1110
import java.util.regex.Matcher;
1211
import java.util.regex.Pattern;
1312

13+
import javax.annotation.Nonnull;
14+
1415
/**
1516
* YouTube's streaming URLs of HTML5 clients are protected with a cipher, which modifies their
1617
* {@code n} query parameter.
@@ -154,16 +155,25 @@ private static String parseDecodeFunction(final String playerJsCode, final Strin
154155
private static String parseWithParenthesisMatching(final String playerJsCode,
155156
final String functionName) {
156157
final String functionBase = functionName + "=function";
157-
return functionBase + StringUtils.matchToClosingParenthesis(playerJsCode, functionBase)
158-
+ ";";
158+
return validateFunction(functionBase
159+
+ StringUtils.matchToClosingParenthesis(playerJsCode, functionBase)
160+
+ ";");
159161
}
160162

161163
@Nonnull
162164
private static String parseWithRegex(final String playerJsCode, final String functionName)
163165
throws Parser.RegexException {
164166
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?};)\n",
165167
Pattern.DOTALL);
166-
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
168+
return validateFunction("function "
169+
+ functionName
170+
+ Parser.matchGroup1(functionPattern, playerJsCode));
171+
}
172+
173+
@Nonnull
174+
private static String validateFunction(@Nonnull final String function) {
175+
JavaScript.compileOrThrow(function);
176+
return function;
167177
}
168178

169179
@Deprecated

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ public final class JavaScript {
99
private JavaScript() {
1010
}
1111

12+
public static void compileOrThrow(final String function) {
13+
try {
14+
final Context context = Context.enter();
15+
context.setOptimizationLevel(-1);
16+
17+
// If it doesn't compile it throws an exception here
18+
context.compileString(function, null, 1, null);
19+
} finally {
20+
Context.exit();
21+
}
22+
}
23+
1224
public static String run(final String function,
1325
final String functionName,
1426
final String... parameters) {

0 commit comments

Comments
 (0)