Skip to content

Commit 8496f46

Browse files
committed
Throw upon trying to fetch unencrypted URL
1 parent 7d0d217 commit 8496f46

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

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

+42-30
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,44 +65,42 @@ 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)
79-
.headers(headers)
80-
.localization(localization)
8180
.build());
8281
}
8382

8483
/**
8584
* Do a HEAD request.
8685
*
87-
* @param url the URL that is pointing to the wanted resource
86+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
8887
* @return the result of the HEAD request
8988
*/
90-
public Response head(final String url) throws IOException, ReCaptchaException {
89+
public final Response head(final String url) throws IOException, ReCaptchaException {
9190
return head(url, null);
9291
}
9392

9493
/**
9594
* Do a HEAD request with the specified headers.
9695
*
97-
* @param url the URL that is pointing to the wanted resource
96+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
9897
* @param headers a list of headers that will be used in the request.
9998
* Any default headers <b>should</b> be overridden by these.
10099
* @return the result of the HEAD request
101100
*/
102-
public Response head(final String url, @Nullable final Map<String, List<String>> headers)
101+
public final Response head(final String url, @Nullable final Map<String, List<String>> headers)
103102
throws IOException, ReCaptchaException {
104-
return execute(Request.newBuilder()
103+
return executeIfHttps(Request.newBuilder()
105104
.head(url)
106105
.headers(headers)
107106
.build());
@@ -110,13 +109,13 @@ public Response head(final String url, @Nullable final Map<String, List<String>>
110109
/**
111110
* Do a POST request with the specified headers, sending the data array.
112111
*
113-
* @param url the URL that is pointing to the wanted resource
112+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
114113
* @param headers a list of headers that will be used in the request.
115114
* Any default headers <b>should</b> be overridden by these.
116115
* @param dataToSend byte array that will be sent when doing the request.
117116
* @return the result of the POST request
118117
*/
119-
public Response post(final String url,
118+
public final Response post(final String url,
120119
@Nullable final Map<String, List<String>> headers,
121120
@Nullable final byte[] dataToSend)
122121
throws IOException, ReCaptchaException {
@@ -128,19 +127,19 @@ public Response post(final String url,
128127
* <br>
129128
* It will set the {@code Accept-Language} header to the language of the localization parameter.
130129
*
131-
* @param url the URL that is pointing to the wanted resource
130+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
132131
* @param headers a list of headers that will be used in the request.
133132
* Any default headers <b>should</b> be overridden by these.
134133
* @param dataToSend byte array that will be sent when doing the request.
135134
* @param localization the source of the value of the {@code Accept-Language} header
136135
* @return the result of the POST request
137136
*/
138-
public Response post(final String url,
137+
public final Response post(final String url,
139138
@Nullable final Map<String, List<String>> headers,
140139
@Nullable final byte[] dataToSend,
141140
final Localization localization)
142141
throws IOException, ReCaptchaException {
143-
return execute(Request.newBuilder()
142+
return executeIfHttps(Request.newBuilder()
144143
.post(url, dataToSend)
145144
.headers(headers)
146145
.localization(localization)
@@ -151,7 +150,7 @@ public Response post(final String url,
151150
* Convenient method to send a POST request using the specified value of the
152151
* {@code Content-Type} header with a given {@link Localization}.
153152
*
154-
* @param url the URL that is pointing to the wanted resource
153+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
155154
* @param headers a list of headers that will be used in the request.
156155
* Any default headers <b>should</b> be overridden by these.
157156
* @param dataToSend byte array that will be sent when doing the request.
@@ -161,7 +160,7 @@ public Response post(final String url,
161160
* @return the result of the POST request
162161
* @see #post(String, Map, byte[], Localization)
163162
*/
164-
public Response postWithContentType(final String url,
163+
public final Response postWithContentType(final String url,
165164
@Nullable final Map<String, List<String>> headers,
166165
@Nullable final byte[] dataToSend,
167166
final Localization localization,
@@ -179,7 +178,7 @@ public Response postWithContentType(final String url,
179178
* Convenient method to send a POST request using the specified value of the
180179
* {@code Content-Type} header.
181180
*
182-
* @param url the URL that is pointing to the wanted resource
181+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
183182
* @param headers a list of headers that will be used in the request.
184183
* Any default headers <b>should</b> be overridden by these.
185184
* @param dataToSend byte array that will be sent when doing the request.
@@ -188,7 +187,7 @@ public Response postWithContentType(final String url,
188187
* @return the result of the POST request
189188
* @see #post(String, Map, byte[], Localization)
190189
*/
191-
public Response postWithContentType(final String url,
190+
public final Response postWithContentType(final String url,
192191
@Nullable final Map<String, List<String>> headers,
193192
@Nullable final byte[] dataToSend,
194193
final String contentType)
@@ -201,15 +200,15 @@ public Response postWithContentType(final String url,
201200
* Convenient method to send a POST request the JSON mime type as the value of the
202201
* {@code Content-Type} header with a given {@link Localization}.
203202
*
204-
* @param url the URL that is pointing to the wanted resource
203+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
205204
* @param headers a list of headers that will be used in the request.
206205
* Any default headers <b>should</b> be overridden by these.
207206
* @param dataToSend byte array that will be sent when doing the request.
208207
* @param localization the source of the value of the {@code Accept-Language} header
209208
* @return the result of the POST request
210209
* @see #post(String, Map, byte[], Localization)
211210
*/
212-
public Response postWithContentTypeJson(final String url,
211+
public final Response postWithContentTypeJson(final String url,
213212
@Nullable final Map<String, List<String>> headers,
214213
@Nullable final byte[] dataToSend,
215214
final Localization localization)
@@ -221,26 +220,39 @@ public Response postWithContentTypeJson(final String url,
221220
* Convenient method to send a POST request the JSON mime type as the value of the
222221
* {@code Content-Type} header.
223222
*
224-
* @param url the URL that is pointing to the wanted resource
223+
* @param url the URL that is pointing to the wanted resource (must start with HTTPS)
225224
* @param headers a list of headers that will be used in the request.
226225
* Any default headers <b>should</b> be overridden by these.
227226
* @param dataToSend byte array that will be sent when doing the request.
228227
* @return the result of the POST request
229228
* @see #post(String, Map, byte[], Localization)
230229
*/
231-
public Response postWithContentTypeJson(final String url,
230+
public final Response postWithContentTypeJson(final String url,
232231
@Nullable final Map<String, List<String>> headers,
233232
@Nullable final byte[] dataToSend)
234233
throws IOException, ReCaptchaException {
235234
return postWithContentTypeJson(url, headers, dataToSend,
236235
NewPipe.getPreferredLocalization());
237236
}
238237

238+
public final Response executeIfHttps(final @Nonnull Request request)
239+
throws IOException, ReCaptchaException {
240+
241+
if (!request.url().equals(Utils.replaceHttpWithHttps(request.url()))) {
242+
throw new IOException(
243+
"All queries must be made using HTTPS. Extractors must guarantee "
244+
+ "that HTTPS links are provided."
245+
);
246+
} else {
247+
return execute(request);
248+
}
249+
}
250+
239251
/**
240252
* Do a request using the specified {@link Request} object.
241253
*
242254
* @return the result of the request
243255
*/
244-
public abstract Response execute(@Nonnull Request request)
256+
protected abstract Response execute(@Nonnull Request request)
245257
throws IOException, ReCaptchaException;
246258
}

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

+1-1
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

+1-1
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

+1-1
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)