diff --git a/app/AdminToolsBase.php b/app/AdminToolsBase.php new file mode 100644 index 00000000..13a31f38 --- /dev/null +++ b/app/AdminToolsBase.php @@ -0,0 +1,16 @@ +id); session('username', $username); - session('password', $password); + // Never retain the clear-text password in the session. The stored + // password hash also makes a session invalid as soon as the user + // changes their password. + session('auth_hash', $userinfo->password); + session('password', null); // 跳转到首页 $data = ['msg' => '验证成功', 'val' => 1, 'url' =>'/']; } else { @@ -64,7 +68,8 @@ class YanZheng session('onlineCategory', 'student'); session('user_id', $userinfo->id); session('username', $username); - session('password', $password); + session('auth_hash', $userinfo->password); + session('password', null); // 跳转到首页 $data = ['msg' => '验证成功', 'val' => 1, 'url' =>'\\studentSearchChengji\\index\\index']; } else { @@ -75,4 +80,30 @@ class YanZheng return $data; } + /** + * Validate an existing login without needing to keep its clear-text + * password in session storage. + */ + public static function validateSession(string $category, int $userId, ?string $authHash): bool + { + if ($userId <= 0 || empty($authHash)) { + return false; + } + + $model = $category === 'admin' + ? \app\admin\model\Admin::class + : ($category === 'student' ? \app\student\model\Student::class : null); + + if ($model === null) { + return false; + } + + $user = $model::where('id', $userId) + ->where('status', 1) + ->field('password') + ->find(); + + return $user !== null && hash_equals((string) $user->password, $authHash); + } + } diff --git a/app/middleware/AdminOnly.php b/app/middleware/AdminOnly.php new file mode 100644 index 00000000..e7ae03ab --- /dev/null +++ b/app/middleware/AdminOnly.php @@ -0,0 +1,16 @@ +action()); $url = $mod.'/'.$con.'/'.$act; + // Data tables are rendered from their controller's index page. A + // generic ajaxdata action must inherit that page's permission, + // rather than granting every role access just because the action + // has a common name. + $permissionUrl = $act === 'ajaxdata' ? $mod.'/'.$con.'/index' : $url; // dump($admin, $url); // halt($url); @@ -43,11 +48,7 @@ class Auth // 排除控制器 $uneed_c = array(); # 荣誉器名首字母要大写 - $uneed_a = array( - 'upload', # 上传 - 'ajaxdata', # 常规数据获取 - 'saveall', # 批量保存 - ); + $uneed_a = array(); // 排除指定模块下的指定方法 $uneed_u = array( @@ -140,7 +141,7 @@ class Auth if ($except == false) { $auth = new AuthHandle; // 验证方法 - if($auth->check($url, session('user_id')) != true){// 第一个参数是规则名称,第二个参数是用户UID + if($auth->check($permissionUrl, session('user_id')) != true){// 第一个参数是规则名称,第二个参数是用户UID $this->error('哎哟~ 因为权限不足', '/login/err'); } } diff --git a/app/middleware/Login.php b/app/middleware/Login.php index a66fa4d4..e2d9916f 100755 --- a/app/middleware/Login.php +++ b/app/middleware/Login.php @@ -18,24 +18,11 @@ class Login { $category = session('onlineCategory'); - $yz = [ - 'val' => 0 - ,'msg' => '已经掉线啦~' - ]; - - switch ($category) { - case 'admin': - $yz = yz::admin(session('username'), session('password')); - break; - case 'student': - $yz = yz::student(session('username'), session('password')); - break; - default: - return redirect('/login'); - } - - if ($yz['val'] === 0) - { + if (!yz::validateSession( + (string) $category, + (int) session('user_id'), + session('auth_hash') + )) { return redirect('/login'); } diff --git a/app/studentSearchChengji/controller/Index.php b/app/studentSearchChengji/controller/Index.php index 7fa94e0e..1247b41f 100755 --- a/app/studentSearchChengji/controller/Index.php +++ b/app/studentSearchChengji/controller/Index.php @@ -89,6 +89,9 @@ class Index extends StudentSearchBase } ]) ->find(); + if ($khInfo === null || (int) $khInfo->student_id !== (int) session('user_id')) { + return $this->error('无权查看该成绩记录', '/studentSearchChengji/index/index'); + } $list['webtitle'] = $khInfo->cjKaoshi->title; $list['id'] = $khInfo->id; diff --git a/app/tools/controller/File.php b/app/tools/controller/File.php index 43f677fa..a29fce50 100755 --- a/app/tools/controller/File.php +++ b/app/tools/controller/File.php @@ -4,14 +4,14 @@ declare (strict_types = 1); namespace app\tools\controller; // 引用控制器基类 -use app\BaseController; +use app\AdminToolsBase; // 引用PhpSpreadsheet类 use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\IOFactory; -class File extends BaseController +class File extends AdminToolsBase { /** * 获取文件信息并保存 @@ -28,6 +28,26 @@ class File extends BaseController */ public function saveFileInfo($file, $list = array(), $isSave = false) { + if ($file === null || !$file->isValid()) { + return ['msg' => '上传文件无效', 'val' => 0]; + } + + $extension = strtolower($file->getOriginalExtension()); + $allowedExtensions = ['jpg', 'jpeg', 'png', 'xls', 'xlsx']; + if (!in_array($extension, $allowedExtensions, true)) { + return ['msg' => '文件格式必须为 jpg、jpeg、png、xls 或 xlsx', 'val' => 0]; + } + + if ($file->getSize() > 10 * 1024 * 1024) { + return ['msg' => '文件不能超过 10MiB', 'val' => 0]; + } + + $directory = trim((string) ($list['serurl'] ?? ''), '/\\'); + if ($directory === '' || !preg_match('/^[A-Za-z0-9][A-Za-z0-9_\\/-]{0,99}$/', $directory)) { + return ['msg' => '上传目录不合法', 'val' => 0]; + } + $list['serurl'] = $directory; + $hash = $file->hash('sha1'); $f = new \app\system\model\Fields; $serFile = $f::where('hash', $hash)->find(); diff --git a/app/tools/controller/OneStudentChengji.php b/app/tools/controller/OneStudentChengji.php index fb3ceafb..19e71074 100755 --- a/app/tools/controller/OneStudentChengji.php +++ b/app/tools/controller/OneStudentChengji.php @@ -4,12 +4,13 @@ declare (strict_types = 1); namespace app\tools\controller; // 引用控制器基类 -use app\BaseController; +use app\ToolsBase; +use think\exception\HttpException; // 引用学生数据模型类 use \app\tools\model\OneStudentChengji as STUCJ; -class OneStudentChengji extends BaseController +class OneStudentChengji extends ToolsBase { // 获取学生历次考试所有学科成绩 public function ajaxOldChengji() @@ -29,6 +30,8 @@ class OneStudentChengji extends BaseController ,'searchval' => '' ], 'POST'); + $this->assertStudentAccess((int) $src['student_id']); + // 获取学生成绩 $stucj = new STUCJ; $khList = $stucj->oldList($src); @@ -47,6 +50,8 @@ class OneStudentChengji extends BaseController 'kaohao_id' => '', ], 'POST'); + $this->assertKaohaoAccess((int) $src['kaohao_id']); + // 获取学生成绩 $stucj = new STUCJ; $data = $stucj->leiDa($src); @@ -64,6 +69,8 @@ class OneStudentChengji extends BaseController 'kaohao_id' => '', ], 'POST'); + $this->assertKaohaoAccess((int) $src['kaohao_id']); + // 获取学生成绩 $stucj = new STUCJ; $data = $stucj->yiBiao($src); @@ -81,6 +88,8 @@ class OneStudentChengji extends BaseController 'kaohao_id' => '', ], 'POST'); + $this->assertKaohaoAccess((int) $src['kaohao_id']); + // 获取学生成绩 $stucj = new STUCJ; $data = $stucj->oneChengji($src); @@ -99,6 +108,8 @@ class OneStudentChengji extends BaseController 'kaohao_id' => '', ], 'POST'); + $this->assertKaohaoAccess((int) $src['kaohao_id']); + // 获取学生成绩 $stucj = new STUCJ; $data = $stucj->oneDeFenLv($src); @@ -116,6 +127,8 @@ class OneStudentChengji extends BaseController 'kaohao_id' => '', ], 'POST'); + $this->assertKaohaoAccess((int) $src['kaohao_id']); + // 获取学生成绩 $stucj = new STUCJ; $data = $stucj->oneSubjectWeiZhi($src); @@ -139,6 +152,8 @@ class OneStudentChengji extends BaseController ,'searchval' => '' ], 'POST'); + $this->assertStudentAccess((int) $src['student_id']); + $stucj = new STUCJ; $data = $stucj->subjectOldChengji($src); return json($data); @@ -154,6 +169,8 @@ class OneStudentChengji extends BaseController 'kaohao_id' => '' ], 'POST'); + $this->assertKaohaoAccess((int) $src['kaohao_id']); + $stucj = new STUCJ; $baogao = $stucj->baogao($src); $data = [ @@ -165,4 +182,29 @@ class OneStudentChengji extends BaseController return json($data); } + private function assertStudentAccess(int $studentId): void + { + if ($studentId <= 0) { + throw new HttpException(422, '学生参数无效'); + } + + if (session('onlineCategory') === 'student' && $studentId !== (int) session('user_id')) { + throw new HttpException(403, '无权访问其他学生的成绩'); + } + } + + private function assertKaohaoAccess(int $kaohaoId): void + { + if ($kaohaoId <= 0) { + throw new HttpException(422, '考号参数无效'); + } + + $studentId = (int) \app\kaohao\model\Kaohao::where('id', $kaohaoId)->value('student_id'); + if ($studentId <= 0) { + throw new HttpException(404, '成绩记录不存在'); + } + + $this->assertStudentAccess($studentId); + } + } diff --git a/app/tools/controller/Teach.php b/app/tools/controller/Teach.php index 6c80aa08..d56b5aa8 100644 --- a/app/tools/controller/Teach.php +++ b/app/tools/controller/Teach.php @@ -2,9 +2,9 @@ namespace app\tools\controller; // 引用控制器基类 -use app\BaseController; +use app\AdminToolsBase; -class Teach extends BaseController +class Teach extends AdminToolsBase { // 获取类别列表 diff --git a/config/cookie.php b/config/cookie.php index 6087ec07..a759b4f9 100755 --- a/config/cookie.php +++ b/config/cookie.php @@ -2,6 +2,8 @@ // +---------------------------------------------------------------------- // | Cookie设置 // +---------------------------------------------------------------------- +use think\facade\Env; + return [ // cookie 保存时间 'expire' => 7200, @@ -10,9 +12,13 @@ return [ // cookie 有效域名 'domain' => '', // cookie 启用安全传输 - 'secure' => false, + // Production defaults to HTTPS-only. Local debug mode stays usable over + // HTTP unless COOKIE_SECURE explicitly overrides the setting. + 'secure' => Env::get('cookie.secure', !Env::get('app.debug', false)), // httponly设置 - 'httponly' => false, + 'httponly' => true, + // Prevent cross-site form posts from carrying the session by default. + 'samesite' => 'lax', // 是否使用 setcookie 'setcookie' => true, ]; diff --git a/config/middleware.php b/config/middleware.php index c42cfcfd..64f9c038 100755 --- a/config/middleware.php +++ b/config/middleware.php @@ -5,12 +5,14 @@ return [ 'alias' => [ 'login' => app\middleware\Login::class, 'auth' => app\middleware\Auth::class, - 'online' => app\middleware\Online::class, + 'online' => app\middleware\Online::class, + 'admin_only' => app\middleware\AdminOnly::class, ], // 优先级设置,此数组中的中间件会按照数组中的顺序优先执行 'priority' => [ 'online', 'login', + 'admin_only', 'auth', ], ];