Skip to content

A free open source IT asset/license management system

Fixed #19688 - validate ABM private key on save

snipe14744688c6fb
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php
index dda32d7ed5..67867ad118 100644
--- a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php
+++ b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php
@@ -183,6 +183,44 @@ class AppleBusinessManagerAdapter extends SyncAdapter
];
}
+ /**
+ * Parse the private key at save time so a malformed paste
+ * (stripped dash, wrong key type, etc) fails now instead
+ * of at the next pull with an OpenSSL error.
+ */
+ public function validationRules(): array
+ {
+ $rules = parent::validationRules();
+ $slug = $this->instance->slug;
+ $key = $slug.'_private_key';
+
+ $rules[$key] = array_merge($rules[$key] ?? ['required', 'string'], [
+ function (string $attribute, mixed $value, \Closure $fail): void {
+ if (! is_string($value) || $value === '') {
+ return;
+ }
+
+ $parsed = @openssl_pkey_get_private($value);
+ if ($parsed === false) {
+ $fail(trans('admin/settings/sync_adapters.abm_private_key_invalid'));
+
+ return;
+ }
+
+ $details = openssl_pkey_get_details($parsed);
+ $type = $details['type'] ?? null;
+ // OpenSSL and NIST spell the P-256 curve differently.
+ $curve = $details['ec']['curve_name'] ?? null;
+ $isP256Ec = $type === OPENSSL_KEYTYPE_EC && in_array($curve, ['prime256v1', 'secp256r1'], true);
+ if (! is_array($details) || ! $isP256Ec) {
+ $fail(trans('admin/settings/sync_adapters.abm_private_key_unsupported'));
+ }
+ },
+ ]);
+
+ return $rules;
+ }
+
public function extraFields(): array
{
return [
diff --git a/resources/lang/en-US/admin/settings/sync_adapters.php b/resources/lang/en-US/admin/settings/sync_adapters.php
index 7e09f958cc..5a766aa575 100644
--- a/resources/lang/en-US/admin/settings/sync_adapters.php
+++ b/resources/lang/en-US/admin/settings/sync_adapters.php
@@ -176,6 +176,8 @@ return [
'abm_client_id_help' => 'Create an API key under Settings -> API in your Apple Business Manager or Apple School Manager console. Copy the Client ID from the key detail page.',
'abm_key_id_help' => 'Paired with the Client ID. Shown on the same key detail page in the ABM/ASM console.',
'abm_private_key_help' => 'Paste the contents of the .pem private-key file you downloaded when you created the API key. Apple only lets you download the key once.',
+ 'abm_private_key_invalid' => 'This does not look like a valid private key file. Check that the first and last lines start with five dashes and that nothing was cut off when pasting.',
+ 'abm_private_key_unsupported' => 'This is a valid key, but it is not the format Apple Business Manager uses. Download a fresh key from your Apple Business Manager console (Settings -> API) and paste that instead. Apple only lets you download each key once, so create a new API key entry rather than trying to re-download an existing one.',
'abm_product_family_filter_help' => 'Only sync devices in the selected product families.',
'abm_pull_model_images_help' => 'Fetch product images from appledb.dev when auto-populating asset models from ABM devices. Existing model images are never overwritten. The category image serves only as a display fallback for models that have none.',
'abm_category_family_label' => 'Category for :family',
diff --git a/tests/Feature/SyncAdapters/AppleBusinessManager/PrivateKeyValidationTest.php b/tests/Feature/SyncAdapters/AppleBusinessManager/PrivateKeyValidationTest.php
new file mode 100644
index 0000000000..763b2ecdb0
--- /dev/null
+++ b/tests/Feature/SyncAdapters/AppleBusinessManager/PrivateKeyValidationTest.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters\AppleBusinessManager;
+
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\AppleBusinessManager\AppleBusinessManagerAdapter;
+use Illuminate\Support\Facades\Validator;
+use Tests\TestCase;
+
+
+class PrivateKeyValidationTest extends TestCase
+{
+ private AppleBusinessManagerAdapter $adapter;
+
+ private string $slug;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $instance = SyncAdapterInstance::where('slug', 'abm')->firstOrFail();
+ $this->adapter = new AppleBusinessManagerAdapter($instance);
+ $this->slug = $instance->slug;
+ }
+
+ public function test_valid_p256_pem_passes_validation(): void
+ {
+ $pem = $this->makeEcPem('prime256v1');
+
+ $this->assertPasses($pem);
+ }
+
+ public function test_blank_value_is_deferred_to_the_required_rule(): void
+ {
+ // Blank submission is the "keep-what's-in-DB" path for secret
+ // fields. The closure returns early so blank input doesn't
+ // false-positive as "invalid key".
+ $errors = $this->runValidator('');
+
+ $this->assertContains(
+ 'validation.required',
+ array_column($errors, 'rule'),
+ 'Blank private_key should fail the required rule, not the shape rule.',
+ );
+ $this->assertNotContains(
+ trans('admin/settings/sync_adapters.abm_private_key_invalid'),
+ array_column($errors, 'message'),
+ );
+ }
+
+ public function test_malformed_pem_fails_with_invalid_message(): void
+ {
+ // Mimics gh #19688 exact case: header lost one leading dash.
+ $malformed = str_replace('-----BEGIN', '----BEGIN', $this->makeEcPem('prime256v1'));
+
+ $this->assertFailsWith($malformed, 'admin/settings/sync_adapters.abm_private_key_invalid');
+ }
+
+ public function test_rsa_key_fails_with_unsupported_message(): void
+ {
+ $key = openssl_pkey_new(['private_key_type' => OPENSSL_KEYTYPE_RSA, 'private_key_bits' => 2048]);
+ $pem = '';
+ openssl_pkey_export($key, $pem);
+
+ $this->assertFailsWith($pem, 'admin/settings/sync_adapters.abm_private_key_unsupported');
+ }
+
+ public function test_wrong_curve_ec_key_fails_with_unsupported_message(): void
+ {
+ // EC on the wrong curve (P-384 here) parses cleanly but is
+ // the wrong shape for ES256 / Apple. Same user-facing
+ // remediation as the RSA case, so same trans key.
+ $pem = $this->makeEcPem('secp384r1');
+
+ $this->assertFailsWith($pem, 'admin/settings/sync_adapters.abm_private_key_unsupported');
+ }
+
+ private function makeEcPem(string $curve): string
+ {
+ $key = openssl_pkey_new([
+ 'private_key_type' => OPENSSL_KEYTYPE_EC,
+ 'curve_name' => $curve,
+ ]);
+ $pem = '';
+ openssl_pkey_export($key, $pem);
+
+ return $pem;
+ }
+
+ private function assertPasses(string $pem): void
+ {
+ $errors = $this->runValidator($pem);
+ $this->assertSame([], $errors, 'Expected private key to pass validation.');
+ }
+
+ private function assertFailsWith(string $pem, string $transKey): void
+ {
+ $errors = $this->runValidator($pem);
+ $messages = array_column($errors, 'message');
+ $this->assertContains(
+ trans($transKey),
+ $messages,
+ 'Expected validator to surface trans key "'.$transKey.'". Got: '.implode(' | ', $messages),
+ );
+ }
+
+ /**
+ * Run the adapter's validationRules() against a single-field
+ * payload and return every error message on the private_key
+ * field, tagged with the underlying rule name when it's a
+ * built-in rule.
+ *
+ * @return array<int, array{message: string, rule: string}>
+ */
+ private function runValidator(string $value): array
+ {
+ $rules = $this->adapter->validationRules();
+ $key = $this->slug.'_private_key';
+
+ $validator = Validator::make([$key => $value], [$key => $rules[$key]]);
+ $validator->passes();
+
+ $out = [];
+ foreach ($validator->errors()->get($key) as $message) {
+ $out[] = ['message' => $message, 'rule' => $this->guessRule($message)];
+ }
+
+ return $out;
+ }
+
+ private function guessRule(string $message): string
+ {
+ // The built-in `required` message renders as "The X field is
+ // required." for the slug-namespaced field. Match on that
+ // shape so the required-vs-shape check in the blank test
+ // doesn't depend on the exact localized string.
+ return str_contains($message, 'is required') ? 'validation.required' : 'closure';
+ }
+}