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

Prevent missing file extensions from throwing errors #5947

Closed

Conversation

lipemat
Copy link
Contributor

@lipemat lipemat commented May 8, 2024

Some file URL do not include the common .zip extension and therefore are missing their extension when reaching the import method of FileCache.

This issue is commonly seen with Premium plugins which use a custom URL to provide access to a keyed download file.

Running wp plugin update <name of premium plugin> was throwing a "Warning: copy(-.): Failed to open stream: Permission denied".

Instead of assuming the extension is valid nor trying to guess the extension is .zip simply bypass the storing of cache when the extension is not available.

Some file URL do not include the common .zip extension and therefore are missing their extension when reaching the `import` method of `FileCache`.

This issue is commonly seen with Premium plugins which use a custom URL to provide access to a keyed download file.

Running `wp plugin update <name of premium plugin>` was throwing a "Warning: copy(-<version>.): Failed to open stream: Permission denied". 

Instead of assuming the extension is valid nor trying to guess the extension is `.zip` simply bypass the storing of cache when the extension is not available.
@lipemat lipemat requested a review from a team as a code owner May 8, 2024 15:52
@BrianHenryIE
Copy link
Member

BrianHenryIE commented May 22, 2024

I'm not familiar with this code myself, so bear with me.

Here's a passing test for FileCache::import(): https://github.com/lipemat/wp-cli/commit/1b484041a8b1b5759b15f917713a7b10a5bdd56a

$key is a cache key, which I think looks typically like plugin/my-fixture-plugin-1.0.0.zip but there is code in FileCache::validate_key() which handles full URLs too.

I suspect your sample data looks something like http://premiumplugins.com/download-latest/?auth=123 but may have been altered by WP CLI before being used as $key, I'm not sure.

$source is the filename of the temp file that has been downloaded by the http request and is probably typically a .zip file.

It's possible for a server to serve plugin.zip from http://premiumplugins.com/download-latest so it may or may not have the correct extension.

So what should a failing test look like with the current code?

Would you please add WP_CLI::error( "FileCache::import( '{$key}', '{$source}' )" ); to the beginning of the FileCache::import(), run it once with an offending premium plugin, and let me know what it prints (then remove it, naturally!).

I think your solution of "never worry about caching files whose type is ambiguous" is fine, but I'd like to write an accurate test for it.

"Warning: copy(-.): Failed to open stream: Permission denied".

I haven't been able to reproduce this. Is that the exact message? It may be better to test is_readable() on the $source file than check extensions, since unzip my-plugin-1.0.0 will work without the extension.

https://github.com/lipemat/wp-cli/commit/132222fb2ad998fb3a5b4a61c2f91caca5f26f9e

@lipemat
Copy link
Contributor Author

lipemat commented May 22, 2024

Here is the error you requested:

Error: FileCache::import( 'plugin/advanced-sidebar-menu-pro-9.5.7.', 'C:\Users\box\AppData\Local\Temp/664e5754330ad-TsJObX.tmp' )

@BrianHenryIE
Copy link
Member

Thanks. I'm on MacOS and that doesn't cause a failing test for me. But Microsoft says:

Do not end a file or directory name with a space or a period.

https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions

So when FileCache::validate_key() is passed a string ending in ., it is returning a filename ending in . which is invalid on Windows.

I think the fix is to use rtrim() in FileCache::validate_key(): c38a371

Will you manually test that please?

@lipemat
Copy link
Contributor Author

lipemat commented May 22, 2024

That seems to work with rtrim leaving a file in the cache without an extension.

@BrianHenryIE
Copy link
Member

Closing in favour of #5951. Thank you.

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

Successfully merging this pull request may close these issues.

None yet

2 participants