Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kharissulistiyo/603c02bc47037fa0751158929230e5e0 to your computer and use it in GitHub Desktop.

Select an option

Save kharissulistiyo/603c02bc47037fa0751158929230e5e0 to your computer and use it in GitHub Desktop.
Patch PHP CSV Injection vulnerability
<?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