Skip to content

Commit

Permalink
Merge pull request #1143 from petlyh/peertube-non-urls
Browse files Browse the repository at this point in the history
Avoid PeerTube accepting non-URLs
  • Loading branch information
TobiGr authored Dec 29, 2023
2 parents 1e93b1d + 61d237d commit 038ebde
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public final class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFactory {
Expand Down Expand Up @@ -51,8 +53,13 @@ public String getUrl(final String id,

@Override
public boolean onAcceptUrl(final String url) {
return url.contains("/accounts/") || url.contains("/a/")
|| url.contains("/video-channels/") || url.contains("/c/");
try {
new URL(url);
return url.contains("/accounts/") || url.contains("/a/")
|| url.contains("/video-channels/") || url.contains("/c/");
} catch (final MalformedURLException e) {
return false;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public final class PeertubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory {
Expand All @@ -27,7 +29,12 @@ public String getId(final String url) throws ParsingException, UnsupportedOperat

@Override
public boolean onAcceptUrl(final String url) throws FoundAdException {
return url.contains("/videos/") || url.contains("/w/");
try {
new URL(url);
return url.contains("/videos/") || url.contains("/w/");
} catch (final MalformedURLException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public final class PeertubePlaylistLinkHandlerFactory extends ListLinkHandlerFactory {
Expand Down Expand Up @@ -52,9 +54,10 @@ public String getId(final String url) throws ParsingException, UnsupportedOperat
@Override
public boolean onAcceptUrl(final String url) {
try {
new URL(url);
getId(url);
return true;
} catch (final ParsingException e) {
} catch (final ParsingException | MalformedURLException e) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser;

import java.net.MalformedURLException;
import java.net.URL;

public final class PeertubeStreamLinkHandlerFactory extends LinkHandlerFactory {

private static final PeertubeStreamLinkHandlerFactory INSTANCE
Expand Down Expand Up @@ -47,9 +50,10 @@ public boolean onAcceptUrl(final String url) throws FoundAdException {
return false;
}
try {
new URL(url);
getId(url);
return true;
} catch (final ParsingException e) {
} catch (final ParsingException | MalformedURLException e) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -69,8 +71,13 @@ public String getId(final String url) throws ParsingException, UnsupportedOperat

@Override
public boolean onAcceptUrl(final String url) {
return url.contains("/videos?") || url.contains("/videos/trending")
|| url.contains("/videos/most-liked") || url.contains("/videos/recently-added")
|| url.contains("/videos/local");
try {
new URL(url);
return url.contains("/videos?") || url.contains("/videos/trending")
|| url.contains("/videos/most-liked") || url.contains("/videos/recently-added")
|| url.contains("/videos/local");
} catch (final MalformedURLException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;

/**
* Test for {@link PeertubeChannelLinkHandlerFactory}
Expand All @@ -33,6 +33,8 @@ public void acceptUrlTest() throws ParsingException {
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/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa"));

testDoNotAcceptNonURLs(linkHandler);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;

/**
* Test for {@link PeertubeCommentsLinkHandlerFactory}
Expand All @@ -31,6 +32,8 @@ public void acceptUrlTest() throws ParsingException {
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/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180a6d/comment-threads?start=0&count=10&sort=-createdAt"));

testDoNotAcceptNonURLs(linkHandler);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;

/**
* Test for {@link PeertubePlaylistLinkHandlerFactory}
Expand All @@ -33,6 +34,8 @@ void acceptUrlTest() throws ParsingException {
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/w/p/96b0ee2b-a5a7-4794-8769-58d8ccb79ab7"));

testDoNotAcceptNonURLs(linkHandler);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;

/**
* Test for {@link PeertubeStreamLinkHandlerFactory}
Expand Down Expand Up @@ -71,5 +72,7 @@ public void testAcceptUrl() throws ParsingException {
// 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/videos/watch/playlist/dacdc4ef-5160-4846-9b70-a655880da667"));

PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs(linkHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs;

/**
* Test for {@link PeertubeTrendingLinkHandlerFactory}
Expand Down Expand Up @@ -57,5 +58,7 @@ public void acceptUrl() throws ParsingException {

assertTrue(LinkHandlerFactory.acceptUrl("https://peertube.mastodon.host/videos/local"));
assertTrue(LinkHandlerFactory.acceptUrl("https://peertube.mastodon.host/videos/local?adsf=fjaj#fhe"));

PeertubeLinkHandlerFactoryTestHelper.testDoNotAcceptNonURLs(LinkHandlerFactory);
}
}

0 comments on commit 038ebde

Please sign in to comment.