信任具有路由参数和隐藏输入值的用户
Trusting users with route parameter and hidden input values
我正在用laravel创建一个学校管理系统软件,这个项目恰好是我的第一个大项目,用于商业用途。而且没有人监督这项工作(所以你看我真的需要这里的建议)。所以我遇到的问题是何时信任用户输入以及何时在另一个 table 中使用模型之前检查模型是否存在。例如,在将 student_id 插入 'attendances' table 之前检查 'student_id' 是否存在于 'users' table 中。这个问题也适用于我想在其他 table 中保留模型的所有其他时间。作为说明,我拿出了一部分让学生出勤的代码。
HTML
<form method="POST" action="{{route('daily.attendance')}}">
@csrf
<input type="hidden" name="section_id" value="{{$section_id}}">
<input type="hidden" name="semester_id" value="{{$semester_id}}">
@foreach ($sections as $section)
@foreach ($section->users as $student)
<input type="checkbox" name = "present[]" value = "{{$student->id}}" class = "present">
<input type="hidden" name = "students[]" value = "{{$student->id}}">
@endforeach
@endforeach
<button type="submit" class="btn btn-primary" >{{ __('take attendance') }}</button>
</form>
php
public static function takeStudentDailyAttendance($request){
$presentStudents = ( isset($request->present) ) ? $request->present : [] ;
$dataForPresent = collect( $request->except(['present' , 'students']) )->merge(['status' => 1 , 'takenBy_id' => Auth::user()->id])->toArray();
$dataForAbsent = collect( $request->except(['present' , 'students']) )->merge(['status' => 0 , 'takenBy_id' => Auth::user()->id])->toArray();
DB::transaction(function () use ($request , $dataForPresent , $dataForAbsent , $presentStudents) {
foreach($request->students as $student_id){
if( in_array($student_id , $presentStudents) ){
$dataForPresent['student_id'] = $student_id;
StudentDailyAttendance::create($dataForPresent);
}else{
$dataForAbsent['student_id'] = $student_id;
StudentDailyAttendance::create($dataForAbsent);
}
}
});
}
因此,即使我为 $section_id 和 $semester_id 使用了隐藏输入,我知道用户(管理员)可以使用他的浏览器控制台更改这些值,然后发送表单。想象管理员有意或无意(显示 for 的路由是这样的 "daily/student/section/{section_id}')
将 $section_id 或 $semester_id 更改为与部分中的任何模型都不对应的值table 和学期 table。砰!!!他或她(管理员)参加了一个甚至不存在的部分或学期,这并不好,因为它有缺陷数据库的。所以问题是我应该信任管理员还是应该在插入 [=] 之前检查相应 table 中的 $section_id 和 $semester_id 23=] table(这样做也会增加脚本结束的时间)。
另外查看 php 代码,student_id 值也可以从 html 中篡改,您会看到代码循环遍历标记为存在的学生和标记为缺席的学生的数组,我是否也应该检查一下如果 $student_id 在考勤前存在于 'users' table 中(这样做也会增加脚本结束的时间)还是我也应该信任管理员?
规则总是说永远不要相信用户,尤其是在谈到数据完整性时。我建议尝试 laravel 的验证方法:
https://laravel.com/docs/6.x/validation#rule-exists
但是,如果您首先关注性能,我会在首次请求出席页面时将 userId 和 sectionId 缓存在 redis 哈希中一段时间,然后检查 student_id 是否在缓存 user_ids
如果您想查看 Laravel 的缓存:
https://laravel.com/docs/5.7/cache#retrieving-items-from-the-cache
我正在用laravel创建一个学校管理系统软件,这个项目恰好是我的第一个大项目,用于商业用途。而且没有人监督这项工作(所以你看我真的需要这里的建议)。所以我遇到的问题是何时信任用户输入以及何时在另一个 table 中使用模型之前检查模型是否存在。例如,在将 student_id 插入 'attendances' table 之前检查 'student_id' 是否存在于 'users' table 中。这个问题也适用于我想在其他 table 中保留模型的所有其他时间。作为说明,我拿出了一部分让学生出勤的代码。
HTML
<form method="POST" action="{{route('daily.attendance')}}">
@csrf
<input type="hidden" name="section_id" value="{{$section_id}}">
<input type="hidden" name="semester_id" value="{{$semester_id}}">
@foreach ($sections as $section)
@foreach ($section->users as $student)
<input type="checkbox" name = "present[]" value = "{{$student->id}}" class = "present">
<input type="hidden" name = "students[]" value = "{{$student->id}}">
@endforeach
@endforeach
<button type="submit" class="btn btn-primary" >{{ __('take attendance') }}</button>
</form>
php
public static function takeStudentDailyAttendance($request){
$presentStudents = ( isset($request->present) ) ? $request->present : [] ;
$dataForPresent = collect( $request->except(['present' , 'students']) )->merge(['status' => 1 , 'takenBy_id' => Auth::user()->id])->toArray();
$dataForAbsent = collect( $request->except(['present' , 'students']) )->merge(['status' => 0 , 'takenBy_id' => Auth::user()->id])->toArray();
DB::transaction(function () use ($request , $dataForPresent , $dataForAbsent , $presentStudents) {
foreach($request->students as $student_id){
if( in_array($student_id , $presentStudents) ){
$dataForPresent['student_id'] = $student_id;
StudentDailyAttendance::create($dataForPresent);
}else{
$dataForAbsent['student_id'] = $student_id;
StudentDailyAttendance::create($dataForAbsent);
}
}
});
}
因此,即使我为 $section_id 和 $semester_id 使用了隐藏输入,我知道用户(管理员)可以使用他的浏览器控制台更改这些值,然后发送表单。想象管理员有意或无意(显示 for 的路由是这样的 "daily/student/section/{section_id}')
将 $section_id 或 $semester_id 更改为与部分中的任何模型都不对应的值table 和学期 table。砰!!!他或她(管理员)参加了一个甚至不存在的部分或学期,这并不好,因为它有缺陷数据库的。所以问题是我应该信任管理员还是应该在插入 [=] 之前检查相应 table 中的 $section_id 和 $semester_id 23=] table(这样做也会增加脚本结束的时间)。
另外查看 php 代码,student_id 值也可以从 html 中篡改,您会看到代码循环遍历标记为存在的学生和标记为缺席的学生的数组,我是否也应该检查一下如果 $student_id 在考勤前存在于 'users' table 中(这样做也会增加脚本结束的时间)还是我也应该信任管理员?
规则总是说永远不要相信用户,尤其是在谈到数据完整性时。我建议尝试 laravel 的验证方法: https://laravel.com/docs/6.x/validation#rule-exists
但是,如果您首先关注性能,我会在首次请求出席页面时将 userId 和 sectionId 缓存在 redis 哈希中一段时间,然后检查 student_id 是否在缓存 user_ids 如果您想查看 Laravel 的缓存: https://laravel.com/docs/5.7/cache#retrieving-items-from-the-cache