4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- if (isset ($ _POST ['command ' ])) {
7
+ require_once __DIR__ . '/../../../../app/bootstrap.php ' ;
8
+
9
+ if (isset ($ _POST ['token ' ]) && isset ($ _POST ['command ' ])) {
10
+ $ magentoObjectManagerFactory = \Magento \Framework \App \Bootstrap::createObjectManagerFactory (BP , $ _SERVER );
11
+ $ magentoObjectManager = $ magentoObjectManagerFactory ->create ($ _SERVER );
12
+ $ tokenModel = $ magentoObjectManager ->get (\Magento \Integration \Model \Oauth \Token::class);
13
+
14
+ $ tokenPassedIn = urldecode ($ _POST ['token ' ]);
8
15
$ command = urldecode ($ _POST ['command ' ]);
16
+
9
17
if (array_key_exists ("arguments " , $ _POST )) {
10
- $ arguments = urldecode ($ _POST ['arguments ' ]);
18
+ $ arguments = escapeshellarg ( urldecode ($ _POST ['arguments ' ]) );
11
19
} else {
12
20
$ arguments = null ;
13
21
}
14
- $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
15
- $ valid = validateCommand ($ command );
16
- if ($ valid ) {
17
- exec (
18
- escapeCommand ($ php . ' -f ../../../../bin/magento ' . $ command ) . " $ arguments " ." 2>&1 " ,
19
- $ output ,
20
- $ exitCode
21
- );
22
- if ($ exitCode == 0 ) {
23
- http_response_code (202 );
22
+
23
+ // Token returned will be null if the token we passed in is invalid
24
+ $ tokenFromMagento = $ tokenModel ->loadByToken ($ tokenPassedIn )->getToken ();
25
+ if (!empty ($ tokenFromMagento ) && ($ tokenFromMagento == $ tokenPassedIn )) {
26
+ $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
27
+ $ magentoBinary = $ php . ' -f ../../../../bin/magento ' ;
28
+ $ valid = validateCommand ($ magentoBinary , $ command );
29
+ if ($ valid ) {
30
+ exec (
31
+ escapeCommand ($ magentoBinary . " $ command " . " $ arguments " ) . " 2>&1 " ,
32
+ $ output ,
33
+ $ exitCode
34
+ );
35
+ if ($ exitCode == 0 ) {
36
+ http_response_code (202 );
37
+ } else {
38
+ http_response_code (500 );
39
+ }
40
+ echo implode ("\n" , $ output );
24
41
} else {
25
- http_response_code (500 );
42
+ http_response_code (403 );
43
+ echo "Given command not found valid in Magento CLI Command list. " ;
26
44
}
27
- echo implode ("\n" , $ output );
28
45
} else {
29
- http_response_code (403 );
30
- echo " Given command not found valid in Magento CLI Command list. " ;
46
+ http_response_code (401 );
47
+ echo ( " Command not unauthorized. " ) ;
31
48
}
32
49
} else {
33
50
http_response_code (412 );
34
- echo ("Command parameter is not set. " );
51
+ echo ("Required parameters are not set. " );
35
52
}
36
53
37
54
/**
@@ -55,13 +72,13 @@ function escapeCommand($command)
55
72
56
73
/**
57
74
* Checks magento list of CLI commands for given $command. Does not check command parameters, just base command.
75
+ * @param string $magentoBinary
58
76
* @param string $command
59
77
* @return bool
60
78
*/
61
- function validateCommand ($ command )
79
+ function validateCommand ($ magentoBinary , $ command )
62
80
{
63
- $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
64
- exec ($ php . ' -f ../../../../bin/magento list ' , $ commandList );
81
+ exec ($ magentoBinary . ' list ' , $ commandList );
65
82
// Trim list of commands after first whitespace
66
83
$ commandList = array_map ("trimAfterWhitespace " , $ commandList );
67
84
return in_array (trimAfterWhitespace ($ command ), $ commandList );
0 commit comments