Skip to content

fix(providers): validate and format APNs private key before use #7982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/providers/src/lib/push/apns/apns.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class APNSPushProvider extends BaseProvider implements IPushProvider {
this.config = config;
this.provider = new apn.Provider({
token: {
key: config.key,
key: this.validateAndFormatKey(config.key),
keyId: config.keyId,
teamId: config.teamId,
},
Expand Down Expand Up @@ -73,4 +73,20 @@ export class APNSPushProvider extends BaseProvider implements IPushProvider {
date: new Date().toISOString(),
};
}

private validateAndFormatKey(key: string): string {
// Check if key is already properly formatted
const properFormat = /^-----BEGIN PRIVATE KEY-----\n[A-Za-z0-9+/=\n]+\n-----END PRIVATE KEY-----$/;

if (properFormat.test(key)) {
return key; // Key is already in correct format
}

// If not properly formatted, clean and format the key
const cleanKey = key.replace(/-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----|[\s\n\r]/g, '');

const formattedKey = cleanKey.match(/.{1,64}/g)?.join('\n') || '';

return `-----BEGIN PRIVATE KEY-----\n${formattedKey}\n-----END PRIVATE KEY-----`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, we will add a validation error during the creation of APN integration so that users are informed proactively about invalid integration properties.

}
}
Loading