fix(#136): mac address matching is case senstive

This commit is contained in:
Benjamin Nussbaum 2025-12-29 23:07:21 +01:00
parent a5cb38421e
commit 1298814521
3 changed files with 82 additions and 5 deletions

View file

@ -20,6 +20,14 @@ class Device extends Model
protected $guarded = ['id'];
/**
* Set the MAC address attribute, normalizing to uppercase.
*/
public function setMacAddressAttribute(?string $value): void
{
$this->attributes['mac_address'] = $value ? mb_strtoupper($value) : null;
}
protected $casts = [
'battery_notification_sent' => 'boolean',
'proxy_cloud' => 'boolean',

View file

@ -18,7 +18,7 @@ use Illuminate\Support\Str;
Route::get('/display', function (Request $request) {
$mac_address = $request->header('id');
$access_token = $request->header('access-token');
$device = Device::where('mac_address', $mac_address)
$device = Device::where('mac_address', mb_strtoupper($mac_address ?? ''))
->where('api_key', $access_token)
->first();
@ -29,7 +29,7 @@ Route::get('/display', function (Request $request) {
if ($auto_assign_user) {
// Create a new device and assign it to this user
$device = Device::create([
'mac_address' => $mac_address,
'mac_address' => mb_strtoupper($mac_address ?? ''),
'api_key' => $access_token,
'user_id' => $auto_assign_user->id,
'name' => "{$auto_assign_user->name}'s TRMNL",
@ -204,7 +204,7 @@ Route::get('/setup', function (Request $request) {
], 404);
}
$device = Device::where('mac_address', $mac_address)->first();
$device = Device::where('mac_address', mb_strtoupper($mac_address))->first();
if (! $device) {
// Check if there's a user with assign_new_devices enabled
@ -219,7 +219,7 @@ Route::get('/setup', function (Request $request) {
// Create a new device and assign it to this user
$device = Device::create([
'mac_address' => $mac_address,
'mac_address' => mb_strtoupper($mac_address),
'api_key' => Str::random(22),
'user_id' => $auto_assign_user->id,
'name' => "{$auto_assign_user->name}'s TRMNL",
@ -345,7 +345,7 @@ Route::post('/display/update', function (Request $request) {
Route::post('/screens', function (Request $request) {
$mac_address = $request->header('id');
$access_token = $request->header('access-token');
$device = Device::where('mac_address', $mac_address)
$device = Device::where('mac_address', mb_strtoupper($mac_address ?? ''))
->where('api_key', $access_token)
->first();

View file

@ -954,3 +954,72 @@ test('setup endpoint handles non-existent device model gracefully', function ():
expect($device)->not->toBeNull()
->and($device->device_model_id)->toBeNull();
});
test('setup endpoint matches MAC address case-insensitively', function (): void {
// Create device with lowercase MAC address
$device = Device::factory()->create([
'mac_address' => 'a1:b2:c3:d4:e5:f6',
'api_key' => 'test-api-key',
'friendly_id' => 'test-device',
]);
// Request with uppercase MAC address should still match
$response = $this->withHeaders([
'id' => 'A1:B2:C3:D4:E5:F6',
])->get('/api/setup');
$response->assertOk()
->assertJson([
'status' => 200,
'api_key' => 'test-api-key',
'friendly_id' => 'test-device',
'message' => 'Welcome to TRMNL BYOS',
]);
});
test('display endpoint matches MAC address case-insensitively', function (): void {
// Create device with lowercase MAC address
$device = Device::factory()->create([
'mac_address' => 'a1:b2:c3:d4:e5:f6',
'api_key' => 'test-api-key',
'current_screen_image' => 'test-image',
]);
// Request with uppercase MAC address should still match
$response = $this->withHeaders([
'id' => 'A1:B2:C3:D4:E5:F6',
'access-token' => $device->api_key,
'rssi' => -70,
'battery_voltage' => 3.8,
'fw-version' => '1.0.0',
])->get('/api/display');
$response->assertOk()
->assertJson([
'status' => '0',
'filename' => 'test-image.bmp',
]);
});
test('screens endpoint matches MAC address case-insensitively', function (): void {
Queue::fake();
// Create device with uppercase MAC address
$device = Device::factory()->create([
'mac_address' => 'A1:B2:C3:D4:E5:F6',
'api_key' => 'test-api-key',
]);
// Request with lowercase MAC address should still match
$response = $this->withHeaders([
'id' => 'a1:b2:c3:d4:e5:f6',
'access-token' => $device->api_key,
])->post('/api/screens', [
'image' => [
'content' => '<div>Test content</div>',
],
]);
$response->assertOk();
Queue::assertPushed(GenerateScreenJob::class);
});