fix(server): limit administrator mail to their own accounts

Require an explicit mail permission for administrators and enforce recipient ownership on the server.
Keep owner access to all players and align the console controls and five-language permission descriptions.
This commit is contained in:
Flechazo committed 2026-10-24 01:23:13 +08:00
1 parent 1239a8dadc
commit 9db4e8356c
9 files changed
+37 -16

No files matched your search

+5
View File
@@ -614,6 +614,11 @@ func (c *Console) sendMail(w http.ResponseWriter, r *http.Request, current sessi
if !ok {
return
}
// Only the owner chooses a recipient; an administrator mails their own account.
if current.Role != RoleOwner && player.AccountID != current.AccountID {
fail(w, http.StatusForbidden, "forbidden", "only the owner can mail other players")
return
}
var request struct {
ElementType uint64 `json:"element_type"`
ID uint64 `json:"id"`
+18 -5
View File
@@ -155,11 +155,24 @@ func TestConsoleTiersFollowOwnerAppointmentsAndPermissions(t *testing.T) {
}
gift := map[string]any{"element_type": 4, "id": 0, "count": 5, "title": "t", "body": "b"}
if status, _ := friend.do(http.MethodPost, "/admin/api/players/other/mail", gift, nil); status != http.StatusCreated {
t.Fatalf("default administrator mail: %d", status)
if status, _ := friend.do(http.MethodPost, "/admin/api/players/friend/mail", gift, nil); status != http.StatusForbidden {
t.Fatalf("default administrator mailed without an explicit grant: %d", status)
}
if raw, err := os.ReadFile(spool); err != nil || !strings.Contains(string(raw), `"account_id": "other"`) {
t.Fatalf("spool recipient: %s, %v", raw, err)
mailer := map[string]any{"administrator": true, "permissions": []string{PermissionMail}}
if status, _ := owner.do(http.MethodPut, "/admin/api/players/friend/administrator", mailer, nil); status != http.StatusOK {
t.Fatalf("grant mail: %d", status)
}
if status, _ := friend.do(http.MethodPost, "/admin/api/players/other/mail", gift, nil); status != http.StatusForbidden {
t.Fatalf("administrator mailed another player: %d", status)
}
if status, _ := friend.do(http.MethodPost, "/admin/api/players/friend/mail", gift, nil); status != http.StatusCreated {
t.Fatalf("administrator mailed own account: %d", status)
}
if status, _ := owner.do(http.MethodPost, "/admin/api/players/other/mail", gift, nil); status != http.StatusCreated {
t.Fatalf("owner mailed another player: %d", status)
}
if raw, err := os.ReadFile(spool); err != nil || !strings.Contains(string(raw), `"account_id": "friend"`) || !strings.Contains(string(raw), `"account_id": "other"`) {
t.Fatalf("spool recipients: %s, %v", raw, err)
}
if _, value := friend.do(http.MethodGet, "/admin/api/players", nil, nil); len(value["players"].([]any)) != 3 {
t.Fatalf("default administrator player list: %v", value)
@@ -190,7 +203,7 @@ func TestConsoleTiersFollowOwnerAppointmentsAndPermissions(t *testing.T) {
if status, _ := friend.do(http.MethodPut, "/admin/api/players/other/inventory", map[string]bool{"unlimited": true}, nil); status != http.StatusOK {
t.Fatalf("granted inventory permission: %d", status)
}
if status, _ := friend.do(http.MethodPost, "/admin/api/players/other/mail", gift, nil); status != http.StatusForbidden {
if status, _ := friend.do(http.MethodPost, "/admin/api/players/friend/mail", gift, nil); status != http.StatusForbidden {
t.Fatalf("replaced permissions kept mail: %d", status)
}
status, document := friend.do(http.MethodGet, "/admin/api/config/game", nil, nil)
+4 -3
View File
@@ -21,8 +21,9 @@ func configPermission(document, section, mode string) string {
return "config." + document + "." + section + "." + mode
}
// DefaultAdministratorPermissions are granted when an owner appoints an administrator.
var DefaultAdministratorPermissions = []string{PermissionMail, PermissionPlayers}
// DefaultAdministratorPermissions are granted when an owner appoints an
// administrator. Mail is never implied: the owner grants it explicitly.
var DefaultAdministratorPermissions = []string{PermissionPlayers}
type Ability struct {
Key string `json:"key"`
@@ -34,7 +35,7 @@ type Ability struct {
var abilities = []Ability{
{Key: PermissionPlayers, Label: "查看玩家", Help: "查看玩家列表、UID 与背包设置。", Default: true},
{Key: PermissionMail, Label: "邮件发放", Help: "给任意玩家寄送物品和货币。", Requires: []string{PermissionPlayers}, Default: true},
{Key: PermissionMail, Label: "邮件发放", Help: "给自己的账号寄送物品和货币;只有服主能给其他玩家寄送。", Requires: []string{PermissionPlayers}},
{Key: PermissionInventory, Label: "调整背包容量", Help: "开关任意玩家的无限背包容量。", Requires: []string{PermissionPlayers}},
}
+5 -3
View File
@@ -489,8 +489,10 @@ const mailTool = (() => {
try { all.forEach(x => { x._h = searchText(x); }); buildCategories(); rendered(await render(), renderPass);
} catch (error) { if (error !== staleRender) throw error; }
}
// Only the owner picks a recipient; an administrator mails their own account.
const mailable = p => p.active && (isOwner() || (state.session.player && p.account_id === state.session.player.account_id));
async function playerOptions() {
const usable = state.players.filter(p => p.active);
const usable = state.players.filter(mailable);
$('player-selector').hidden = usable.length < 2;
const selected = $('player').value, language = locale;
const options = await Promise.all(usable.map(async p => new Option(p.owner ? await dynamicText('console.account.as_owner', playerLabel(p)) : playerLabel(p), p.account_id)));
@@ -504,7 +506,7 @@ const mailTool = (() => {
await paintParcel(); paintPeek(); await paintLog();
const usable = await playerOptions();
const self = state.session.player && state.session.player.account_id;
const initial = accountID || (target && usable.some(p => p.account_id === target.account_id) ? target.account_id : null) || (usable.some(p => p.account_id === self) ? self : usable[0] && usable[0].account_id);
const initial = (usable.some(p => p.account_id === accountID) ? accountID : null) || (target && usable.some(p => p.account_id === target.account_id) ? target.account_id : null) || (usable.some(p => p.account_id === self) ? self : usable[0] && usable[0].account_id);
if (initial) $('player').value = initial;
if (loaded) await relabelCatalog(); else if (!all.length) await loadCatalog();
await checkQuantity(); await select(initial || '');
@@ -552,7 +554,7 @@ const roster = (() => {
el('td', {class: 'c'}, toggle(p.unlimited_inventory, !p.active || !can('players.inventory'), async event => await setInventory(p, event.target), t('console.inventory.unlimited'))),
el('td', {class: 'act'},
owner && p.administrator ? el('button', {type: 'button', class: 'linkish', onclick: async () => await editPermissions(p)}, t('console.roster.permissions')) : null,
p.active && can('mail.send') ? el('button', {type: 'button', class: 'linkish', onclick: () => { location.hash = '#mail'; setTimeout(async () => await mailTool.open(p.account_id)); }}, t('console.roster.mail')) : null));
can('mail.send') && mailable(p) ? el('button', {type: 'button', class: 'linkish', onclick: () => { location.hash = '#mail'; setTimeout(async () => await mailTool.open(p.account_id)); }}, t('console.roster.mail')) : null));
})), renderPass));
if (!rows.length) $('roster').replaceChildren(el('tr', {}, el('td', {colspan: 6, class: 'muted'}, t(state.players.length ? 'console.roster.no_match' : 'console.roster.empty'))));
@@ -171,7 +171,7 @@
"console.permission.players.view.help": "See the player list, UIDs and inventory settings.",
"console.permission.players.view.short": "View players",
"console.permission.mail.send.label": "Send mail",
"console.permission.mail.send.help": "Send items and currency to any player.",
"console.permission.mail.send.help": "Send items and currency to your own account; only the owner can mail other players.",
"console.permission.mail.send.short": "Mail",
"console.permission.players.inventory.label": "Adjust inventory capacity",
"console.permission.players.inventory.help": "Turn unlimited inventory capacity on or off for any player.",
@@ -171,7 +171,7 @@
"console.permission.players.view.help": "プレイヤー一覧、UID、所持品の設定を確認できます。",
"console.permission.players.view.short": "プレイヤー閲覧",
"console.permission.mail.send.label": "メールを送信",
"console.permission.mail.send.help": "任意のプレイヤーにアイテムや通貨を送れます。",
"console.permission.mail.send.help": "自分のアカウントにアイテムや通貨を送れます。ほかのプレイヤーへの送付はサーバー主のみです。",
"console.permission.mail.send.short": "メール",
"console.permission.players.inventory.label": "所持品の容量を変更",
"console.permission.players.inventory.help": "任意のプレイヤーの所持品の容量制限を切り替えられます。",
@@ -171,7 +171,7 @@
"console.permission.players.view.help": "플레이어 목록, UID 및 인벤토리 설정을 확인합니다.",
"console.permission.players.view.short": "플레이어 조회",
"console.permission.mail.send.label": "메일 발송",
"console.permission.mail.send.help": "모든 플레이어에게 아이템과 재화를 보냅니다.",
"console.permission.mail.send.help": "자신의 계정에 아이템과 재화를 보냅니다. 다른 플레이어에게는 서버 주인만 보낼 수 있습니다.",
"console.permission.mail.send.short": "메일",
"console.permission.players.inventory.label": "인벤토리 용량 조정",
"console.permission.players.inventory.help": "모든 플레이어의 인벤토리 무제한을 켜거나 끕니다.",
@@ -171,7 +171,7 @@
"console.permission.players.view.help": "查看玩家列表、UID 与背包设置。",
"console.permission.players.view.short": "查看玩家",
"console.permission.mail.send.label": "邮件发放",
"console.permission.mail.send.help": "给任意玩家寄送物品和货币。",
"console.permission.mail.send.help": "给自己的账号寄送物品和货币;只有服主能给其他玩家寄送。",
"console.permission.mail.send.short": "寄件",
"console.permission.players.inventory.label": "调整背包容量",
"console.permission.players.inventory.help": "开关任意玩家的无限背包容量。",
@@ -171,7 +171,7 @@
"console.permission.players.view.help": "檢視玩家列表、UID 與背包設定。",
"console.permission.players.view.short": "檢視玩家",
"console.permission.mail.send.label": "郵件發放",
"console.permission.mail.send.help": "給任意玩家寄送物品和貨幣。",
"console.permission.mail.send.help": "給自己的帳號寄送物品和貨幣;只有服主能給其他玩家寄送。",
"console.permission.mail.send.short": "寄件",
"console.permission.players.inventory.label": "調整背包容量",
"console.permission.players.inventory.help": "開關任意玩家的無限背包容量。",