Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ public function delete(ContentsServiceInterface $service)
{
if ($this->request->is(['post', 'put', 'delete'])) {
$id = $this->request->getData('content.id');
// 数字以外の ID は型変換できず例外になるため、存在しないページとして扱う
if (!is_scalar($id) || !ctype_digit((string)$id)) $this->notFound();
/* @var \BaserCore\Model\Entity\Content $content */
$content = $service->get($id);
if ($service->deleteRecursive($id)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ public function add(PermissionGroupsAdminServiceInterface $service, int $userGro
$this->BcMessage->setError(__d('baser_core', 'データベース処理中にエラーが発生しました。') . $e->getMessage());
}
}
// prefix が無い場合は getNew() が TypeError になるため、一覧の初期値と同じ Admin にする
$prefix = $this->getRequest()->getQuery('prefix');
if (!is_string($prefix) || $prefix === '') $prefix = 'Admin';
$this->set($service->getViewVarsForForm(
$userGroupId,
$entity?? $service->getNew($this->getRequest()->getQuery('prefix'))
$entity?? $service->getNew($prefix)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public function add(
*/
public function edit(
PermissionsAdminServiceInterface $service,
$userGroupId,
$permissionId,
$permissionGroupId = null
int $userGroupId,
int $permissionId,
?int $permissionGroupId = null
) {
$permission = $service->get($permissionId);
if ($this->request->is(['patch', 'post', 'put'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function index(PluginsServiceInterface $service)
*/
public function install(PluginsAdminServiceInterface $service, string $name)
{
// 存在しないプラグイン名では設定を取得できず TypeError になるため、存在しないページとして扱う
if (!BcUtil::getPluginPath($name)) $this->notFound();
$this->set($service->getViewVarsForInstall($this->Plugins->getPluginConfig($name)));
if ($service->getInstallStatusMessage($name) || !$this->request->is(['put', 'post'])) {
return;
Expand Down Expand Up @@ -320,7 +322,8 @@ public function reset_db(PluginsServiceInterface $service, UsersServiceInterface
{
if (!$this->request->is('put')) {
$this->BcMessage->setError(__d('baser_core', '無効な処理です。'));
return;
// reset_db のテンプレートは存在せず MissingTemplateException になるため、一覧へ戻す
return $this->redirect(['action' => 'index']);
}
$plugin = $service->getByName($this->request->getData('name'));
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function add(
* @unitTest
* @note(value="インストーラーを実装してからテーマの保有するプラグインをインストールする処理を追加する")
*/
public function edit(SitesAdminServiceInterface $service, $id)
public function edit(SitesAdminServiceInterface $service, int $id)
{
if (!$id) {
$this->notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function add(UserGroupsServiceInterface $service)
* @noTodo
* @unitTest
*/
public function edit(UserGroupsServiceInterface $service, UsersServiceInterface $usersService, $id)
public function edit(UserGroupsServiceInterface $service, UsersServiceInterface $usersService, int $id)
{
$userGroup = $service->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/baser-core/src/Controller/Admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function login_code(UsersServiceInterface $usersService, TwoFactorAuthent
* @unitTest
* @noTodo
*/
public function login_agent(UsersServiceInterface $service, $id): ?Response
public function login_agent(UsersServiceInterface $service, int $id): ?Response
{
// 特権確認
if (BcUtil::isAdminUser() === false) {
Expand Down Expand Up @@ -352,7 +352,7 @@ public function add(UsersAdminServiceInterface $service)
* @noTodo
* @unitTest
*/
public function edit(UsersAdminServiceInterface $service, $id = null)
public function edit(UsersAdminServiceInterface $service, ?int $id = null)
{
if (!$id && empty($this->request->getData())) {
$this->BcMessage->setError(__d('baser_core', '無効なIDです。'));
Expand Down
6 changes: 4 additions & 2 deletions plugins/baser-core/src/Middleware/BcAdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ public function setCurrentSite($request): ServerRequestInterface
$queryCurrentSiteId = $request->getQuery('site_id');
$sitesTable = TableRegistry::getTableLocator()->get('BaserCore.Sites');
if ($queryCurrentSiteId) {
$query = $sitesTable->find()->where(['Sites.id' => $queryCurrentSiteId]);
if(!$query->count()) {
// 数字以外は型変換できず例外になるため、存在しないサイトとして扱う
$query = (is_scalar($queryCurrentSiteId) && ctype_digit((string)$queryCurrentSiteId))?
$sitesTable->find()->where(['Sites.id' => $queryCurrentSiteId]) : null;
if(!$query || !$query->count()) {
$request = $request->withQueryParams(array_merge(
$request->getQueryParams(),
['site_id' => $defaultSiteId]
Expand Down
10 changes: 5 additions & 5 deletions plugins/baser-core/src/Service/ContentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ public function setConditions(Query $query, array $queryParams): Query
if(!empty($params['type!'])) $conditions['Contents.type IS NOT'] = $params['type!'];
// author_id
if(!empty($params['parent_id'])) $conditions['Contents.parent_id'] = $params['parent_id'];
// author_id
if(!empty($params['author_id'])) $conditions['Contents.author_id'] = $params['author_id'];
// author_id(数字以外は型変換できず例外になるため条件にしない)
if(!empty($params['author_id']) && is_scalar($params['author_id']) && ctype_digit((string)$params['author_id'])) $conditions['Contents.author_id'] = $params['author_id'];
// site_id
if(!empty($params['site_id'])) $conditions['Contents.site_id'] = $params['site_id'];
// title
Expand All @@ -271,10 +271,10 @@ public function setConditions(Query $query, array $queryParams): Query
}

// folder_id
if (!is_null($params['folder_id']) && $params['folder_id']) {
if (!is_null($params['folder_id']) && $params['folder_id'] && is_scalar($params['folder_id']) && ctype_digit((string)$params['folder_id'])) {
$folder = $this->Contents->find()->select(['lft', 'rght'])
->where(['Contents.id' => $queryParams['folder_id']])->first();
$conditions[] = ['Contents.rght <' => $folder->rght, 'Contents.lft >' => $folder->lft];
->where(['Contents.id' => $params['folder_id']])->first();
if ($folder) $conditions[] = ['Contents.rght <' => $folder->rght, 'Contents.lft >' => $folder->lft];
}

return $query->where($conditions);
Expand Down
3 changes: 2 additions & 1 deletion plugins/baser-core/src/Service/DblogsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ public function getIndex(array $queryParams = []): Query
if (!empty($queryParams['message'])) {
$query->where(['message LIKE' => '%' . $queryParams['message'] . '%']);
}
if (!empty($queryParams['user_id'])) {
// 数値で指定する条件に数字以外が渡されると型変換できず例外になるため、条件にしない
if (!empty($queryParams['user_id']) && is_scalar($queryParams['user_id']) && ctype_digit((string)$queryParams['user_id'])) {
$query->where(['user_id' => $queryParams['user_id']]);
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/baser-core/src/Service/UsersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ private function createIndexConditions(Query $query, array $params): Query
'real_name' => null
], $params);

// 数値で指定する条件に数字以外が渡されると型変換できず例外になるため、条件にしない
if (!is_null($params['user_group_id']) && (!is_scalar($params['user_group_id']) || !ctype_digit((string)$params['user_group_id']))) {
$params['user_group_id'] = null;
}
if (!is_null($params['user_group_id'])) {
$query->matching('UserGroups', function($q) use ($params) {
return $q->where(['UserGroups.id' => $params['user_group_id']]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,17 @@ public function testAdmin_empty()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
}

/**
* test delete
* 数字以外の ID は 404
*/
public function testDelete_non_numeric_id()
{
$this->enableSecurityToken();
$this->enableCsrfToken();
$this->post('/baser/admin/baser-core/contents/delete', ['content' => ['id' => "6'"]]);
$this->assertResponseCode(404);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,15 @@ public function test_rebuild_by_user_group()
'1'
]);
}

/**
* test add
* prefix を指定しない場合は Admin として表示する
*/
public function testAdd_without_prefix()
{
$this->get('/baser/admin/baser-core/permission_groups/add/1');
$this->assertResponseOk();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ public function testPublish()
$this->assertTrue($permission->status);
}

/**
* test edit
* 数字以外の ID は 404
*/
public function testEdit_non_numeric_id()
{
$this->get('/baser/admin/baser-core/permissions/edit/' . urlencode("2'") . '/1');
$this->assertResponseCode(404);
}

/**
* test delete / copy / publish / unpublish
* 数字以外の ID は 404
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,27 @@ public function test_get_core_update()
rename(ROOT . DS . 'composer.bak.lock', ROOT . DS . 'composer.lock');
}

/**
* test install
* 存在しないプラグイン名は 404
*/
public function testInstall_not_exists()
{
$this->get('/baser/admin/baser-core/plugins/install/' . urlencode("BcBlog'"));
$this->assertResponseCode(404);
}

/**
* test reset_db
* PUT 以外は一覧へ戻す
*/
public function testReset_db_not_put()
{
$this->enableSecurityToken();
$this->enableCsrfToken();
$this->post('/baser/admin/baser-core/plugins/reset_db', ['name' => 'BcBlog']);
$this->assertRedirect(['plugin' => 'BaserCore', 'prefix' => 'Admin', 'controller' => 'Plugins', 'action' => 'index']);
$this->assertFlashMessage('無効な処理です。');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,18 @@ public function testBack_agent()
$this->assertRedirect('/baser/admin');
}

/**
* test edit / login_agent
* 数字以外の ID は 404
*/
public function testEdit_non_numeric_id()
{
$this->get('/baser/admin/baser-core/users/edit/' . urlencode("1'"));
$this->assertResponseCode(404);
$this->get('/baser/admin/baser-core/users/login_agent/' . urlencode("1'"));
$this->assertResponseCode(404);
}

/**
* test delete
* 数字以外の ID は 404
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function testProcess(): void
$request = $this->getRequest('/baser/admin/?site_id=1');
$request = $this->execPrivateMethod($this->BcAdminMiddleware, 'setCurrentSite', [$request]);
$this->assertEquals(1, $request->getAttribute('currentSite')->id);

// 数字以外のサイトIDは既定のサイトにする
$request = $this->getRequest('/baser/admin/?site_id=' . urlencode("1'"));
$request = $this->execPrivateMethod($this->BcAdminMiddleware, 'setCurrentSite', [$request]);
$this->assertEquals(1, $request->getAttribute('currentSite')->id);
}

}
14 changes: 14 additions & 0 deletions plugins/baser-core/tests/TestCase/Service/ContentsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1291,4 +1291,18 @@ public function testGetCrumbs()
$rs = $this->ContentsService->getCrumbs(5);
$this->assertCount(3, $rs);
}

/**
* test getIndex
* 数字以外の作成者ID・フォルダーID、存在しないフォルダーIDは条件にしない
*/
public function testGetIndex_invalid_filter(): void
{
$this->loadFixtureScenario(ContentsScenario::class);
$count = $this->ContentsService->getIndex([])->count();
$this->assertEquals($count, $this->ContentsService->getIndex(['author_id' => "1'"])->count());
$this->assertEquals($count, $this->ContentsService->getIndex(['folder_id' => "1'"])->count());
$this->assertEquals($count, $this->ContentsService->getIndex(['folder_id' => 99999])->count());
}

}
12 changes: 12 additions & 0 deletions plugins/baser-core/tests/TestCase/Service/DblogsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@ public function test_delete()
$this->assertEquals($count - 1, $this->DblogsService->getIndex()->count());
}

/**
* test getIndex
* 数字以外のユーザーIDは条件にしない
*/
public function testGetIndex_non_numeric_user_id()
{
$this->assertEquals(
$this->DblogsService->getIndex([])->count(),
$this->DblogsService->getIndex(['user_id' => "1'"])->count()
);
}

}
14 changes: 14 additions & 0 deletions plugins/baser-core/tests/TestCase/Service/UsersServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,18 @@ public function test_createIndexConditions()
$this->assertTrue($found, 'バインド値 %山田% が存在すること');
}

/**
* test createIndexConditions
* 数字以外のユーザーグループIDは条件にしない
*/
public function test_createIndexConditions_non_numeric_user_group_id()
{
$query = $this->Users->Users->find();
$result = $this->execPrivateMethod($this->Users, 'createIndexConditions', [$query, ['user_group_id' => "1'"]]);
$this->assertStringNotContainsString('INNER JOIN user_groups UserGroups', $result->sql());
$this->assertEquals($this->Users->getIndex([])->count(), $this->Users->getIndex(['user_group_id' => "1'"])->count());
// 配列形式も条件にしない
$this->assertEquals($this->Users->getIndex([])->count(), $this->Users->getIndex(['user_group_id' => [1]])->count());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function index(
* @noTodo
* @unitTest
*/
public function add(BlogCategoriesAdminServiceInterface $service, string $blogContentId)
public function add(BlogCategoriesAdminServiceInterface $service, int $blogContentId)
{
if ($this->request->is('post')) {
// EVENT BlogCategories.beforeAdd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function add(BlogTagsServiceInterface $service)
* @noTodo
* @unitTest
*/
public function edit(BlogTagsServiceInterface $service, $id)
public function edit(BlogTagsServiceInterface $service, int $id)
{
$blogTag = $service->get($id);
if ($this->request->is(['patch', 'post', 'put'])) {
Expand Down
4 changes: 4 additions & 0 deletions plugins/bc-blog/src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ public function archives(
}
}
}
// 年月日として解釈できない値は、カレンダーウィジェットの mktime() で TypeError になるため、存在しないページとして扱う
if ((isset($pass[1]) && $year === '') || (isset($pass[2]) && $month === '') || (isset($pass[3]) && $day === '')) {
$this->notFound();
}
$this->set($service->getViewVarsForArchivesByDate(
$this->paginate($blogPostsService->getIndexByDate($year, $month, $day, array_merge([
'status' => 'publish',
Expand Down
4 changes: 4 additions & 0 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ protected function createIndexConditions(Query $query, array $params)
foreach($params as $key => $value) {
if ($value === '') unset($params[$key]);
}
// 数値で指定する条件に数字以外が渡されると型変換できず例外になるため、条件にしない
foreach(['user_id', 'blog_tag_id', 'blog_category_id'] as $key) {
if (isset($params[$key]) && (!is_scalar($params[$key]) || !ctype_digit((string)$params[$key]))) unset($params[$key]);
}
if (empty($params)) return $query;

$params = array_merge([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ public function test_archives()
$vars = $this->_controller->viewBuilder()->getVars();
$this->assertEquals('yearly', $vars['blogArchiveType']);
$this->assertEquals('post1', $vars['posts']->toArray()[0]->name);
// 年月日として解釈できない値は 404
$this->get('/news/archives/date/2023/' . urlencode("1'"));
$this->assertResponseCode(404);
$this->get('/news/archives/date/2023/1/' . urlencode("1'"));
$this->assertResponseCode(404);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,27 @@ public function test_getUrl()
$this->assertEquals('https://localhost/archives/1', $rs);
}

/**
* test getIndex
* 数字以外のユーザーID・タグID・カテゴリIDは条件にしない
*/
public function testGetIndex_non_numeric_id()
{
BlogPostFactory::make(['id' => 1, 'blog_content_id' => 1, 'user_id' => 1, 'blog_category_id' => 1])->persist();
BlogPostFactory::make(['id' => 2, 'blog_content_id' => 1, 'user_id' => 2, 'blog_category_id' => 2])->persist();
$this->assertEquals(2, $this->BlogPostsService->getIndex([
'user_id' => "1'",
'blog_tag_id' => "1'",
'blog_category_id' => "1'",
])->count());
// 配列形式も条件にしない
$this->assertEquals(2, $this->BlogPostsService->getIndex([
'user_id' => [1],
'blog_tag_id' => [1],
'blog_category_id' => [1],
])->count());
}

/**
* test filterBlogTagIds
*/
Expand Down
Loading
Loading