Merge pull request #1143 from petlyh/peertube-non-urls

Avoid PeerTube accepting non-URLs
This commit is contained in:
Tobi 2023-12-29 12:47:30 +01:00 committed by GitHub
commit 038ebdedc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 92 additions and 10 deletions

View File

@ -5,6 +5,8 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List; import java.util.List;
public final class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
@ -51,8 +53,13 @@ public final class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFact
@Override @Override
public boolean onAcceptUrl(final String url) { public boolean onAcceptUrl(final String url) {
try {
new URL(url);
return url.contains("/accounts/") || url.contains("/a/") return url.contains("/accounts/") || url.contains("/a/")
|| url.contains("/video-channels/") || url.contains("/c/"); || url.contains("/video-channels/") || url.contains("/c/");
} catch (final MalformedURLException e) {
return false;
}
} }
/** /**

View File

@ -5,6 +5,8 @@ import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List; import java.util.List;
public final class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory {
@ -27,7 +29,12 @@ public final class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFac
@Override @Override
public boolean onAcceptUrl(final String url) throws FoundAdException { public boolean onAcceptUrl(final String url) throws FoundAdException {
try {
new URL(url);
return url.contains("/videos/") || url.contains("/w/"); return url.contains("/videos/") || url.contains("/w/");
} catch (final MalformedURLException e) {
return false;
}
} }
@Override @Override

View File

@ -6,6 +6,8 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List; import java.util.List;
public final class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory { public final class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
@ -52,9 +54,10 @@ public final class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFac
@Override @Override
public boolean onAcceptUrl(final String url) { public boolean onAcceptUrl(final String url) {
try { try {
new URL(url);
getId(url); getId(url);
return true; return true;
} catch (final ParsingException e) { } catch (final ParsingException | MalformedURLException e) {
return false; return false;
} }
} }

View File

@ -6,6 +6,9 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser;
import java.net.MalformedURLException;
import java.net.URL;
public final class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory { public final class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {
private static final PeertubeStreamLinkHandlerFactory INSTANCE private static final PeertubeStreamLinkHandlerFactory INSTANCE
@ -47,9 +50,10 @@ public final class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {
return false; return false;
} }
try { try {
new URL(url);
getId(url); getId(url);
return true; return true;
} catch (final ParsingException e) { } catch (final ParsingException | MalformedURLException e) {
return false; return false;
} }
} }

View File

@ -4,6 +4,8 @@ import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -69,8 +71,13 @@ public final class PeertubeTrendingLinkHandlerFactory extends ListLinkHandlerFac
@Override @Override
public boolean onAcceptUrl(final String url) { public boolean onAcceptUrl(final String url) {
try {
new URL(url);
return url.contains("/videos?") || url.contains("/videos/trending") return url.contains("/videos?") || url.contains("/videos/trending")
|| url.contains("/videos/most-liked") || url.contains("/videos/recently-added") || url.contains("/videos/most-liked") || url.contains("/videos/recently-added")
|| url.contains("/videos/local"); || url.contains("/videos/local");
} catch (final MalformedURLException e) {
return false;
}
} }
} }

View File

@ -7,9 +7,9 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory; import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube; import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;
/** /**
* Test for {@link PeertubeChannelLinkHandlerFactory} * Test for {@link PeertubeChannelLinkHandlerFactory}
@ -33,6 +33,8 @@ public class PeertubeChannelLinkHandlerFactoryTest {
assertTrue(linkHandler.acceptUrl("https://peertube.stream/video-channels/kranti_channel@videos.squat.net/videos")); assertTrue(linkHandler.acceptUrl("https://peertube.stream/video-channels/kranti_channel@videos.squat.net/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/c/kranti_channel@videos.squat.net/videos")); assertTrue(linkHandler.acceptUrl("https://peertube.stream/c/kranti_channel@videos.squat.net/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa")); assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa"));
testDoNotAcceptNonURLs(linkHandler);
} }
@Test @Test

View File

@ -9,6 +9,7 @@ import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeCommen
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;
/** /**
* Test for {@link PeertubeCommentsLinkHandlerFactory} * Test for {@link PeertubeCommentsLinkHandlerFactory}
@ -31,6 +32,8 @@ public class PeertubeCommentsLinkHandlerFactoryTest {
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d")); assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/9c9de5e8-0a1e-484a-b099-e80766180a6d"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/9c9de5e8-0a1e-484a-b099-e80766180a6d")); assertTrue(linkHandler.acceptUrl("https://framatube.org/w/9c9de5e8-0a1e-484a-b099-e80766180a6d"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d/comment-threads?start=0&count=10&sort=-createdAt")); assertTrue(linkHandler.acceptUrl("https://framatube.org/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d/comment-threads?start=0&count=10&sort=-createdAt"));
testDoNotAcceptNonURLs(linkHandler);
} }
@Test @Test

View File

@ -0,0 +1,40 @@
package org.schabi.newpipe.extractor.services.peertube;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class PeertubeLinkHandlerFactoryTestHelper {
public static void testDoNotAcceptNonURLs(LinkHandlerFactory linkHandler)
throws ParsingException {
assertFalse(linkHandler.acceptUrl("orchestr/a/"));
assertFalse(linkHandler.acceptUrl("/a/"));
assertFalse(linkHandler.acceptUrl("something/c/"));
assertFalse(linkHandler.acceptUrl("/c/"));
assertFalse(linkHandler.acceptUrl("videos/"));
assertFalse(linkHandler.acceptUrl("I-hate-videos/"));
assertFalse(linkHandler.acceptUrl("/w/"));
assertFalse(linkHandler.acceptUrl("ksmg/w/"));
assertFalse(linkHandler.acceptUrl("a reandom search query"));
assertFalse(linkHandler.acceptUrl("test 230 "));
assertFalse(linkHandler.acceptUrl("986513"));
}
public static void testDoNotAcceptNonURLs(ListLinkHandlerFactory linkHandler)
throws ParsingException {
assertFalse(linkHandler.acceptUrl("orchestr/a/"));
assertFalse(linkHandler.acceptUrl("/a/"));
assertFalse(linkHandler.acceptUrl("something/c/"));
assertFalse(linkHandler.acceptUrl("/c/"));
assertFalse(linkHandler.acceptUrl("videos/"));
assertFalse(linkHandler.acceptUrl("I-hate-videos/"));
assertFalse(linkHandler.acceptUrl("/w/"));
assertFalse(linkHandler.acceptUrl("ksmg/w/"));
assertFalse(linkHandler.acceptUrl("a reandom search query"));
assertFalse(linkHandler.acceptUrl("test 230 "));
assertFalse(linkHandler.acceptUrl("986513"));
}
}

View File

@ -10,6 +10,7 @@ import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubePlayli
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;
/** /**
* Test for {@link PeertubePlaylistLinkHandlerFactory} * Test for {@link PeertubePlaylistLinkHandlerFactory}
@ -33,6 +34,8 @@ public class PeertubePlaylistLinkHandlerFactoryTest {
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/dacdc4ef-5160-4846-9b70-a655880da667")); assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/dacdc4ef-5160-4846-9b70-a655880da667"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7")); assertTrue(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));
assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7")); assertTrue(linkHandler.acceptUrl("https://framatube.org/w/p/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));
testDoNotAcceptNonURLs(linkHandler);
} }
@Test @Test

View File

@ -9,6 +9,7 @@ import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeStream
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube; import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;
/** /**
* Test for {@link PeertubeStreamLinkHandlerFactory} * Test for {@link PeertubeStreamLinkHandlerFactory}
@ -71,5 +72,7 @@ public class PeertubeStreamLinkHandlerFactoryTest {
// make sure playlists aren't accepted // make sure playlists aren't accepted
assertFalse(linkHandler.acceptUrl("https://framatube.org/w/p/dacdc4ef-5160-4846-9b70-a655880da667")); assertFalse(linkHandler.acceptUrl("https://framatube.org/w/p/dacdc4ef-5160-4846-9b70-a655880da667"));
assertFalse(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/dacdc4ef-5160-4846-9b70-a655880da667")); assertFalse(linkHandler.acceptUrl("https://framatube.org/videos/watch/playlist/dacdc4ef-5160-4846-9b70-a655880da667"));
PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs(linkHandler);
} }
} }

View File

@ -11,6 +11,7 @@ import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeTrendi
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube; import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;
/** /**
* Test for {@link PeertubeTrendingLinkHandlerFactory} * Test for {@link PeertubeTrendingLinkHandlerFactory}
@ -57,5 +58,7 @@ public class PeertubeTrendingLinkHandlerFactoryTest {
assertTrue(LinkHandlerFactory.acceptUrl("https://peertube.mastodon.host/videos/local")); assertTrue(LinkHandlerFactory.acceptUrl("https://peertube.mastodon.host/videos/local"));
assertTrue(LinkHandlerFactory.acceptUrl("https://peertube.mastodon.host/videos/local?adsf=fjaj#fhe")); assertTrue(LinkHandlerFactory.acceptUrl("https://peertube.mastodon.host/videos/local?adsf=fjaj#fhe"));
PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs(LinkHandlerFactory);
} }
} }