Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Validate uploaded filename only if no upload error occured. #6423

Merged
merged 1 commit into from
Aug 11, 2014
Merged

Validate uploaded filename only if no upload error occured. #6423

merged 1 commit into from
Aug 11, 2014

Conversation

hschletz
Copy link
Contributor

Most file upload errors (in particular UPLOAD_ERR_NO_FILE) yield an empty filename. The UploadFile validator does not report the original error in that case, but FILE_NOT_FOUND instead.

This fix validates the filename only in case of UPLOAD_ERR_OK. Any other error code will be reported correctly.

Most file upload errors (in particular UPLOAD_ERR_NO_FILE) yield an empty filename. The UploadFile validator does not report the original error in that case, but
FILE_NOT_FOUND instead.

This fix validates the filename only in case of UPLOAD_ERR_OK. Any other error code will be reported correctly.
@Ocramius Ocramius added this to the 2.3.2 milestone Jun 27, 2014
@Ocramius
Copy link
Member

Ocramius commented Aug 6, 2014

There are changes in the tests that I didn't notice before. Marking as BC Break and downgrading set milestone to 2.4

@Ocramius Ocramius modified the milestones: 2.3.2, 2.4.0 Aug 6, 2014
@hschletz
Copy link
Contributor Author

I disagree on the BC break.

The PR fixes a bug introduced in 606f28a. Before that commit, any PHP upload errors (like UPLOAD_ERR_NO_FILE) got checked and reported with their own validation message. These errors are typically accompanied by an empty string as filename because there is no file.

The faulty commit added an extra check on the filename which reports UploadFile::FILE_NOT_FOUND if the file does not exist. The problem is that this check is done before the PHP error code is evaluated. Since we have an empty string as filename in case of an upload error, we now get a meaningless UploadFile::FILE_NOT_FOUND message instead of the correct message like UploadFile::NO_FILE. The code that reports these messages is still there, but it never gets called because the filename check incorrectly takes precedence. There is no point in evaluating the filename if no file got uploaded, for reasons reported by the PHP error code.

Along with the commit came a new test testEmptyFileArrayShouldReturnFalseRatherThanTriggerError(), which later got renamed to testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage(). This test is broken too. It sets up a test case with UPLOAD_ERR_NO_FILE and an empty filename, and then asserts that UploadFile::FILE_NOT_FOUND is reported. The test passes, but only because it tests broken behavior. The correct message should be UploadFile::NO_FILE.

I changed the test to assert UploadFile::FILE_NOT_FOUND only in case of no error. The other test case with UPLOAD_ERR_NO_FILE got put into a new test which checks for UploadFile::NO_FILE.

To summarize: it's not a BC break, but a fix for recently broken behavior and broken tests.

@Ocramius
Copy link
Member

Still changes behavior on validation errors being produced, and therefore can't land in master IMO

@hschletz
Copy link
Contributor Author

It's a fix for a regression introduced in PR #5555, first released in ZF 2.2.6. That PR addresses an issue in all file validators, and except for UploadFile, that change makes sense. So this unintentional change in behavior is an oversight that should never have made it into a release.

Yes, my PR changes behavior (that's the nature of every bugfix) - but so does the mentioned faulty commit, because it never yields anything other than FILE_NOT_FOUND. If the breakage was introduced in a minor release, couldn't it be fixed in one?

@weierophinney
Copy link
Member

@Ocramius I'm inclined to agree with @hschletz on this one. The point here is that the validation message was actually incorrect previously, as another file upload error may have been the reason for the file not being present or accessible. In addition, we should not have attempted to resolve the file until we knew the file upload status was ok.

I'm marking for 2.3.2 -- the original fix was done during a patch release and introduced a change in validation behavior as well -- to correct an issue. This is the same situation. I'll make sure to point it out in the release notes.

@weierophinney weierophinney modified the milestones: 2.4.0, 2.3.2 Aug 11, 2014
@weierophinney weierophinney self-assigned this Aug 11, 2014
weierophinney added a commit that referenced this pull request Aug 11, 2014
…ptyFileCheck

Validate uploaded filename only if no upload error occured.
weierophinney added a commit that referenced this pull request Aug 11, 2014
weierophinney added a commit that referenced this pull request Aug 11, 2014
@weierophinney weierophinney merged commit 2f18e43 into zendframework:master Aug 11, 2014
weierophinney added a commit that referenced this pull request Aug 11, 2014
weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015
…oadErrorCodeShouldPrecedeEmptyFileCheck

Validate uploaded filename only if no upload error occured.
weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-validator that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants