Fix MCP permissions and repayments; restrict deletion to default admin
This commit is contained in:
1 parent
35bd2e1828
commit
794274d31b
21 files changed
+702
-52
No files matched your search
@@ -83,6 +83,7 @@ async function main() {
|
||||
'test/mcp.test.ts',
|
||||
'test/oauth-duration.test.ts',
|
||||
'test/database-migrations.test.ts',
|
||||
'test/connection-repayment-integration.test.ts',
|
||||
];
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
|
||||
@@ -135,7 +135,8 @@ export class AdminService {
|
||||
throw new ForbiddenException('管理员权限已变更');
|
||||
const target = await tx.user.findUnique({ where: { id } });
|
||||
if (!target) throw new NotFoundException('账号不存在');
|
||||
if (target.role !== 'admin') throw new BadRequestException('仅支持删除管理员账号');
|
||||
if (target.username !== 'admin' || target.role !== 'admin')
|
||||
throw new ForbiddenException('仅允许删除默认 admin 账号,其他账号不可删除');
|
||||
if (v.confirmationUsername !== target.username)
|
||||
throw new BadRequestException('确认账号名称不一致,请重新核对');
|
||||
if (
|
||||
|
||||
@@ -193,7 +193,7 @@ export class AgentCatalogue {
|
||||
),
|
||||
write(
|
||||
'movement_create',
|
||||
'执行 transfer 转账、borrow 借入、lend 借出、collect 收款、repay 还款。amount 为本金,received 为到账/债务本金,fee 可负表示优惠;原币十进制字符串,双边事务和余额检查。',
|
||||
'执行 transfer 转账、borrow 借入、lend 借出、collect 收款、repay 还款。amount 为本金,received 为到账/债务本金,fee 可负表示优惠;原币十进制字符串,双边事务和余额检查。repay 的 sourceId 必须是资产账户,targetId 可以是借入债务或信用卡等负债账户;负债账户超额还款保留为存款。',
|
||||
transferInput.safeExtend({ requestId: z.never().optional() }),
|
||||
(r, p) => transfers.create(r, p),
|
||||
),
|
||||
|
||||
@@ -2,6 +2,8 @@ import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
NotFoundException,
|
||||
Delete,
|
||||
Req,
|
||||
Param,
|
||||
@@ -11,9 +13,11 @@ import {
|
||||
ForbiddenException,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { compare } from 'bcryptjs';
|
||||
import { Response } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { loginInput } from '../user-access';
|
||||
import { Database } from '../database';
|
||||
import { AuthService, UserRequest } from '../auth';
|
||||
import { AgentOAuth, urls, scopeInput, oauthDays } from './oauth';
|
||||
@@ -96,6 +100,53 @@ export class AgentManagementController {
|
||||
return { id: v.grant.id, token: v.tokens.access_token, expiresAt: v.grant.expiresAt };
|
||||
});
|
||||
}
|
||||
@Patch('connections/:id') async permissions(
|
||||
@Req() r: UserRequest,
|
||||
@Param('id') id: string,
|
||||
@Body() raw: unknown,
|
||||
) {
|
||||
z.string().uuid().parse(id);
|
||||
const p = z
|
||||
.object({ scopes: scopeInput, password: loginInput.shape.password })
|
||||
.strict()
|
||||
.parse(raw);
|
||||
this.auth.limit(r);
|
||||
const verified = await this.db.user.findUniqueOrThrow({ where: { id: r.userId } });
|
||||
if (!(await compare(p.password, verified.passwordHash)))
|
||||
throw new ForbiddenException('密码错误');
|
||||
return this.db.atomic(async () => {
|
||||
await this.db.$queryRaw(Prisma.sql`SELECT id FROM User WHERE id=${r.userId} FOR UPDATE`);
|
||||
const user = await this.db.user.findUniqueOrThrow({ where: { id: r.userId } });
|
||||
if (user.banned || user.mustChangePassword || user.passwordHash !== verified.passwordHash)
|
||||
throw new ForbiddenException('账号状态已变化,请重新登录');
|
||||
if (
|
||||
user.role === 'readonly' &&
|
||||
p.scopes.some((s) => ['draft', 'write', 'hidden_write'].includes(s))
|
||||
)
|
||||
throw new ForbiddenException('只读账号只能授予查询权限');
|
||||
// Refresh and edits lock the same grant before reading its permissions.
|
||||
await this.db.$queryRaw(
|
||||
Prisma.sql`SELECT id FROM AgentGrant WHERE id=${id} AND userId=${r.userId} FOR UPDATE`,
|
||||
);
|
||||
const grant = await this.db.agentGrant.findFirst({ where: { id, userId: r.userId } });
|
||||
const expiry = grant?.clientId ? grant.refreshExpiresAt : grant?.expiresAt;
|
||||
if (!grant || grant.revokedAt || (expiry && expiry <= new Date()))
|
||||
throw new NotFoundException('有效连接不存在');
|
||||
const previous = grant.scopes as string[];
|
||||
if (
|
||||
previous.length === p.scopes.length &&
|
||||
previous.every((s) => (p.scopes as string[]).includes(s))
|
||||
)
|
||||
return { ok: true };
|
||||
await this.db.agentGrant.update({ where: { id }, data: { scopes: p.scopes } });
|
||||
// Permission edits never execute old drafts under a newly granted privilege.
|
||||
await this.db.agentOperation.updateMany({
|
||||
where: { grantId: id, userId: r.userId, status: 'pending' },
|
||||
data: { status: 'cancelled', completedAt: new Date() },
|
||||
});
|
||||
return { ok: true };
|
||||
});
|
||||
}
|
||||
@Delete('connections/:id') async revoke(@Req() r: UserRequest, @Param('id') id: string) {
|
||||
z.string().uuid().parse(id);
|
||||
await this.db.agentGrant.updateMany({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { networkConfig } from '../network';
|
||||
import { Injectable, BadRequestException, ForbiddenException } from '@nestjs/common';
|
||||
import { randomBytes, randomUUID, createHash } from 'node:crypto';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { Response } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { Database } from '../database';
|
||||
@@ -324,6 +325,9 @@ export class AgentOAuth implements OAuthServerProvider {
|
||||
) {
|
||||
this.resource(resource);
|
||||
return this.db.atomic(async () => {
|
||||
await this.db.$queryRaw(
|
||||
Prisma.sql`SELECT id FROM AgentGrant WHERE refreshDigest=${digest(token)} FOR UPDATE`,
|
||||
);
|
||||
const row = await this.db.agentGrant.findUnique({ where: { refreshDigest: digest(token) } });
|
||||
if (
|
||||
!row ||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { scopeInput } from './mcp/oauth';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { metalConfig, metalHoldingInput } from './metals';
|
||||
@@ -38,6 +39,9 @@ export function setupOpenApi(app: INestApplication) {
|
||||
'PATCH /api/admin/users/{id}': adminUpdateInput,
|
||||
'DELETE /api/admin/users/{id}': adminDeleteInput,
|
||||
'PATCH /api/auth/credentials': credentialChange,
|
||||
'PATCH /api/agent/connections/{id}': z
|
||||
.object({ scopes: scopeInput, password: loginInput.shape.password })
|
||||
.strict(),
|
||||
'POST /api/auth/reveal': z.object({ password: credentials.shape.password.optional() }).strict(),
|
||||
'POST /api/positions': positionInput,
|
||||
'PATCH /api/positions/{id}': positionMeta,
|
||||
@@ -101,6 +105,12 @@ export function setupOpenApi(app: INestApplication) {
|
||||
description: '{ items, nextCursor, revealed };金额为绝对余额,before 为真实前序余额',
|
||||
};
|
||||
}
|
||||
if (method === 'patch' && path === '/api/agent/connections/{id}')
|
||||
operation.description =
|
||||
'验证当前密码后修改自己的有效 OAuth/PAT 连接权限;实际权限变更取消未确认草稿,不延长授权期限。只读用户不能授予写入。';
|
||||
if (['post', 'put'].includes(method) && path.startsWith('/api/transfers'))
|
||||
operation.description =
|
||||
'repay 从资产账户向借入债务或负债账户还款;负债账户超额还款保存为溢缴存款。amount/received 为原币本金,fee 负数表示优惠。同币种本金一致,双边金额与历史原子更新。';
|
||||
if (method === 'get' && path === '/api/trend') {
|
||||
operation.parameters = [
|
||||
...['from', 'to'].map((name) => ({
|
||||
|
||||
@@ -183,10 +183,12 @@ export async function executeMovement(
|
||||
(v.operation !== 'transfer' && source.side !== 'asset') ||
|
||||
(v.operation === 'transfer'
|
||||
? target.kind !== 'account'
|
||||
: target.kind !== 'debt' ||
|
||||
target.side !== (['borrow', 'repay'].includes(v.operation) ? 'liability' : 'asset'))
|
||||
: !(
|
||||
(target.kind === 'debt' || (v.operation === 'repay' && target.kind === 'account')) &&
|
||||
target.side === (['borrow', 'repay'].includes(v.operation) ? 'liability' : 'asset')
|
||||
))
|
||||
)
|
||||
throw new BadRequestException('请选择有效的资产账户和对应借入或借出债务');
|
||||
throw new BadRequestException('请选择资产账户及对应债务;还款也可选择负债账户');
|
||||
if (accounts.some((p) => p.revisions[0] && +p.revisions[0].effectiveDate > +when))
|
||||
throw new ConflictException('转账时间不能早于任一账户的最新余额记录,请以当前余额转账');
|
||||
if (source.currency === target.currency && !new Decimal(v.amount).eq(v.received))
|
||||
@@ -235,7 +237,7 @@ export async function executeMovement(
|
||||
notes: v.notes,
|
||||
},
|
||||
});
|
||||
if (v.operation !== 'transfer')
|
||||
if (v.operation !== 'transfer' && target.kind === 'debt')
|
||||
await tx.positionLink.upsert({
|
||||
where: { sourceId_targetId: { sourceId: target.id, targetId: source.id } },
|
||||
create: { sourceId: target.id, targetId: source.id },
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
import 'dotenv/config';
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { randomUUID, randomBytes } from 'node:crypto';
|
||||
import { hash } from 'bcryptjs';
|
||||
import { Database } from '../src/database';
|
||||
import { AgentOAuth, urls } from '../src/mcp/oauth';
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
||||
import { today } from '../src/validation';
|
||||
|
||||
test(
|
||||
'connection permission edits, OAuth refresh, account repayment drafts and protecting non-default user accounts',
|
||||
{
|
||||
skip: process.env.TEST_ISOLATED !== 'true',
|
||||
},
|
||||
async () => {
|
||||
const db = new Database(),
|
||||
oauth = new AgentOAuth(db);
|
||||
const ids: string[] = [],
|
||||
clients: Client[] = [];
|
||||
let clientId = '';
|
||||
const password = randomBytes(20).toString('hex');
|
||||
const base = process.env.TEST_API_URL!;
|
||||
async function web(cookie: string, path: string, method = 'GET', body?: unknown) {
|
||||
const r = await fetch(base + path, {
|
||||
method,
|
||||
headers: {
|
||||
Origin: process.env.WEB_ORIGIN!,
|
||||
Cookie: cookie,
|
||||
...(body ? { 'Content-Type': 'application/json' } : {}),
|
||||
},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
return {
|
||||
status: r.status,
|
||||
data: await r.json(),
|
||||
cookie: r.headers.get('set-cookie')?.split(';')[0] || cookie,
|
||||
};
|
||||
}
|
||||
async function user(role: 'admin' | 'user' | 'readonly') {
|
||||
const u = await db.user.create({
|
||||
data: {
|
||||
username: 'update_access_' + randomUUID(),
|
||||
role,
|
||||
passwordHash: await hash(password, 10),
|
||||
},
|
||||
});
|
||||
ids.push(u.id);
|
||||
const login = await web('', '/auth/login', 'POST', { username: u.username, password });
|
||||
assert.equal(login.status, 201);
|
||||
return { ...u, cookie: login.cookie };
|
||||
}
|
||||
async function connect(token: string) {
|
||||
const client = new Client({ name: 'Update regression', version: '1' });
|
||||
clients.push(client);
|
||||
await client.connect(
|
||||
new StreamableHTTPClientTransport(urls().resource, {
|
||||
requestInit: { headers: { Authorization: 'Bearer ' + token } },
|
||||
}),
|
||||
);
|
||||
assert.ok((await client.listTools()).tools.some((t) => t.name === 'movement_create'));
|
||||
return client;
|
||||
}
|
||||
async function tool(c: Client, name: string, args: any = {}) {
|
||||
const r: any = await c.callTool({ name, arguments: args });
|
||||
assert.ok(!r.isError, JSON.stringify(r));
|
||||
return r.structuredContent.data;
|
||||
}
|
||||
async function draft(c: Client, name: string, args: any) {
|
||||
return tool(c, name, {
|
||||
...args,
|
||||
expectedState: (await tool(c, 'state_get')).state,
|
||||
idempotencyKey: randomUUID(),
|
||||
});
|
||||
}
|
||||
const position = {
|
||||
kind: 'account',
|
||||
side: 'asset',
|
||||
name: 'payer',
|
||||
category: 'bank',
|
||||
currency: 'CNY',
|
||||
amount: '100',
|
||||
date: today(),
|
||||
notes: '',
|
||||
};
|
||||
try {
|
||||
const admin = await user('admin'),
|
||||
owner = await user('user'),
|
||||
readonly = await user('readonly');
|
||||
const pat = await web(owner.cookie, '/agent/tokens', 'POST', {
|
||||
name: 'draft grant',
|
||||
scopes: ['read', 'draft'],
|
||||
days: 1,
|
||||
password,
|
||||
});
|
||||
assert.equal(pat.status, 201);
|
||||
const sdk = await connect(pat.data.token);
|
||||
const pending = await draft(sdk, 'position_create', position);
|
||||
assert.equal(pending.status, 'pending');
|
||||
assert.equal(
|
||||
(
|
||||
await web(owner.cookie, '/agent/connections/' + pat.data.id, 'PATCH', {
|
||||
scopes: ['read', 'draft'],
|
||||
password,
|
||||
})
|
||||
).status,
|
||||
200,
|
||||
);
|
||||
assert.equal(
|
||||
(await tool(sdk, 'operation_get', { operationId: pending.operationId })).status,
|
||||
'pending',
|
||||
);
|
||||
|
||||
const path = '/agent/connections/' + pat.data.id;
|
||||
assert.equal(
|
||||
(await web(admin.cookie, path, 'PATCH', { scopes: ['read', 'write'], password })).status,
|
||||
404,
|
||||
);
|
||||
assert.equal(
|
||||
(await web(owner.cookie, path, 'PATCH', { scopes: ['read', 'write'], password: 'wrong' }))
|
||||
.status,
|
||||
403,
|
||||
);
|
||||
assert.equal(
|
||||
(await web(owner.cookie, path, 'PATCH', { scopes: ['read', 'draft', 'write'], password }))
|
||||
.status,
|
||||
400,
|
||||
);
|
||||
const before = await db.agentGrant.findUniqueOrThrow({ where: { id: pat.data.id } });
|
||||
assert.equal(
|
||||
(await web(owner.cookie, path, 'PATCH', { scopes: ['read', 'write'], password })).status,
|
||||
200,
|
||||
);
|
||||
assert.equal((await tool(sdk, 'connection_info')).permission, 'write');
|
||||
assert.equal(
|
||||
(await tool(sdk, 'operation_get', { operationId: pending.operationId })).status,
|
||||
'cancelled',
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await db.agentGrant.findUniqueOrThrow({ where: { id: before.id } })
|
||||
).expiresAt?.toISOString(),
|
||||
before.expiresAt?.toISOString(),
|
||||
);
|
||||
assert.equal(await db.position.count({ where: { userId: owner.id } }), 0);
|
||||
assert.equal(
|
||||
(await web(owner.cookie, path, 'PATCH', { scopes: ['read', 'draft'], password })).status,
|
||||
200,
|
||||
);
|
||||
|
||||
const source = await web(owner.cookie, '/positions', 'POST', position);
|
||||
const target = await web(owner.cookie, '/positions', 'POST', {
|
||||
...position,
|
||||
side: 'liability',
|
||||
name: 'credit',
|
||||
category: 'credit_card',
|
||||
amount: '80',
|
||||
});
|
||||
assert.equal(source.status, 201);
|
||||
assert.equal(target.status, 201);
|
||||
const repayment = {
|
||||
operation: 'repay',
|
||||
sourceId: source.data.id,
|
||||
targetId: target.data.id,
|
||||
amount: '50',
|
||||
received: '50',
|
||||
fee: '-2',
|
||||
date: today(),
|
||||
notes: '',
|
||||
};
|
||||
const pay = await draft(sdk, 'movement_create', repayment);
|
||||
assert.equal(pay.status, 'pending');
|
||||
assert.equal((await tool(sdk, 'position_get', { id: source.data.id })).amount, '100');
|
||||
const approved = await web(owner.cookie, '/agent/operations/confirm-batch', 'POST', {
|
||||
approve: true,
|
||||
operationIds: [pay.operationId],
|
||||
});
|
||||
assert.equal(approved.status, 201, JSON.stringify(approved.data));
|
||||
const applied = await tool(sdk, 'operation_get', { operationId: pay.operationId });
|
||||
assert.equal(applied.status, 'completed');
|
||||
assert.equal((await tool(sdk, 'position_get', { id: source.data.id })).amount, '52');
|
||||
assert.equal((await tool(sdk, 'position_get', { id: target.data.id })).amount, '30');
|
||||
const record = await db.transfer.findUniqueOrThrow({ where: { id: applied.result.id } });
|
||||
assert.equal(record.operation, 'repay');
|
||||
assert.equal(await db.positionLink.count({ where: { sourceId: target.data.id } }), 0);
|
||||
assert.equal(
|
||||
(
|
||||
await web(owner.cookie, '/transfers/' + record.id, 'PUT', {
|
||||
...repayment,
|
||||
amount: '90',
|
||||
received: '90',
|
||||
fee: '0',
|
||||
})
|
||||
).status,
|
||||
200,
|
||||
);
|
||||
assert.equal((await tool(sdk, 'position_get', { id: target.data.id })).amount, '-10');
|
||||
assert.equal((await web(owner.cookie, '/transfers/' + record.id, 'DELETE')).status, 200);
|
||||
assert.equal((await tool(sdk, 'position_get', { id: source.data.id })).amount, '100');
|
||||
assert.equal((await tool(sdk, 'position_get', { id: target.data.id })).amount, '80');
|
||||
assert.equal(
|
||||
(
|
||||
await web(owner.cookie, '/transfers', 'POST', {
|
||||
...repayment,
|
||||
sourceId: target.data.id,
|
||||
targetId: source.data.id,
|
||||
})
|
||||
).status,
|
||||
400,
|
||||
);
|
||||
assert.equal((await web(admin.cookie, '/transfers', 'POST', repayment)).status, 400);
|
||||
|
||||
const client = await oauth.clientsStore.registerClient({
|
||||
client_name: 'Permission regression',
|
||||
redirect_uris: ['http://127.0.0.1:47891/callback'],
|
||||
token_endpoint_auth_method: 'none',
|
||||
grant_types: ['authorization_code', 'refresh_token'],
|
||||
});
|
||||
clientId = client.client_id;
|
||||
const issued = await db.atomic(() =>
|
||||
oauth.issue(owner.id, 'OAuth regression', ['read'], 1 / 24, clientId, 7),
|
||||
);
|
||||
const oauthPath = '/agent/connections/' + issued.grant.id;
|
||||
const rights = ['read', 'write', 'hidden_read', 'hidden_write'];
|
||||
assert.equal(
|
||||
(await web(owner.cookie, oauthPath, 'PATCH', { scopes: rights, password })).status,
|
||||
200,
|
||||
);
|
||||
assert.deepEqual((await oauth.verifyAccessToken(issued.tokens.access_token)).scopes, rights);
|
||||
const refreshed = await oauth.exchangeRefreshToken(
|
||||
client,
|
||||
issued.tokens.refresh_token!,
|
||||
undefined,
|
||||
urls().resource,
|
||||
);
|
||||
assert.deepEqual((await oauth.verifyAccessToken(refreshed.access_token)).scopes, rights);
|
||||
assert.equal(
|
||||
(
|
||||
await db.agentGrant.findUniqueOrThrow({ where: { id: issued.grant.id } })
|
||||
).refreshExpiresAt?.toISOString(),
|
||||
issued.grant.refreshExpiresAt?.toISOString(),
|
||||
);
|
||||
await db.agentGrant.update({
|
||||
where: { id: issued.grant.id },
|
||||
data: { expiresAt: new Date(0) },
|
||||
});
|
||||
assert.equal(
|
||||
(await web(owner.cookie, oauthPath, 'PATCH', { scopes: ['read'], password })).status,
|
||||
200,
|
||||
);
|
||||
const renewed = await oauth.exchangeRefreshToken(
|
||||
client,
|
||||
refreshed.refresh_token!,
|
||||
undefined,
|
||||
urls().resource,
|
||||
);
|
||||
assert.deepEqual((await oauth.verifyAccessToken(renewed.access_token)).scopes, ['read']);
|
||||
|
||||
await assert.rejects(
|
||||
oauth.exchangeRefreshToken(
|
||||
client,
|
||||
renewed.refresh_token!,
|
||||
['read', 'write'],
|
||||
urls().resource,
|
||||
),
|
||||
);
|
||||
const concurrent = await Promise.all([
|
||||
web(owner.cookie, oauthPath, 'PATCH', { scopes: rights, password }),
|
||||
oauth.exchangeRefreshToken(client, renewed.refresh_token!, undefined, urls().resource),
|
||||
]);
|
||||
assert.equal(concurrent[0].status, 200);
|
||||
assert.deepEqual((await oauth.verifyAccessToken(concurrent[1].access_token)).scopes, rights);
|
||||
await db.agentGrant.update({
|
||||
where: { id: issued.grant.id },
|
||||
data: { refreshExpiresAt: new Date(0) },
|
||||
});
|
||||
assert.equal(
|
||||
(await web(owner.cookie, oauthPath, 'PATCH', { scopes: ['read'], password })).status,
|
||||
404,
|
||||
);
|
||||
const openapi = await web(owner.cookie, '/openapi.json');
|
||||
assert.ok(
|
||||
openapi.data.paths['/api/agent/connections/{id}'].patch.requestBody.content[
|
||||
'application/json'
|
||||
].schema.properties.scopes,
|
||||
);
|
||||
const ro = await web(readonly.cookie, '/agent/tokens', 'POST', {
|
||||
name: 'readonly',
|
||||
scopes: ['read'],
|
||||
days: 1,
|
||||
password,
|
||||
});
|
||||
assert.equal(ro.status, 201);
|
||||
assert.equal(
|
||||
(
|
||||
await web(readonly.cookie, '/agent/connections/' + ro.data.id, 'PATCH', {
|
||||
scopes: ['read', 'write'],
|
||||
password,
|
||||
})
|
||||
).status,
|
||||
403,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await web(readonly.cookie, '/agent/connections/' + ro.data.id, 'PATCH', {
|
||||
scopes: ['read', 'hidden_read'],
|
||||
password,
|
||||
})
|
||||
).status,
|
||||
200,
|
||||
);
|
||||
await web(owner.cookie, path, 'DELETE');
|
||||
assert.equal(
|
||||
(await web(owner.cookie, path, 'PATCH', { scopes: ['read'], password })).status,
|
||||
404,
|
||||
);
|
||||
await assert.rejects(oauth.verifyAccessToken(pat.data.token));
|
||||
|
||||
for (const targetUser of [owner, readonly, await user('admin')]) {
|
||||
const removePath = '/admin/users/' + targetUser.id;
|
||||
const body = { confirmationUsername: targetUser.username, currentPassword: password };
|
||||
assert.equal((await web(readonly.cookie, removePath, 'DELETE', body)).status, 403);
|
||||
assert.equal(
|
||||
(
|
||||
await web(admin.cookie, removePath, 'DELETE', {
|
||||
...body,
|
||||
confirmationUsername: 'wrong',
|
||||
})
|
||||
).status,
|
||||
403,
|
||||
);
|
||||
assert.equal(
|
||||
(await web(admin.cookie, removePath, 'DELETE', { ...body, currentPassword: 'wrong' }))
|
||||
.status,
|
||||
403,
|
||||
);
|
||||
assert.equal((await web(admin.cookie, removePath, 'DELETE', body)).status, 403);
|
||||
assert.ok(await db.user.findUnique({ where: { id: targetUser.id } }));
|
||||
if (targetUser.id === owner.id)
|
||||
assert.equal(await db.position.count({ where: { userId: targetUser.id } }), 2);
|
||||
if (targetUser.id === owner.id)
|
||||
assert.equal(await db.agentGrant.count({ where: { userId: targetUser.id } }), 2);
|
||||
assert.equal((await web(targetUser.cookie, '/auth/me')).status, 200);
|
||||
}
|
||||
assert.equal(
|
||||
(
|
||||
await web(admin.cookie, '/admin/users/' + admin.id, 'DELETE', {
|
||||
confirmationUsername: admin.username,
|
||||
currentPassword: password,
|
||||
})
|
||||
).status,
|
||||
403,
|
||||
);
|
||||
} finally {
|
||||
for (const c of clients) await c.close().catch(() => {});
|
||||
await db.user.deleteMany({ where: { id: { in: ids } } });
|
||||
if (clientId) await db.agentClient.deleteMany({ where: { id: clientId } });
|
||||
await db.$disconnect();
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -6,6 +6,8 @@ test('HTTP resources, web links and client redirects work beyond loopback in dev
|
||||
const before = { ...process.env };
|
||||
try {
|
||||
process.env.NODE_ENV = 'development';
|
||||
process.env.NETWORK_ALLOW_HTTP = 'true';
|
||||
process.env.NETWORK_ALLOW_HTTP_REDIRECTS = 'true';
|
||||
process.env.MCP_PUBLIC_URL = 'http://192.0.2.10:3100/mcp';
|
||||
process.env.MCP_WEB_URL = 'http://192.0.2.10:5173';
|
||||
assert.equal(urls().issuer.href, 'http://192.0.2.10:3100/');
|
||||
@@ -28,7 +30,13 @@ test('HTTP resources, web links and client redirects work beyond loopback in dev
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
for (const k of ['NODE_ENV', 'MCP_PUBLIC_URL', 'MCP_WEB_URL']) {
|
||||
for (const k of [
|
||||
'NODE_ENV',
|
||||
'MCP_PUBLIC_URL',
|
||||
'MCP_WEB_URL',
|
||||
'NETWORK_ALLOW_HTTP',
|
||||
'NETWORK_ALLOW_HTTP_REDIRECTS',
|
||||
]) {
|
||||
if (before[k] === undefined) delete process.env[k];
|
||||
else process.env[k] = before[k];
|
||||
}
|
||||
@@ -39,6 +47,8 @@ test('production requires HTTPS for configured endpoints and rejects non-loopbac
|
||||
const before = { ...process.env };
|
||||
try {
|
||||
process.env.NODE_ENV = 'production';
|
||||
process.env.NETWORK_ALLOW_HTTP = 'false';
|
||||
process.env.NETWORK_ALLOW_HTTP_REDIRECTS = 'false';
|
||||
process.env.MCP_PUBLIC_URL = 'http://192.0.2.10:3100/mcp';
|
||||
process.env.MCP_WEB_URL = 'https://worthpath.example';
|
||||
assert.throws(() => urls(), /HTTPS/);
|
||||
@@ -61,7 +71,13 @@ test('production requires HTTPS for configured endpoints and rejects non-loopbac
|
||||
token_endpoint_auth_method: 'none',
|
||||
});
|
||||
} finally {
|
||||
for (const k of ['NODE_ENV', 'MCP_PUBLIC_URL', 'MCP_WEB_URL']) {
|
||||
for (const k of [
|
||||
'NODE_ENV',
|
||||
'MCP_PUBLIC_URL',
|
||||
'MCP_WEB_URL',
|
||||
'NETWORK_ALLOW_HTTP',
|
||||
'NETWORK_ALLOW_HTTP_REDIRECTS',
|
||||
]) {
|
||||
if (before[k] === undefined) delete process.env[k];
|
||||
else process.env[k] = before[k];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user