Skip to content

Commit 4158c0b

Browse files
kakkokari-gtyihsyuilo
authored andcommitted
feat: サーバー初期設定時に初期パスワードを要求できるように (misskey-dev#14626)
* feat: サーバー初期設定時専用の初期パスワードを設定できるように * 無いのに入力された場合もエラーにする * 🎨 * 🎨 * cypress-devcontainerにもpassを設定(テストが失敗するため) * [ci skip] 🎨 * ✌️ * test: please revert this commit before merge * Revert "test: please revert this commit before merge" This reverts commit 66b2b48. * Update locales/ja-JP.yml Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com> * build assets * Update Changelog * fix condition * fix condition * add comment * change error code * 他のエラーコードと合わせる * Update CHANGELOG.md --------- Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
1 parent d0b0498 commit 4158c0b

File tree

8 files changed

+96
-5
lines changed

8 files changed

+96
-5
lines changed

.config/cypress-devcontainer.yml

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
# Misskey configuration
33
#━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
44

5+
# ┌────────────────────────┐
6+
#───┘ Initial Setup Password └─────────────────────────────────────────────────────
7+
8+
# Password to initiate setting up admin account.
9+
# It will not be used after the initial setup is complete.
10+
#
11+
# Be sure to change this when you set up Misskey via the Internet.
12+
#
13+
# The provider of the service who sets up Misskey on behalf of the customer should
14+
# set this value to something unique when generating the Misskey config file,
15+
# and provide it to the customer.
16+
initialPassword: example_password_please_change_this_or_you_will_get_hacked
17+
518
# ┌─────┐
619
#───┘ URL └─────────────────────────────────────────────────────
720

.config/example.yml

+13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@
5959
#
6060
# publishTarballInsteadOfProvideRepositoryUrl: true
6161

62+
# ┌────────────────────────┐
63+
#───┘ Initial Setup Password └─────────────────────────────────────────────────────
64+
65+
# Password to initiate setting up admin account.
66+
# It will not be used after the initial setup is complete.
67+
#
68+
# Be sure to change this when you set up Misskey via the Internet.
69+
#
70+
# The provider of the service who sets up Misskey on behalf of the customer should
71+
# set this value to something unique when generating the Misskey config file,
72+
# and provide it to the customer.
73+
initialPassword: example_password_please_change_this_or_you_will_get_hacked
74+
6275
# ┌─────┐
6376
#───┘ URL └─────────────────────────────────────────────────────
6477

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
## Unreleased
22

3+
### Note
4+
- サーバー初期設定時に使用する初期パスワードを設定できるようになりました。今後Misskeyサーバーを新たに設置する際には、初回の起動前にコンフィグファイルの`initialPassword`を必ず変更してください。(すでに初期設定を完了しているサーバーについては、この変更に伴い対応する必要はありません)
5+
ホスティングサービスを運営している場合は、コンフィグファイルを構築する際に`initialPassword`をランダムな値に設定し、ユーザーに通知するようにしてください。
6+
37
### General
8+
- Feat: サーバー初期設定時に初期パスワードを設定できるように
49
- Enhance: セキュリティ向上のため、サインイン時もCAPTCHAを求めるようになりました
510
- Enhance: 依存関係の更新
611
- Enhance: l10nの更新

cypress/e2e/basic.cy.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('Before setup instance', () => {
2323

2424
cy.intercept('POST', '/api/admin/accounts/create').as('signup');
2525

26+
cy.get('[data-cy-admin-initial-password] input').type('example_password_please_change_this_or_you_will_get_hacked');
2627
cy.get('[data-cy-admin-username] input').type('admin');
2728
cy.get('[data-cy-admin-password] input').type('admin1234');
2829
cy.get('[data-cy-admin-ok]').click();

packages/backend/src/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ type Source = {
6565

6666
publishTarballInsteadOfProvideRepositoryUrl?: boolean;
6767

68+
initialPassword?: string;
69+
6870
proxy?: string;
6971
proxySmtp?: string;
7072
proxyBypassHosts?: string[];
@@ -179,6 +181,7 @@ export type Config = {
179181

180182
version: string;
181183
publishTarballInsteadOfProvideRepositoryUrl: boolean;
184+
initialPassword: string | undefined;
182185
host: string;
183186
hostname: string;
184187
scheme: string;
@@ -280,6 +283,7 @@ export function loadConfig(): Config {
280283
return {
281284
version,
282285
publishTarballInsteadOfProvideRepositoryUrl: !!config.publishTarballInsteadOfProvideRepositoryUrl,
286+
initialPassword: config.initialPassword,
283287
url: url.origin,
284288
port: config.port ?? parseInt(process.env.PORT ?? '3000', 10),
285289
socket: config.socket,

packages/backend/src/server/api/endpoints/admin/accounts/create.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@ import { UserEntityService } from '@/core/entities/UserEntityService.js';
1212
import { InstanceActorService } from '@/core/InstanceActorService.js';
1313
import { localUsernameSchema, passwordSchema } from '@/models/User.js';
1414
import { DI } from '@/di-symbols.js';
15+
import type { Config } from '@/config.js';
16+
import { ApiError } from '@/server/api/error.js';
1517
import { Packed } from '@/misc/json-schema.js';
1618

1719
export const meta = {
1820
tags: ['admin'],
1921

22+
errors: {
23+
accessDenied: {
24+
message: 'Access denied.',
25+
code: 'ACCESS_DENIED',
26+
id: '1fb7cb09-d46a-4fff-b8df-057708cce513',
27+
},
28+
29+
wrongInitialPassword: {
30+
message: 'Initial password is incorrect.',
31+
code: 'INCORRECT_INITIAL_PASSWORD',
32+
id: '97147c55-1ae1-4f6f-91d6-e1c3e0e76d62',
33+
},
34+
},
35+
2036
res: {
2137
type: 'object',
2238
optional: false, nullable: false,
@@ -35,13 +51,17 @@ export const paramDef = {
3551
properties: {
3652
username: localUsernameSchema,
3753
password: passwordSchema,
54+
initialPassword: { type: 'string', nullable: true },
3855
},
3956
required: ['username', 'password'],
4057
} as const;
4158

4259
@Injectable()
4360
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
4461
constructor(
62+
@Inject(DI.config)
63+
private config: Config,
64+
4565
@Inject(DI.usersRepository)
4666
private usersRepository: UsersRepository,
4767

@@ -52,7 +72,23 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
5272
super(meta, paramDef, async (ps, _me, token) => {
5373
const me = _me ? await this.usersRepository.findOneByOrFail({ id: _me.id }) : null;
5474
const realUsers = await this.instanceActorService.realLocalUsersPresent();
55-
if ((realUsers && !me?.isRoot) || token !== null) throw new Error('access denied');
75+
76+
if (!realUsers && me == null && token == null) {
77+
// 初回セットアップの場合
78+
if (this.config.initialPassword != null) {
79+
// 初期パスワードが設定されている場合
80+
if (ps.initialPassword !== this.config.initialPassword) {
81+
// 初期パスワードが違う場合
82+
throw new ApiError(meta.errors.wrongInitialPassword);
83+
}
84+
} else if (ps.initialPassword != null && ps.initialPassword.trim() !== '') {
85+
// 初期パスワードが設定されていないのに初期パスワードが入力された場合
86+
throw new ApiError(meta.errors.wrongInitialPassword);
87+
}
88+
} else if ((realUsers && !me?.isRoot) || token !== null) {
89+
// 初回セットアップではなく、管理者でない場合 or 外部トークンを使用している場合
90+
throw new ApiError(meta.errors.accessDenied);
91+
}
5692

5793
const { account, secret } = await this.signupService.signup({
5894
username: ps.username,

packages/frontend/src/pages/welcome.setup.vue

+22-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ SPDX-License-Identifier: AGPL-3.0-only
1414
</div>
1515
<div class="_gaps_m" style="padding: 32px;">
1616
<div>{{ i18n.ts.intro }}</div>
17+
<MkInput v-model="initialPassword" type="password" data-cy-admin-initial-password>
18+
<template #label>{{ i18n.ts.initialPasswordForSetup }} <div v-tooltip:dialog="i18n.ts.initialPasswordForSetupDescription" class="_button _help"><i class="ti ti-help-circle"></i></div></template>
19+
<template #prefix><i class="ti ti-lock"></i></template>
20+
</MkInput>
1721
<MkInput v-model="username" pattern="^[a-zA-Z0-9_]{1,20}$" :spellcheck="false" required data-cy-admin-username>
1822
<template #label>{{ i18n.ts.username }}</template>
1923
<template #prefix>@</template>
@@ -47,6 +51,7 @@ import MkAnimBg from '@/components/MkAnimBg.vue';
4751

4852
const username = ref('');
4953
const password = ref('');
54+
const initialPassword = ref('');
5055
const submitting = ref(false);
5156

5257
function submit() {
@@ -56,14 +61,27 @@ function submit() {
5661
misskeyApi('admin/accounts/create', {
5762
username: username.value,
5863
password: password.value,
64+
initialPassword: initialPassword.value === '' ? null : initialPassword.value,
5965
}).then(res => {
6066
return login(res.token);
61-
}).catch(() => {
67+
}).catch((err) => {
6268
submitting.value = false;
6369

70+
let title = i18n.ts.somethingHappened;
71+
let text = err.message + '\n' + err.id;
72+
73+
if (err.code === 'ACCESS_DENIED') {
74+
title = i18n.ts.permissionDeniedError;
75+
text = i18n.ts.operationForbidden;
76+
} else if (err.code === 'INCORRECT_INITIAL_PASSWORD') {
77+
title = i18n.ts.permissionDeniedError;
78+
text = i18n.ts.incorrectPassword;
79+
}
80+
6481
os.alert({
6582
type: 'error',
66-
text: i18n.ts.somethingHappened,
83+
title,
84+
text,
6785
});
6886
});
6987
}
@@ -74,8 +92,8 @@ function submit() {
7492
min-height: 100svh;
7593
padding: 32px 32px 64px 32px;
7694
box-sizing: border-box;
77-
display: grid;
78-
place-content: center;
95+
display: grid;
96+
place-content: center;
7997
}
8098

8199
.form {

packages/misskey-js/src/autogen/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5819,6 +5819,7 @@ export type operations = {
58195819
'application/json': {
58205820
username: string;
58215821
password: string;
5822+
initialPassword?: string | null;
58225823
};
58235824
};
58245825
};

0 commit comments

Comments
 (0)