Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Aug 27, 2024
2 parents 6598028 + 4dd0c8c commit 638fb86
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 82 deletions.
2 changes: 1 addition & 1 deletion docker/web/aws-signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ end

local function get_hashed_canonical_request(timestamp, host, uri)
local digest = get_sha256_digest(ngx.var.request_body)
local canonical_request = ngx.var.request_method .. '\n'
local canonical_request = 'GET' .. '\n'
.. uri .. '\n'
.. '\n'
.. 'host:' .. host .. '\n'
Expand Down
2 changes: 1 addition & 1 deletion docker/web/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ init_by_lua_block {
function sign_aws_request()
-- The API token used should not allow writing, but
-- sanitize this anyway to stop an upstream error
if ngx.req.get_method() ~= 'GET' then
if ngx.req.get_method() ~= 'GET' and ngx.req.get_method() ~= 'HEAD' then
ngx.status = ngx.HTTP_UNAUTHORIZED
ngx.say('Unauthorized')
return ngx.exit(ngx.HTTP_UNAUTHORIZED)
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena/duplicate_reports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Philomena.DuplicateReports do
{intensities, aspect}
|> find_duplicates(dist: dist, aspect_dist: dist, limit: limit)
|> preload([:user, :intensity, [:sources, tags: :aliases]])
|> Repo.all()
|> Repo.paginate(page_size: 50)

{:ok, images}

Expand Down
43 changes: 9 additions & 34 deletions lib/philomena_proxy/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule PhilomenaProxy.Http do
body: body,
headers: [{:user_agent, @user_agent} | headers],
max_redirects: 1,
connect_options: connect_options(url),
connect_options: connect_options(),
inet6: true,
into: &stream_response_callback/2,
decode_body: false
Expand All @@ -93,39 +93,14 @@ defmodule PhilomenaProxy.Http do
|> Req.request()
end

defp connect_options(url) do
transport_opts =
case URI.parse(url) do
%{scheme: "https"} ->
# SSL defaults validate SHA-1 on root certificates but this is unnecessary because many
# many roots are still signed with SHA-1 and it isn't relevant for security. Relax to
# allow validation of SHA-1, even though this creates a less secure client.
# https://github.com/erlang/otp/issues/8601
[
transport_opts: [
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
],
signature_algs_cert: :ssl.signature_algs(:default, :"tlsv1.3") ++ [sha: :rsa]
]
]

_ ->
# Do not pass any options for non-HTTPS schemes. Finch will raise badarg if the above
# options are passed.
[]
end

proxy_opts =
case Application.get_env(:philomena, :proxy_host) do
nil ->
[]

url ->
[proxy: proxy_opts(URI.parse(url))]
end

transport_opts ++ proxy_opts
defp connect_options do
case Application.get_env(:philomena, :proxy_host) do
nil ->
[]

proxy_url ->
[proxy: proxy_opts(URI.parse(proxy_url))]
end
end

defp proxy_opts(%{host: host, port: port, scheme: "https"}),
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena_web/controllers/admin/advert_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule PhilomenaWeb.Admin.AdvertController do
|> put_flash(:info, "Advert was successfully created.")
|> redirect(to: ~p"/admin/adverts")

{:error, :advert, changeset, _changes} ->
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/philomena_web/controllers/admin/user/erase_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule PhilomenaWeb.Admin.User.EraseController do
persisted: true,
preload: [:roles]

plug :prevent_deleting_nonexistent_users
plug :prevent_deleting_privileged_users
plug :prevent_deleting_verified_users

Expand All @@ -35,6 +36,17 @@ defmodule PhilomenaWeb.Admin.User.EraseController do
end
end

defp prevent_deleting_nonexistent_users(conn, _opts) do
if is_nil(conn.assigns.user) do
conn
|> put_flash(:error, "Couldn't find that username. Was it already erased?")
|> redirect(to: ~p"/admin/users")
|> Plug.Conn.halt()
else
conn
end
end

