fix(server): allow dots in ssh username - #9951
Conversation
Centralize SSH username rules and sanitization so dotted usernames are accepted consistently across API, onboarding, and Livewire server forms.
|
@coderabbitai review |
✅ Action performedReview finished.
|
WalkthroughThis PR consolidates server username validation into a centralized pattern system. New regex constants and helper methods in Come with me if you want to live on servers instead of serverless—this PR proves self-hosting validation patterns are not a Skynet moment, just good engineering. No tacos harmed in the making of this PR (though gluten-free ones would be appreciated). 🌮⚔️ ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
app/Livewire/Boarding/Index.php (1)
276-283: 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick winInconsistent validation messages detected. Recommend upgrade. 🔧
Your onboarding flow uses the centralized rules (good!) but inline
$this->validate([...])doesn't let you provide custom error messages. Users get generic Laravel messages instead of the helpful "may only contain letters, numbers, dots, hyphens, and underscores" guidance.Your other Livewire components (
ByIp,Show) got this right by definingrules()andmessages()methods. For onboarding UX, where users are configuring their first server, clear validation messages are especially critical.♻️ Proposed fix for consistency
Add
rules()andmessages()methods to the component:+protected function rules(): array +{ + return [ + 'remoteServerName' => 'required|string', + 'remoteServerHost' => 'required|string', + 'remoteServerPort' => 'required|integer|min:1|max:65535', + 'remoteServerUser' => ValidationPatterns::serverUsernameRules(), + ]; +} + +protected function messages(): array +{ + return [ + ...ValidationPatterns::serverUsernameMessages('remoteServerUser', 'SSH User'), + ]; +}Then update both methods:
public function saveServer() { - $this->validate([ - 'remoteServerName' => 'required|string', - 'remoteServerHost' => 'required|string', - 'remoteServerPort' => 'required|integer', - 'remoteServerUser' => ValidationPatterns::serverUsernameRules(), - ]); + $this->validate(); // ... rest of method } public function saveAndValidateServer() { - $this->validate([ - 'remoteServerPort' => 'required|integer|min:1|max:65535', - 'remoteServerUser' => ValidationPatterns::serverUsernameRules(), - ]); + $this->validate(); // ... rest of method }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@app/Livewire/Boarding/Index.php` around lines 276 - 283, The saveServer() method currently calls $this->validate([...]) inline which prevents custom messages; add component-level rules() and messages() methods (matching pattern used in ByIp and Show) that return the validation array (use ValidationPatterns::serverUsernameRules() for 'remoteServerUser' etc.) and the user-friendly messages (e.g., "may only contain letters, numbers, dots, hyphens, and underscores" for the username rule); then update saveServer() to call $this->validate() with no parameters so Livewire uses the new rules() and messages() methods.app/Http/Controllers/Api/ServersController.php (1)
485-495:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMissing custom validation messages, and that bothers me. 🎯
You've wired up the username rules but forgot to merge in the custom error messages. When
uservalidation fails, API consumers get generic Laravel messages instead of the helpful "may only contain letters, numbers, dots, hyphens, and underscores" message.Your Livewire components got it right (see
ByIp.php:78andShow.php:143). Terminate the inconsistency!💬 Proposed fix to add custom messages
After the validator is created (around line 485), merge the custom messages:
$validator = customApiValidator($request->all(), [ 'name' => 'string|max:255', 'description' => 'string|nullable', 'ip' => ['string', 'required', new ValidServerIp], 'port' => 'integer|nullable|between:1,65535', 'private_key_uuid' => 'string|required', 'user' => ValidationPatterns::serverUsernameRules(required: false), 'is_build_server' => 'boolean|nullable', 'instant_validate' => 'boolean|nullable', 'proxy_type' => 'string|nullable', +], [ + ...ValidationPatterns::serverUsernameMessages('user', 'User'), ]);Apply the same fix to the
update_servermethod around line 664.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@app/Http/Controllers/Api/ServersController.php` around lines 485 - 495, The validator for ServersController methods (store_server around the shown block and update_server near line ~664) is missing the custom error messages for the 'user' field; after creating the validator via customApiValidator(...) merge in the same custom messages used in the Livewire components (see ByIp.php line 78 and Show.php line 143) so the username rule from ValidationPatterns::serverUsernameRules() produces the friendly "may only contain letters, numbers, dots, hyphens, and underscores" message instead of generic Laravel text; update both the store_server and update_server flows to merge those messages into the validator before returning errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@app/Http/Controllers/Api/ServersController.php`:
- Around line 485-495: The validator for ServersController methods (store_server
around the shown block and update_server near line ~664) is missing the custom
error messages for the 'user' field; after creating the validator via
customApiValidator(...) merge in the same custom messages used in the Livewire
components (see ByIp.php line 78 and Show.php line 143) so the username rule
from ValidationPatterns::serverUsernameRules() produces the friendly "may only
contain letters, numbers, dots, hyphens, and underscores" message instead of
generic Laravel text; update both the store_server and update_server flows to
merge those messages into the validator before returning errors.
In `@app/Livewire/Boarding/Index.php`:
- Around line 276-283: The saveServer() method currently calls
$this->validate([...]) inline which prevents custom messages; add
component-level rules() and messages() methods (matching pattern used in ByIp
and Show) that return the validation array (use
ValidationPatterns::serverUsernameRules() for 'remoteServerUser' etc.) and the
user-friendly messages (e.g., "may only contain letters, numbers, dots, hyphens,
and underscores" for the username rule); then update saveServer() to call
$this->validate() with no parameters so Livewire uses the new rules() and
messages() methods.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: c71baa0e-2ded-4bca-a683-174a63875abb
📒 Files selected for processing (8)
app/Http/Controllers/Api/ServersController.phpapp/Livewire/Boarding/Index.phpapp/Livewire/Server/New/ByIp.phpapp/Livewire/Server/Show.phpapp/Models/Server.phpapp/Support/ValidationPatterns.phptests/Feature/ServerUsernameValidationTest.phptests/Unit/ServerUsernamePatternTest.php
|
Thank you for the PR! 💜 |
Changes
Issues
Category
AI Assistance
If AI was used:
Testing
Spin up coolify dev and tried add server with username that has dots (on both onboarding and servers page)
Contributor Agreement
Important