Skip to content

Commit bff7518

Browse files
committed
Throw upon trying to fetch unencrypted URL
1 parent fafd471 commit bff7518

File tree

4 files changed

+45
-31
lines changed

4 files changed

+45
-31
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Downloader.java

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.schabi.newpipe.extractor.NewPipe;
44
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
55
import org.schabi.newpipe.extractor.localization.Localization;
6+
import org.schabi.newpipe.extractor.utils.Utils;
67

78
import javax.annotation.Nonnull;
89
import javax.annotation.Nullable;
@@ -25,10 +26,10 @@ public abstract class Downloader {
2526
* localization. It should only be used when the resource that will be fetched won't be affected
2627
* by the localization.
2728
*
28-
* @param url the URL that is pointing to the wanted resource
29+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
2930
* @return the result of the GET request
3031
*/
31-
public Response get(final String url) throws IOException, ReCaptchaException {
32+
public final Response get(final String url) throws IOException, ReCaptchaException {
3233
return get(url, null, NewPipe.getPreferredLocalization());
3334
}
3435

@@ -37,24 +38,24 @@ public Response get(final String url) throws IOException, ReCaptchaException {
3738
* <br>
3839
* It will set the {@code Accept-Language} header to the language of the localization parameter.
3940
*
40-
* @param url the URL that is pointing to the wanted resource
41+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
4142
* @param localization the source of the value of the {@code Accept-Language} header
4243
* @return the result of the GET request
4344
*/
44-
public Response get(final String url, final Localization localization)
45+
public final Response get(final String url, final Localization localization)
4546
throws IOException, ReCaptchaException {
4647
return get(url, null, localization);
4748
}
4849

4950
/**
5051
* Do a GET request with the specified headers.
5152
*
52-
* @param url the URL that is pointing to the wanted resource
53+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
5354
* @param headers a list of headers that will be used in the request.
5455
* Any default headers <b>should</b> be overridden by these.
5556
* @return the result of the GET request
5657
*/
57-
public Response get(final String url, @Nullable final Map<String, List<String>> headers)
58+
public final Response get(final String url, @Nullable final Map<String, List<String>> headers)
5859
throws IOException, ReCaptchaException {
5960
return get(url, headers, NewPipe.getPreferredLocalization());
6061
}
@@ -64,17 +65,17 @@ public Response get(final String url, @Nullable final Map<String, List<String>>
6465
* <br>
6566
* It will set the {@code Accept-Language} header to the language of the localization parameter.
6667
*
67-
* @param url the URL that is pointing to the wanted resource
68+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
6869
* @param headers a list of headers that will be used in the request.
6970
* Any default headers <b>should</b> be overridden by these.
7071
* @param localization the source of the value of the {@code Accept-Language} header
7172
* @return the result of the GET request
7273
*/
73-
public Response get(final String url,
74+
public final Response get(final String url,
7475
@Nullable final Map<String, List<String>> headers,
7576
final Localization localization)
7677
throws IOException, ReCaptchaException {
77-
return execute(Request.newBuilder()
78+
return executeIfHttps(Request.newBuilder()
7879
.get(url)
7980
.headers(headers)
8081
.localization(localization)
@@ -84,24 +85,24 @@ public Response get(final String url,
8485
/**
8586
* Do a HEAD request.
8687
*
87-
* @param url the URL that is pointing to the wanted resource
88+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
8889
* @return the result of the HEAD request
8990
*/
90-
public Response head(final String url) throws IOException, ReCaptchaException {
91+
public final Response head(final String url) throws IOException, ReCaptchaException {
9192
return head(url, null);
9293
}
9394

9495
/**
9596
* Do a HEAD request with the specified headers.
9697
*
97-
* @param url the URL that is pointing to the wanted resource
98+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
9899
* @param headers a list of headers that will be used in the request.
99100
* Any default headers <b>should</b> be overridden by these.
100101
* @return the result of the HEAD request
101102
*/
102-
public Response head(final String url, @Nullable final Map<String, List<String>> headers)
103+
public final Response head(final String url, @Nullable final Map<String, List<String>> headers)
103104
throws IOException, ReCaptchaException {
104-
return execute(Request.newBuilder()
105+
return executeIfHttps(Request.newBuilder()
105106
.head(url)
106107
.headers(headers)
107108
.build());
@@ -110,13 +111,13 @@ public Response head(final String url, @Nullable final Map<String, List<String>>
110111
/**
111112
* Do a POST request with the specified headers, sending the data array.
112113
*
113-
* @param url the URL that is pointing to the wanted resource
114+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
114115
* @param headers a list of headers that will be used in the request.
115116
* Any default headers <b>should</b> be overridden by these.
116117
* @param dataToSend byte array that will be sent when doing the request.
117118
* @return the result of the POST request
118119
*/
119-
public Response post(final String url,
120+
public final Response post(final String url,
120121
@Nullable final Map<String, List<String>> headers,
121122
@Nullable final byte[] dataToSend)
122123
throws IOException, ReCaptchaException {
@@ -128,19 +129,19 @@ public Response post(final String url,
128129
* <br>
129130
* It will set the {@code Accept-Language} header to the language of the localization parameter.
130131
*
131-
* @param url the URL that is pointing to the wanted resource
132+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
132133
* @param headers a list of headers that will be used in the request.
133134
* Any default headers <b>should</b> be overridden by these.
134135
* @param dataToSend byte array that will be sent when doing the request.
135136
* @param localization the source of the value of the {@code Accept-Language} header
136137
* @return the result of the POST request
137138
*/
138-
public Response post(final String url,
139+
public final Response post(final String url,
139140
@Nullable final Map<String, List<String>> headers,
140141
@Nullable final byte[] dataToSend,
141142
final Localization localization)
142143
throws IOException, ReCaptchaException {
143-
return execute(Request.newBuilder()
144+
return executeIfHttps(Request.newBuilder()
144145
.post(url, dataToSend)
145146
.headers(headers)
146147
.localization(localization)
@@ -151,7 +152,7 @@ public Response post(final String url,
151152
* Convenient method to send a POST request using the specified value of the
152153
* {@code Content-Type} header with a given {@link Localization}.
153154
*
154-
* @param url the URL that is pointing to the wanted resource
155+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
155156
* @param headers a list of headers that will be used in the request.
156157
* Any default headers <b>should</b> be overridden by these.
157158
* @param dataToSend byte array that will be sent when doing the request.
@@ -161,7 +162,7 @@ public Response post(final String url,
161162
* @return the result of the POST request
162163
* @see #post(String, Map, byte[], Localization)
163164
*/
164-
public Response postWithContentType(final String url,
165+
public final Response postWithContentType(final String url,
165166
@Nullable final Map<String, List<String>> headers,
166167
@Nullable final byte[] dataToSend,
167168
final Localization localization,
@@ -179,7 +180,7 @@ public Response postWithContentType(final String url,
179180
* Convenient method to send a POST request using the specified value of the
180181
* {@code Content-Type} header.
181182
*
182-
* @param url the URL that is pointing to the wanted resource
183+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
183184
* @param headers a list of headers that will be used in the request.
184185
* Any default headers <b>should</b> be overridden by these.
185186
* @param dataToSend byte array that will be sent when doing the request.
@@ -188,7 +189,7 @@ public Response postWithContentType(final String url,
188189
* @return the result of the POST request
189190
* @see #post(String, Map, byte[], Localization)
190191
*/
191-
public Response postWithContentType(final String url,
192+
public final Response postWithContentType(final String url,
192193
@Nullable final Map<String, List<String>> headers,
193194
@Nullable final byte[] dataToSend,
194195
final String contentType)
@@ -201,15 +202,15 @@ public Response postWithContentType(final String url,
201202
* Convenient method to send a POST request the JSON mime type as the value of the
202203
* {@code Content-Type} header with a given {@link Localization}.
203204
*
204-
* @param url the URL that is pointing to the wanted resource
205+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
205206
* @param headers a list of headers that will be used in the request.
206207
* Any default headers <b>should</b> be overridden by these.
207208
* @param dataToSend byte array that will be sent when doing the request.
208209
* @param localization the source of the value of the {@code Accept-Language} header
209210
* @return the result of the POST request
210211
* @see #post(String, Map, byte[], Localization)
211212
*/
212-
public Response postWithContentTypeJson(final String url,
213+
public final Response postWithContentTypeJson(final String url,
213214
@Nullable final Map<String, List<String>> headers,
214215
@Nullable final byte[] dataToSend,
215216
final Localization localization)
@@ -221,26 +222,39 @@ public Response postWithContentTypeJson(final String url,
221222
* Convenient method to send a POST request the JSON mime type as the value of the
222223
* {@code Content-Type} header.
223224
*
224-
* @param url the URL that is pointing to the wanted resource
225+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
225226
* @param headers a list of headers that will be used in the request.
226227
* Any default headers <b>should</b> be overridden by these.
227228
* @param dataToSend byte array that will be sent when doing the request.
228229
* @return the result of the POST request
229230
* @see #post(String, Map, byte[], Localization)
230231
*/
231-
public Response postWithContentTypeJson(final String url,
232+
public final Response postWithContentTypeJson(final String url,
232233
@Nullable final Map<String, List<String>> headers,
233234
@Nullable final byte[] dataToSend)
234235
throws IOException, ReCaptchaException {
235236
return postWithContentTypeJson(url, headers, dataToSend,
236237
NewPipe.getPreferredLocalization());
237238
}
238239

240+
public final Response executeIfHttps(final @Nonnull Request request)
241+
throws IOException, ReCaptchaException {
242+
243+
if (!request.url().equals(Utils.replaceHttpWithHttps(request.url()))) {
244+
throw new IOException(
245+
"All queries must be made using HTTPS. Extractors must guarantee "
246+
+ "that HTTPS links are provided."
247+
);
248+
} else {
249+
return execute(request);
250+
}
251+
}
252+
239253
/**
240254
* Do a request using the specified {@link Request} object.
241255
*
242256
* @return the result of the request
243257
*/
244-
public abstract Response execute(@Nonnull Request request)
258+
protected abstract Response execute(@Nonnull Request request)
245259
throws IOException, ReCaptchaException;
246260
}

extractor/src/test/java/org/schabi/newpipe/downloader/DownloaderTestImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static DownloaderTestImpl getInstance() {
4747
}
4848

4949
@Override
50-
public Response execute(@Nonnull final Request request)
50+
protected Response execute(@Nonnull final Request request)
5151
throws IOException, ReCaptchaException {
5252
final String httpMethod = request.httpMethod();
5353
final String url = request.url();

extractor/src/test/java/org/schabi/newpipe/downloader/MockDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public MockDownloader(@Nonnull final String path) throws IOException {
4646
}
4747

4848
@Override
49-
public Response execute(@Nonnull final Request request) {
49+
protected Response execute(@Nonnull final Request request) {
5050
final Response result = mocks.get(request);
5151
if (result == null) {
5252
throw new NullPointerException("No mock response for request with url '" + request

extractor/src/test/java/org/schabi/newpipe/downloader/RecordingDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public RecordingDownloader(final String stringPath) throws IOException {
7070
public Response execute(@Nonnull final Request request) throws IOException,
7171
ReCaptchaException {
7272
final Downloader downloader = DownloaderTestImpl.getInstance();
73-
Response response = downloader.execute(request);
73+
Response response = downloader.executeIfHttps(request);
7474
String cleanedResponseBody = response.responseBody().replaceAll(IP_V4_PATTERN, "127.0.0.1");
7575
response = new Response(
7676
response.responseCode(),

0 commit comments

Comments
 (0)