Skip to content

Commit 43df653

Browse files
committed
Ensure reserved environment names aren't changed, fix undefined variable, ref pterodactyl#412
1 parent 5545075 commit 43df653

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

app/Models/ServiceVariable.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ class ServiceVariable extends Model
5353
'user_editable' => 'integer',
5454
];
5555

56+
/**
57+
* Reserved environment variable names.
58+
*
59+
* @var array
60+
*/
61+
protected static $reservedNames = [
62+
'SERVER_MEMORY',
63+
'SERVER_IP',
64+
'SERVER_PORT',
65+
'ENV',
66+
'HOME',
67+
'USER',
68+
];
69+
70+
/**
71+
* Returns an array of environment variable names that cannot be used.
72+
*
73+
* @return array
74+
*/
75+
public static function reservedNames()
76+
{
77+
return self::$reservedNames;
78+
}
79+
5680
/**
5781
* Returns the display executable for the option and will use the parent
5882
* service one if the option does not have one defined.

app/Repositories/VariableRepository.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,25 @@ public function create($option, array $data)
5353
'env_variable' => 'required|regex:/^[\w]{1,255}$/',
5454
'default_value' => 'string',
5555
'options' => 'sometimes|required|array',
56-
'rules' => 'bail|required|string|min:1',
56+
'rules' => 'bail|required|string',
5757
]);
5858

5959
// Ensure the default value is allowed by the rules provided.
60-
$rules = (isset($data['rules'])) ? $data['rules'] : $variable->rules;
61-
$validator->sometimes('default_value', $rules, function ($input) {
60+
$validator->sometimes('default_value', $data['rules'] ?? null, function ($input) {
6261
return $input->default_value;
6362
});
6463

6564
if ($validator->fails()) {
6665
throw new DisplayValidationException(json_encode($validator->errors()));
6766
}
6867

69-
if (isset($data['env_variable'])) {
70-
$search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
71-
if ($search->first()) {
72-
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
73-
}
68+
if (in_array($data['env_variable'], ServiceVariable::reservedNames())) {
69+
throw new DisplayException('The environment variable name provided is a reserved keyword for the daemon.');
70+
}
71+
72+
$search = ServiceVariable::where('env_variable', $data['env_variable'])->where('option_id', $option->id);
73+
if ($search->first()) {
74+
throw new DisplayException('The envionment variable name assigned to this variable must be unique for this service option.');
7475
}
7576

7677
if (! isset($data['options']) || ! is_array($data['options'])) {
@@ -141,6 +142,10 @@ public function update($id, array $data)
141142
}
142143

143144
if (isset($data['env_variable'])) {
145+
if (in_array($data['env_variable'], ServiceVariable::reservedNames())) {
146+
throw new DisplayException('The environment variable name provided is a reserved keyword for the daemon.');
147+
}
148+
144149
$search = ServiceVariable::where('env_variable', $data['env_variable'])
145150
->where('option_id', $variable->option_id)
146151
->where('id', '!=', $variable->id);

0 commit comments

Comments
 (0)