Repository navigation
Quiz attempt security improved #3282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harunollyo
wants to merge
3
commits into
4.3.0
Choose a base branch
from
security/z8zyy1vjmg
base: 4.3.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| use Tutor\Components\Modal; | ||
| use Tutor\Components\SvgIcon; | ||
| use Tutor\Components\Table; | ||
| use Tutor\Core\DB; | ||
| use Tutor\Core\Input; | ||
| use Tutor\Helpers\HttpHelper; | ||
| use Tutor\Helpers\QueryHelper; | ||
|
|
@@ -951,37 +952,26 @@ | |
| } | ||
|
|
||
| // Get total marks of all question comes. | ||
| $question_ids = tutor_utils()->avalue_dot( 'quiz_question_ids', $attempt_answer ); | ||
| $question_ids = array_filter( $question_ids, fn ( $id ) => is_numeric( $id ) && intval( $id ) > 0 ); | ||
| $quiz_answers = tutor_utils()->avalue_dot( 'quiz_question', $attempt_answer ); | ||
| $served_questions = tutor_utils()->get_random_questions_by_quiz( $attempt->quiz_id ); | ||
| $question_ids = wp_list_pluck( (array) $served_questions, 'question_id' ); | ||
| $quiz_answers = tutor_utils()->avalue_dot( 'quiz_question', $attempt_answer ); | ||
|
|
||
| // Calculate and set the total marks in attempt table for this question. | ||
| if ( tutor_utils()->count( $question_ids ) ) { | ||
| $question_ids_string = QueryHelper::prepare_in_clause( $question_ids ); | ||
|
|
||
| // Get total marks of the questions from question table. | ||
| //phpcs:disable | ||
| $query = $wpdb->prepare( | ||
| "SELECT SUM(question_mark) | ||
| FROM {$wpdb->prefix}tutor_quiz_questions | ||
| WHERE 1 = %d | ||
| AND quiz_id = %d | ||
| AND question_id IN({$question_ids_string}); | ||
| ", | ||
| 1, | ||
| $attempt->quiz_id | ||
| ); | ||
| $total_question_marks = $wpdb->get_var( $query ); | ||
| //phpcs:enable | ||
| $total_question_marks = DB::table( 'tutor_quiz_questions' ) | ||
| ->select_raw( 'SUM(question_mark) as total_marks' ) | ||
| ->where( 'quiz_id', $attempt->quiz_id ) | ||
| ->where_in( 'question_id', $question_ids ) | ||
| ->first(); | ||
|
|
||
| $total_question_marks = intval( $total_question_marks ? $total_question_marks->total_marks : 0 ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $total_question_marks = apply_filters( 'tutor_filter_update_before_question_mark', $total_question_marks, $question_ids, $user_id, $attempt_id ); | ||
|
|
||
| // Set the the total mark in the attempt table for the question. | ||
| $wpdb->update( | ||
| $wpdb->prefix . 'tutor_quiz_attempts', | ||
| array( 'total_marks' => $total_question_marks ), | ||
| array( 'attempt_id' => $attempt_id ) | ||
| ); | ||
| DB::table( 'tutor_quiz_attempts' ) | ||
| ->where( 'attempt_id', $attempt_id ) | ||
| ->update( array( 'total_marks' => $total_question_marks ) ); | ||
| } | ||
|
|
||
| $total_marks = 0; | ||
|
|
@@ -1265,7 +1255,8 @@ | |
| $wpdb->update( $wpdb->prefix . 'tutor_quiz_attempts', $attempt_info, array( 'attempt_id' => $attempt_id ) ); | ||
| do_action( 'tutor_quiz_finished', $attempt_id, $quiz_id, $attempt->user_id ); | ||
|
|
||
| wp_redirect( tutor_utils()->input_old( '_wp_http_referer' ) ); | ||
| wp_safe_redirect( wp_get_referer() ?: get_permalink( $quiz_id ) ); | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-picking the served set at submit depends on
RAND(attempt_id)+LIMITreturning the same rows as at render. That stops being true if the instructor adds, removes or reorders questions, or changesquestions_order, while an attempt is open, or if the result oftutor_filter_unsupported_quiz_question_typeschanges (e.g. Pro turned on or off). The student then gets scored on questions they never saw.Saving the served question IDs on the attempt when it starts, and validating against that list here, would be more robust.