Plugin Directory

Changeset 3837659


Ignore:
Timestamp:
12/01/2026 12:55:37 PM (3 days ago)
Author:
mxp
Message:

Make Feedback file-binding regression deterministic

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mxp-dev-tools/tests/fixtures/feedback-pow-regression.php

    r3801429 r3837659  
    5858}
    5959
     60function mdt_feedback_test_proof_matches($token, $payload_hash, $difficulty, $counter) {
     61        $hash = hash('sha256', $token . hex2bin($payload_hash) . pack('N', (int) $counter), true);
     62        $full_bytes = intdiv($difficulty, 8);
     63        $remaining_bits = $difficulty % 8;
     64        for ($index = 0; $index < $full_bytes; $index++) {
     65                if (ord($hash[$index]) !== 0) {
     66                        return false;
     67                }
     68        }
     69        if (!$remaining_bits) {
     70                return true;
     71        }
     72        $mask = (0xff << (8 - $remaining_bits)) & 0xff;
     73        return (ord($hash[$full_bytes]) & $mask) === 0;
     74}
     75
    6076function mdt_feedback_test_solve($token, $payload_hash, $difficulty) {
    6177        $payload_hash_binary = hex2bin($payload_hash);
    … …  
    184200        $changed_file_path = tempnam(get_temp_dir(), 'feedback-pow-');
    185201        copy($screenshot, $changed_file_path);
    186         file_put_contents($changed_file_path, 'changed', FILE_APPEND);
    187         $changed_file = mdt_feedback_test_request($auth_params, $changed_file_path);
    188         $changed_file->set_param(MDTFeedbackReporterPow::TOKEN_FIELD, $challenge['token']);
    189         $changed_file->set_param(MDTFeedbackReporterPow::COUNTER_FIELD, $auth_request->get_param(MDTFeedbackReporterPow::COUNTER_FIELD));
     202        do {
     203                file_put_contents($changed_file_path, 'changed', FILE_APPEND);
     204                clearstatcache(true, $changed_file_path);
     205                $changed_file = mdt_feedback_test_request($auth_params, $changed_file_path);
     206                $changed_file->set_param(MDTFeedbackReporterPow::TOKEN_FIELD, $challenge['token']);
     207                $changed_file->set_param(MDTFeedbackReporterPow::COUNTER_FIELD, $auth_request->get_param(MDTFeedbackReporterPow::COUNTER_FIELD));
     208                $changed_file_payload_hash = mdt_feedback_test_payload_hash($changed_file);
     209        } while (mdt_feedback_test_proof_matches(
     210                $challenge['token'],
     211                $changed_file_payload_hash,
     212                (int) $challenge['pow_difficulty'],
     213                $auth_request->get_param(MDTFeedbackReporterPow::COUNTER_FIELD)
     214        ));
    190215        $changed_file_result = mdt_feedback_test_response_data(rest_do_request($changed_file));
    191216        @unlink($changed_file_path);
Note: See TracChangeset for help on using the changeset viewer.