Created
November 29, 2024 10:25
-
-
Save kharissulistiyo/603c02bc47037fa0751158929230e5e0 to your computer and use it in GitHub Desktop.
Patch PHP CSV Injection vulnerability
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
| <?php | |
| const FORMULAS_START_CHARACTERS = [ '=', '-', '+', '@', "\t", "\r" ]; | |
| public function write( $data, $columns ) { | |
| $is_test_mode_off = ! defined( 'AAL_TESTMODE' ) || ( defined( 'AAL_TESTMODE' ) && ! AAL_TESTMODE ); | |
| if ( $is_test_mode_off ) { | |
| header( 'Content-type: text/csv' ); | |
| header( 'Content-Disposition: attachment; filename="activity-log-export.csv"' ); | |
| } | |
| $fp = fopen( 'php://output', 'w' ); | |
| fputcsv( $fp, $columns ); | |
| foreach ( $data as $row ) { | |
| $encoded_row = $this->get_encoded_row( $row ); | |
| fputcsv( $fp, $encoded_row ); | |
| } | |
| fclose( $fp ); | |
| if ( $is_test_mode_off ) { | |
| exit; | |
| } | |
| } | |
| private function get_encoded_row( $row ) { | |
| $result = []; | |
| foreach ( $row as $key => $value ) { | |
| $encoded_value = $value; | |
| if ( in_array( substr( (string) $value, 0, 1 ), self::FORMULAS_START_CHARACTERS, true ) ) { | |
| $encoded_value = "'" . $value; | |
| } | |
| $result[ $key ] = $encoded_value; | |
| } | |
| return $result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment