-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bugfix] Improve OPML route security (#535)
* WIP - moved plugs; set up a new token-protected route plug * Added a route_token column to settings model * Hooked up token_protected_route plug to database * Hooked up new OPML route to UI; turned RSS and OPML feed buttons into links * Docs, tests * Added a note about the phoenix bug
- Loading branch information
1 parent
246ca3b
commit f51b219
Showing
12 changed files
with
295 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 9 additions & 11 deletions
20
lib/pinchflat_web/controllers/sources/source_html/actions_dropdown.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
lib/pinchflat_web/controllers/sources/source_html/index.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
defmodule PinchflatWeb.Plugs do | ||
@moduledoc """ | ||
Custom plugs for PinchflatWeb. | ||
""" | ||
|
||
use PinchflatWeb, :router | ||
alias Pinchflat.Settings | ||
|
||
@doc """ | ||
If the `expose_feed_endpoints` setting is true, this plug does nothing. Otherwise, it calls `basic_auth/2`. | ||
""" | ||
def maybe_basic_auth(conn, opts) do | ||
if Application.get_env(:pinchflat, :expose_feed_endpoints) do | ||
conn | ||
else | ||
basic_auth(conn, opts) | ||
end | ||
end | ||
|
||
@doc """ | ||
If the `basic_auth_username` and `basic_auth_password` settings are set, this plug calls `Plug.BasicAuth.basic_auth/3`. | ||
""" | ||
def basic_auth(conn, _opts) do | ||
username = Application.get_env(:pinchflat, :basic_auth_username) | ||
password = Application.get_env(:pinchflat, :basic_auth_password) | ||
|
||
if credential_set?(username) && credential_set?(password) do | ||
Plug.BasicAuth.basic_auth(conn, username: username, password: password, realm: "Pinchflat") | ||
else | ||
conn | ||
end | ||
end | ||
|
||
@doc """ | ||
Removes the `x-frame-options` header from the response to allow the page to be embedded in an iframe. | ||
""" | ||
def allow_iframe_embed(conn, _opts) do | ||
delete_resp_header(conn, "x-frame-options") | ||
end | ||
|
||
@doc """ | ||
If the `route_token` query parameter matches the `route_token` setting, this plug does nothing. | ||
Otherwise, it sends a 401 response. | ||
""" | ||
def token_protected_route(%{query_params: %{"route_token" => route_token}} = conn, _opts) do | ||
if Settings.get!(:route_token) == route_token do | ||
conn | ||
else | ||
send_unauthorized(conn) | ||
end | ||
end | ||
|
||
def token_protected_route(conn, _opts) do | ||
send_unauthorized(conn) | ||
end | ||
|
||
defp credential_set?(credential) do | ||
credential && credential != "" | ||
end | ||
|
||
defp send_unauthorized(conn) do | ||
conn | ||
|> send_resp(:unauthorized, "Unauthorized") | ||
|> halt() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions
11
priv/repo/migrations/20241230192618_add_route_token_to_settings.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Pinchflat.Repo.Migrations.AddRouteTokenToSettings do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:settings) do | ||
add :route_token, :string, null: false, default: "tmp-token" | ||
end | ||
|
||
execute "UPDATE settings SET route_token = gen_random_uuid();", "SELECT 1;" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.