forked from fairpm/fair-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespace.php
More file actions
288 lines (245 loc) · 8 KB
/
namespace.php
File metadata and controls
288 lines (245 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* Update FAIR packages.
*
* @package FAIR
*/
namespace FAIR\Updater;
use const FAIR\Packages\CACHE_DID_FOR_INSTALL;
use const FAIR\Packages\CACHE_RELEASE_PACKAGES;
use FAIR\Packages;
use function FAIR\is_wp_cli;
use Plugin_Upgrader;
use Theme_Upgrader;
use WP_CLI;
use WP_Error;
use WP_Upgrader;
/**
* Bootstrap.
*/
function bootstrap() {
add_action( 'init', __NAMESPACE__ . '\\run' );
add_filter( 'upgrader_source_selection', __NAMESPACE__ . '\\move_package_during_install', 10, 4 );
}
/**
* Gather all plugins/themes with data in Update URI and DID header.
*
* @return array
*/
function get_packages() : array {
$packages = [];
// Seems to be required for PHPUnit testing on GitHub workflow.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_path = trailingslashit( WP_PLUGIN_DIR );
$plugins = get_plugins();
foreach ( $plugins as $file => $plugin ) {
$plugin_id = get_file_data( $plugin_path . $file, [ 'PluginID' => 'Plugin ID' ] )['PluginID'];
if ( ! empty( $plugin_id ) ) {
$packages['plugins'][ $plugin_id ] = $plugin_path . $file;
}
}
$theme_path = WP_CONTENT_DIR . '/themes/';
$themes = wp_get_themes();
foreach ( $themes as $file => $theme ) {
$theme_id = get_file_data( $theme_path . $file . '/style.css', [ 'ThemeID' => 'Theme ID' ] )['ThemeID'];
if ( ! empty( $theme_id ) ) {
$packages['themes'][ $theme_id ] = $theme_path . $file . '/style.css';
}
}
return $packages;
}
/**
* Run FAIR\Updater\Updater for potential packages.
*
* @return void
*/
function run() {
$packages = get_packages();
$plugins = $packages['plugins'] ?? [];
$themes = $packages['themes'] ?? [];
$packages = array_merge( $plugins, $themes );
foreach ( $packages as $did => $filepath ) {
( new Updater( $did, $filepath ) )->run();
}
}
/**
* Download a package with signature verification.
*
* @param bool|string|WP_Error $reply Whether to proceed with the download, the path to the downloaded package, or an existing WP_Error object. Default true.
* @param string $package The URI of the package. If this is the full path to an existing local file, it will be returned untouched.
* @param WP_Upgrader $upgrader The WP_Upgrader instance.
* @param array $hook_extra Extra hook data.
* @return true|WP_Error True if the signature is valid, otherwise WP_Error.
*/
function verify_signature_on_download( $reply, string $package, WP_Upgrader $upgrader, $hook_extra ) {
static $has_run = [];
if ( false !== $reply || ( ! $upgrader instanceof Plugin_Upgrader && ! $upgrader instanceof Theme_Upgrader ) ) {
return $reply;
}
$did = get_transient( CACHE_DID_FOR_INSTALL );
if ( ! $did ) {
return $reply;
}
// This method is hooked to 'upgrader_pre_download', which is used in WP_Upgrader::download_package().
// Bailing on subsequent runs for the same package URI prevents an infinite loop.
$key = sha1( $did . '_' . $package );
if ( isset( $has_run[ $key ] ) ) {
return $reply;
}
$has_run[ $key ] = true;
// Local files should be returned untouched.
if ( ! preg_match( '!^(http|https|ftp)://!i', $package ) && file_exists( $package ) ) {
return $package;
}
$releases = get_transient( CACHE_RELEASE_PACKAGES ) ?? [];
if ( empty( $releases ) || ! isset( $releases[ $did ] ) ) {
return $reply;
}
$artifact = Packages\pick_artifact_by_lang( $releases[ $did ]->artifacts->package );
if ( ! $artifact || $package !== $artifact->url ) {
return $reply;
}
$path = $upgrader->download_package( $package, false, $hook_extra );
if ( is_wp_error( $path ) ) {
return $path;
}
add_filter( 'wp_trusted_keys', __NAMESPACE__ . '\\get_trusted_keys', 100 );
$decoded_base64url = sodium_base642bin( $artifact->signature, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING );
$result = verify_file_signature( $path, base64_encode( $decoded_base64url ) );
remove_filter( 'wp_trusted_keys', __NAMESPACE__ . '\\get_trusted_keys', 100 );
if ( $result === true ) {
if ( is_wp_cli() ) {
WP_CLI::success(
sprintf(
/* translators: %s: The DID of the package. */
__( 'Verified signature for %s', 'fair' ),
$did
)
);
}
return $path;
}
if ( is_wp_error( $result ) ) {
return $result;
}
return new WP_Error(
'fair.packages.signature_verification.failed',
sprintf(
/* translators: %s: The package's URL. */
__( 'Signature verification could not be performed for the package: %s', 'fair' ),
$package
)
);
}
/**
* Get trusted keys for signature verification.
*
* @return array
*/
function get_trusted_keys(): array {
$did = get_transient( CACHE_DID_FOR_INSTALL );
if ( ! $did ) {
return [];
}
$doc = Packages\get_did_document( $did );
if ( is_wp_error( $doc ) ) {
return [];
}
$keys = $doc->get_fair_signing_keys();
if ( empty( $keys ) ) {
return [];
}
/*
* FAIR uses Base58BTC-encoded Ed25519 keys.
* Core expects base64-encoded keys.
*/
$recoded_keys = [];
foreach ( $keys as $key ) {
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$str = Base58BTC::decode( $key->publicKeyMultibase );
// Ed25519 keys only.
if ( substr( $str, 0, 2 ) !== "\xed\x01" ) {
continue;
}
$key_material = substr( $str, 2 );
$recoded_keys[] = base64_encode( $key_material );
}
return $recoded_keys;
}
/**
* Move a package to the correctly named directory during installation.
*
* @param string $source Path of $source.
* @param string $remote_source Path of $remote_source.
* @param WP_Upgrader $upgrader An Upgrader object.
* @param array $hook_extra Array of hook data.
* @return string The correct directory path for installation.
*/
function move_package_during_install( string $source, string $remote_source, WP_Upgrader $upgrader, array $hook_extra ): string {
global $wp_filesystem;
if ( ! isset( $hook_extra['action'] ) || $hook_extra['action'] !== 'install' ) {
// Other actions are handled elsewhere.
return $source;
}
if ( ! in_array( $hook_extra['type'] ?? '', [ 'plugin', 'theme' ], true ) ) {
// This package type is not supported.
return $source;
}
$did = get_did_by_path( $source, $hook_extra['type'] );
if ( is_wp_error( $did ) ) {
// This isn't a valid FAIR package.
return $source;
}
$did_hash = Packages\get_did_hash( $did->get_id() );
if ( str_ends_with( $source, "{$did_hash}/" ) ) {
// The directory name is already correct.
return $source;
}
$new_source = untrailingslashit( $source ) . "-{$did_hash}/";
// Core must be able to find the new source directory.
$wp_filesystem->move( $source, $new_source, true );
return $new_source;
}
/**
* Get a package's DID by its path.
*
* @param string $path The absolute path to the package's directory or main file.
* @param string $type The type of package. Allowed types are 'plugin' or 'theme'.
* @return DID|WP_Error The DID object on success, WP_Error on failure.
*/
function get_did_by_path( $path, $type ) {
global $wp_filesystem;
if ( $type === 'theme' ) {
if ( ! str_ends_with( $path, 'style.css' ) ) {
$path = trailingslashit( $path ) . 'style.css';
}
$id = get_file_data( $path, [ 'id' => 'Theme ID' ] )['id'];
if ( $id ) {
return Packages\parse_did( $id );
}
}
if ( $type === 'plugin' ) {
if ( str_ends_with( $path, '.php' ) ) {
$id = get_file_data( $path, [ 'id' => 'Plugin ID' ] )['id'];
return Packages\parse_did( $id );
}
$files = $wp_filesystem->dirlist( $path ) ?: false;
if ( ! $files ) {
// Finding a DID is impossible.
return new WP_Error( 'fair.packages.dirlist_failed', __( "The package's file list could not be retrieved.", 'fair' ) );
}
foreach ( $files as $filename => $data ) {
if ( $data['type'] !== 'f' || ! str_ends_with( $filename, '.php' ) ) {
continue;
}
$filepath = trailingslashit( $path ) . $filename;
$id = get_file_data( $filepath, [ 'id' => 'Plugin ID' ] )['id'];
if ( $id ) {
return Packages\parse_did( $id );
}
}
}
return new WP_Error( 'fair.packages.none_found', __( 'No FAIR packages were found.', 'fair' ) );
}