Skip to content

Commit a6385bb

Browse files
committed
better code: centralize admin_net check
1 parent 314a5d7 commit a6385bb

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/adminApis.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ export const adminApis = {
177177

178178
for (const [k, was] of typedEntries(adminApis))
179179
(adminApis[k] as any) = ((params, ctx) => {
180-
if (!allowAdmin(ctx))
181-
return new ApiError(HTTP_FORBIDDEN)
182180
if (ctxAdminAccess(ctx))
183181
return was(params, ctx)
184182
const props = { possible: anyAccountCanLoginAdmin() }
@@ -193,8 +191,10 @@ export const favicon = defineConfig('favicon', '')
193191
export const title = defineConfig('title', "File server")
194192

195193
export function ctxAdminAccess(ctx: Koa.Context) {
196-
return !ctx.ips.length // we consider localhost_admin only if no proxy is being used
197-
&& localhostAdmin.get() && isLocalHost(ctx)
194+
if (preventAdminAccess(ctx))
195+
return false
196+
// for extra security, skip localhost_admin via proxy, even tho this prevents using it with local proxies, which is legit in principle
197+
return !ctx.ips.length && localhostAdmin.get() && isLocalHost(ctx)
198198
|| ctx.state.account && accountCanLoginAdmin(ctx.state.account)
199199
}
200200

@@ -213,6 +213,6 @@ export function anyAccountCanLoginAdmin() {
213213
return Boolean(_.find(accounts.get(), accountCanLoginAdmin))
214214
}
215215

216-
export function allowAdmin(ctx: Koa.Context) {
217-
return isLocalHost(ctx) || adminNet.compiled()(ctx.ip)
216+
export function preventAdminAccess(ctx: Koa.Context) {
217+
return !isLocalHost(ctx) && !adminNet.compiled()(ctx.ip)
218218
}

src/serveGuiAndSharedFiles.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Writable } from 'stream'
1313
import { serveFile, serveFileNode } from './serveFile'
1414
import { BUILD_TIMESTAMP, DEV, MIME_AUTO, VERSION } from './const'
1515
import { zipStreamFromFolder } from './zip'
16-
import { allowAdmin, favicon } from './adminApis'
16+
import { preventAdminAccess, favicon } from './adminApis'
1717
import { serveGuiFiles } from './serveGuiFiles'
1818
import mount from 'koa-mount'
1919
import { baseUrl } from './listen'
@@ -42,8 +42,7 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
4242
if (path.length === ADMIN_URI.length - 1 && ADMIN_URI.startsWith(path))
4343
return ctx.redirect(ctx.state.revProxyPath + ADMIN_URI)
4444
if (path.startsWith(ADMIN_URI))
45-
return allowAdmin(ctx) ? serveAdminPrefixed(ctx,next)
46-
: sendErrorPage(ctx, HTTP_FORBIDDEN)
45+
return preventAdminAccess(ctx) ? sendErrorPage(ctx, HTTP_FORBIDDEN) : serveAdminPrefixed(ctx, next)
4746
if (path.startsWith(ICONS_URI)) {
4847
const a = path.substring(ICONS_URI.length).split('/')
4948
const iconName = a.at(-1)

0 commit comments

Comments
 (0)