defp prevent_deleting_privileged_users(conn, _opts) do
if conn.assigns.user.role != "user" do
conn
Expand Down
8 changes: 6 additions & 2 deletions lib/philomena_web/controllers/profile_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ defmodule PhilomenaWeb.ProfileController do
preload(Image, [:sources, tags: :aliases]),
preload(Image, [:sources, tags: :aliases]),
preload(Image, [:sources, tags: :aliases]),
preload(Comment, user: [awards: :badge], image: [:sources, tags: :aliases]),
preload(Post, user: [awards: :badge], topic: :forum)
preload(Comment, [
:deleted_by,
user: [awards: :badge],
image: [:sources, tags: :aliases]
]),
preload(Post, [:deleted_by, user: [awards: :badge], topic: :forum])
]
)

Expand Down
23 changes: 20 additions & 3 deletions lib/philomena_web/controllers/search/reverse_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,32 @@ defmodule PhilomenaWeb.Search.ReverseController do
case DuplicateReports.execute_search_query(image_params) do
{:ok, images} ->
changeset = DuplicateReports.change_search_query(%SearchQuery{})
render(conn, "index.html", title: "Reverse Search", images: images, changeset: changeset)

render(conn, "index.html",
title: "Reverse Search",
layout_class: "layout--wide",
images: images,
changeset: changeset
)

{:error, changeset} ->
render(conn, "index.html", title: "Reverse Search", images: nil, changeset: changeset)
render(conn, "index.html",
title: "Reverse Search",
layout_class: "layout--wide",
images: nil,
changeset: changeset
)
end
end

def create(conn, _params) do
changeset = DuplicateReports.change_search_query(%SearchQuery{})
render(conn, "index.html", title: "Reverse Search", images: nil, changeset: changeset)

render(conn, "index.html",
title: "Reverse Search",
layout_class: "layout--wide",
images: nil,
changeset: changeset
)
end
end
61 changes: 24 additions & 37 deletions lib/philomena_web/templates/search/reverse/index.html.slime
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
h1 Reverse Search

= form_for @changeset, ~p"/search/reverse", [multipart: true, as: :image], fn f ->
p
' Basic image similarity search. Finds uploaded images similar to the one
' provided based on simple intensities and uses the median frame of
' animations; very low contrast images (such as sketches) will produce
' poor results and, regardless of contrast, results may include seemingly
' random images that look very different.
.walloftext
p
' Basic image similarity search. Finds uploaded images similar to the one
' provided based on simple intensities and uses the median frame of
' animations; very low contrast images (such as sketches) will produce
' poor results and, regardless of contrast, results may include seemingly
' random images that look very different.

.image-other
#js-image-upload-previews
Expand Down Expand Up @@ -40,42 +41,28 @@ h1 Reverse Search

= cond do
- is_nil(@images) ->
/ Don't render anything.

- Enum.any?(@images) ->
h2 Results

table
tr
th  
th Image
th  

= for match <- @images do
tr
th
h3 = link "##{match.id}", to: ~p"/images/#{match}"
p
= if image_has_sources(match) do
span.source_url
= link "Source", to: image_first_source(match)
- else
' Unknown source
.block#imagelist-container
section.block__header.page__header.flex
span.block__header__title.page__title.hide-mobile
' Search by uploaded image

th
= render PhilomenaWeb.ImageView, "_image_container.html", image: match, size: :thumb, conn: @conn
.block__content.js-resizable-media-container
= for image <- @images do
= render PhilomenaWeb.ImageView, "_image_box.html", image: image, link: ~p"/images/#{image}", size: :thumb, conn: @conn

th
h3
= match.image_width
| x
=> match.image_height
' -
=> round(match.image_size / 1024)
' KiB
.block__header.block__header--light.page__header.flex
span.block__header__title.page__info
= render PhilomenaWeb.PaginationView, "_pagination_info.html", page: @images

= render PhilomenaWeb.TagView, "_tag_list.html", tags: Tag.display_order(match.tags), conn: @conn
.flex__right.page__options
a href="/settings/edit" title="Display Settings"
i.fa.fa-cog
span.hide-mobile.hide-limited-desktop<>
' Display Settings

- true ->
h2 Results
p
' We couldn't find any images matching this in our image database.
' No images found!
2 changes: 0 additions & 2 deletions lib/philomena_web/views/search/reverse_view.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
defmodule PhilomenaWeb.Search.ReverseView do
use PhilomenaWeb, :view

alias Philomena.Tags.Tag
end

0 comments on commit 638fb86

Please sign in to comment.