Skip to content

Commit

Permalink
fix(emqx_dashboard): return 404 for everything we don't know
Browse files Browse the repository at this point in the history
  • Loading branch information
sstrigler committed Jun 9, 2023
1 parent 219b424 commit 8c1334c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
3 changes: 1 addition & 2 deletions apps/emqx_dashboard/src/emqx_dashboard.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ start_listeners(Listeners) ->
{"/", cowboy_static, {priv_file, emqx_dashboard, "www/index.html"}},
{"/static/[...]", cowboy_static, {priv_dir, emqx_dashboard, "www/static"}},
{emqx_mgmt_api_status:path(), emqx_mgmt_api_status, []},
{emqx_dashboard_swagger:relative_uri("/[...]"), emqx_dashboard_bad_api, []},
{'_', cowboy_static, {priv_file, emqx_dashboard, "www/index.html"}}
{'_', emqx_dashboard_not_found, []}
],
BaseMinirest = #{
base_path => emqx_dashboard_swagger:base_path(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_dashboard_bad_api).
-module(emqx_dashboard_not_found).

-include_lib("emqx/include/logger.hrl").

Expand All @@ -23,10 +23,22 @@
init(Req0, State) ->
RedactedReq = emqx_utils:redact(Req0),
?SLOG(warning, #{msg => "unexpected_api_access", request => RedactedReq}),
CT = ct(cowboy_req:header(<<"accept">>, Req0, <<"text/html">>)),
Req = cowboy_req:reply(
404,
#{<<"content-type">> => <<"application/json">>},
<<"{\"code\": \"API_NOT_EXIST\", \"message\": \"Request Path Not Found\"}">>,
#{<<"content-type">> => CT},
ct_body(CT),
RedactedReq
),
{ok, Req, State}.

ct(<<"text/plain", _/binary>>) -> <<"text/plain">>;
ct(<<"application/json", _/binary>>) -> <<"application/json">>;
ct(_AnyOther) -> <<"text/html">>.

ct_body(<<"text/html">>) ->
<<"<html><head><title>404 - NOT FOUND</title></head><body><h1>404 - NOT FOUND</h1></body></html>">>;
ct_body(<<"text/plain">>) ->
<<"404 - NOT FOUND">>;
ct_body(<<"application/json">>) ->
<<"{\"code\": \"NOT_FOUND\", \"message\": \"Request Path Not Found\"}">>.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%% limitations under the License.
%%--------------------------------------------------------------------

-module(emqx_dashboard_bad_api_SUITE).
-module(emqx_dashboard_not_found_SUITE).

-compile(nowarn_export_all).
-compile(export_all).
Expand All @@ -23,7 +23,7 @@

-include_lib("eunit/include/eunit.hrl").

-define(SERVER, "http://127.0.0.1:18083/api/v5").
-define(SERVER, "http://127.0.0.1:18083/").

-import(emqx_mgmt_api_test_util, [request/2]).

Expand Down
1 change: 1 addition & 0 deletions changes/ce/fix-10996.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Default to 404 for any HTTP/API request, we don't know, rather than the dashboard's index.html.

0 comments on commit 8c1334c

Please sign in to comment.