Skip to content
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

fputcsv() flagged when writing to stream #404

Closed
paulschreiber opened this issue Apr 6, 2019 · 7 comments
Closed

fputcsv() flagged when writing to stream #404

paulschreiber opened this issue Apr 6, 2019 · 7 comments

Comments

@paulschreiber
Copy link
Contributor

Bug Description

The below code creates a CSV for download. PHPCS flags this as an error. I'm writing to a stream, not the filesystem.

Minimal Code Snippet

$output_stream = fopen( 'php://output', 'w' );

foreach ( $list as $row ) {
    fputcsv( $output_stream, $row );
}

Error Code

  88 | ERROR   | Filesystem writes are forbidden, you should not be using
     |         | fputcsv() (WordPress.VIP.FileSystemWritesDisallow)

Environment

Question Answer
PHP version 7.1.23
PHP_CodeSniffer version 3.4.1
VIPCS version 0.2.5
@GaryJones
Copy link
Contributor

VIPCS version | 0.2.5

Does this still happen with VIPCS 0.4.0 (latest published version)?

How about master?

@paulschreiber
Copy link
Contributor Author

  • Aside: version 0.4.0 of VIPCS conflicts with WPCS 2.1.0.
  • Problem still occurs with VIPCS 0.4.0, WPCS 1.2.1, PHPCS 3.4.1,

@GaryJones
Copy link
Contributor

How about master?

@paulschreiber
Copy link
Contributor Author

@GaryJones Is there a specific commit or PR you think addresses this or a related problem? Or are you just enjoying having me randomly regress things?

Yes, the problem still occurs with master (97fa945).

@GaryJones
Copy link
Contributor

Thank you.

Background notes:

@GaryJones GaryJones self-assigned this Apr 9, 2019
@GaryJones GaryJones added this to the Future Release milestone Apr 9, 2019
@GaryJones GaryJones modified the milestones: Future Release, 2.1 Jul 13, 2019
@GaryJones GaryJones modified the milestones: 2.2.0, 2.3.0 Jul 27, 2020
@rebeccahum
Copy link
Contributor

Due to the nature of static analysis, I think that that example would still flagged it because of $output_stream being a variable.

@jrfnl What are your thoughts for this one?

@jrfnl
Copy link
Collaborator

jrfnl commented Feb 24, 2021

@rebeccahum I agree.

Just based on the call to fputcsv(), there is no way to know whether the output will be written to a file or a stream.

With the code snippet above, the sniff could walk back to find how $output_stream is declared and analyse that, but in reality, most code isn't this clean cut and the $handle ($output_stream variable) may be passed into a function doing the actual writes, as a function parameter in which case there is no way to know what was passed in.

We know it will be a resource handle, but that's it. Presuming it is a resource handle to php://output would be a huge jump and would leave any code which does actually attempt to write to a file undetected, which I think is a worse outcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants