fix(before_all): halt response after rendering disabled page error

The return from before_all did not stop the route handler from
executing. The trending/popular/hashtag routes still ran and
overwrote the 403 response. Now set a halted flag and use Kemal
halt in the before_all block to prevent route handler execution.

Error page now renders with the full Invidious theme (dark mode,
navbar, CSS) matching the rest of the UI.
This commit is contained in:
NorkzYT 2026-03-23 01:59:38 +00:00
parent 8cd5f0f652
commit 7ca2bbd768
2 changed files with 49 additions and 4 deletions

View File

@ -200,6 +200,12 @@ end
before_all do |env| before_all do |env|
Invidious::Routes::BeforeAll.handle(env) Invidious::Routes::BeforeAll.handle(env)
# If before_all flagged a halt (e.g. disabled page), stop the route handler.
# Use halt with the already-set status code to prevent the route handler from running.
if env.get?("halted")
halt env, status_code: env.response.status_code
end
end end
Invidious::Routing.register_all Invidious::Routing.register_all

View File

@ -164,13 +164,52 @@ module Invidious::Routes::BeforeAll
end end
if page_key && !CONFIG.page_enabled?(page_key) if page_key && !CONFIG.page_enabled?(page_key)
env.response.status_code = 403
env.set "halted", true
if path.starts_with?("/api/") if path.starts_with?("/api/")
error_message = {error: "Administrator has disabled this endpoint."}.to_json env.response.content_type = "application/json"
haltf env, 403, error_message env.response.print({error: "Administrator has disabled this endpoint."}.to_json)
else else
message = "#{page_key}_page_disabled" preferences = env.get("preferences").as(Preferences)
return error_template(403, message) locale = preferences.locale
dark_mode = preferences.dark_mode
theme_class = dark_mode.blank? ? "no" : dark_mode
error_message = translate(locale, "#{page_key}_page_disabled")
env.response.content_type = "text/html"
env.response.print <<-HTML
<!DOCTYPE html>
<html lang="#{locale}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Error - Invidious</title>
<link rel="stylesheet" href="/css/pure-min.css">
<link rel="stylesheet" href="/css/grids-responsive-min.css">
<link rel="stylesheet" href="/css/ionicons.min.css">
<link rel="stylesheet" href="/css/default.css">
</head>
<body class="#{theme_class}-theme">
<div class="pure-g">
<div class="pure-u-1 pure-u-xl-20-24" id="contents">
<div class="pure-g navbar h-box">
<div class="pure-u-1 pure-u-md-16-24">
<a href="/" class="index-link pure-menu-heading">Invidious</a>
</div>
</div>
<div class="h-box" style="margin-top: 2em;">
<p>#{error_message}</p>
<p><a href="/"> #{translate(locale, "Back")}</a></p>
</div>
</div>
</div>
</body>
</html>
HTML
end end
return
end end
end end
end end