Skip to content

Commit 3fef73a

Browse files
committed
Fix wrong domains not allowed
1 parent 6407c42 commit 3fef73a

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

lib/bookmark/archives.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,16 @@ defmodule Bookmark.Archives do
210210
end
211211

212212
defp check_nsfw_domain(url) do
213+
domain = Utils.get_domain(url)
214+
213215
blocked_domains =
214216
:bookmark
215217
|> :code.priv_dir()
216218
|> Path.join("/static/blocked_domains.txt")
217219
|> File.read!()
218220
|> String.split("\n", trim: true)
219221

220-
blocked_domains
221-
|> Enum.find(&String.contains?(url, &1))
222+
Enum.find(blocked_domains, fn blocked_domain -> blocked_domain == domain end)
222223
end
223224

224225
defp archivebox_url() do

lib/bookmark/utils.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,23 @@ defmodule Bookmark.Utils do
55
|> Map.put(:query, nil)
66
|> URI.to_string()
77
end
8+
9+
# Extracts the domain from a url
10+
# Example: www.sub1.sub2.sub3.domain.com/path -> domain.com
11+
def get_domain(url) do
12+
case URI.parse(url) do
13+
%{authority: nil} ->
14+
url
15+
16+
%{authority: host} ->
17+
# sub1.sub2.sub3.domain.com -> 3
18+
subdomains = (host |> String.split(".") |> length()) - 2
19+
if subdomains >= 1 do
20+
subdomains = String.split(host, ".", parts: subdomains + 1)
21+
List.last(subdomains)
22+
else
23+
host
24+
end
25+
end
26+
end
827
end

test/bookmark/bookmark_context_test.exs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@ defmodule Bookmark.ArchivesTest do
1414

1515
@invalid_attrs %{name: nil}
1616

17+
#********************************** Get Archives *********************************#
18+
1719
test "list_archives/0 returns all archives" do
1820
archive = archive_fixture()
1921
[archive_from_list] = Archives.list_archives()
2022
assert archive_from_list |> Bookmark.Repo.preload(:user) == archive
2123
end
2224

23-
test "create_archive/1 with valid data creates a archive" do
24-
valid_attrs = %{name: "some name"}
25-
26-
assert {:ok, %Archive{} = archive} = Archives.create_archive(valid_attrs, nil)
27-
assert archive.name == "some name"
28-
end
29-
30-
test "create_archive/1 with invalid data returns error changeset" do
31-
assert_raise(MatchError, fn -> Archives.create_archive(@invalid_attrs, nil) end)
32-
end
3325

3426
test "get_archives_by_user/1 returns all archives from the user" do
3527
# Create 2 users
@@ -59,6 +51,20 @@ defmodule Bookmark.ArchivesTest do
5951
Bookmark.Archives.get_archives_by_user(user2) |> Enum.map(fn a -> Bookmark.Repo.preload(a, :user) end)
6052
end
6153

54+
#********************************** Create Archives *********************************#
55+
56+
test "create_archive/1 with valid data creates a archive" do
57+
valid_attrs = %{name: "some name"}
58+
59+
assert {:ok, %Archive{} = archive} = Archives.create_archive(valid_attrs, nil)
60+
assert archive.name == "some name"
61+
end
62+
63+
test "create_archive/1 with invalid data returns error changeset" do
64+
assert_raise(MatchError, fn -> Archives.create_archive(@invalid_attrs, nil) end)
65+
end
66+
67+
6268
test "archive_url/2 with valid data creates archives" do
6369
# Mocked functions
6470
Mimic.expect(Req, :post, fn _url, _opts -> archivebox_response("archive/some_id") end)
@@ -70,6 +76,21 @@ defmodule Bookmark.ArchivesTest do
7076
assert archive.title == "some_title"
7177
end
7278

79+
test "Avoid archive if url is blocked" do
80+
# Blocked URL
81+
assert {:error, :domain_not_allowed} = Archives.archive_url("https://es.pornhub.com/", nil)
82+
assert {:error, :domain_not_allowed} = Archives.archive_url("https://s1.s2.s3.s4.sex.com/", nil)
83+
84+
# Not blocked URL
85+
Mimic.expect(Req, :post, fn url, _opts -> archivebox_response("archive/_id") end)
86+
Mimic.expect(Archives, :get_title, fn _archive -> "_" end )
87+
88+
allowed_url = "https://whyisthisinteresting.substack.com/p/the-platinum-photography-edition"
89+
assert {:ok, %Archive{} = _archive} = Archives.archive_url(allowed_url, nil)
90+
end
91+
92+
#********************************** Bulk Archives *********************************#
93+
7394
test "bulk_archives/3 with valid data creates archives" do
7495
# Mocked functions
7596
Mimic.expect(Req, :post, 2, fn _url, _opts -> archivebox_response("archive/some_id") end)

0 commit comments

Comments
 (0)