Skip to content

Commit 91d4fe2

Browse files
committed
fix(certificates): improve error handling in certificate extraction functions
- Updated the `extractExpirationDate` and `extractCommonName` functions to return null instead of throwing errors when encountering unexpected structures in the certificate data. This change enhances the robustness of the certificate parsing logic.
1 parent 92caee5 commit 91d4fe2

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

  • apps/dokploy/components/dashboard/settings/certificates

apps/dokploy/components/dashboard/settings/certificates/utils.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export const extractExpirationDate = (certData: string): Date | null => {
3636
}
3737

3838
// Skip the outer certificate sequence
39-
if (der[offset++] !== 0x30) throw new Error("Expected sequence");
39+
if (der[offset++] !== 0x30) return null;
4040
({ offset } = readLength(offset));
4141

4242
// Skip tbsCertificate sequence
43-
if (der[offset++] !== 0x30) throw new Error("Expected tbsCertificate");
43+
if (der[offset++] !== 0x30) return null;
4444
({ offset } = readLength(offset));
4545

4646
// Check for optional version field (context-specific tag [0])
@@ -52,15 +52,14 @@ export const extractExpirationDate = (certData: string): Date | null => {
5252

5353
// Skip serialNumber, signature, issuer
5454
for (let i = 0; i < 3; i++) {
55-
if (der[offset] !== 0x30 && der[offset] !== 0x02)
56-
throw new Error("Unexpected structure");
55+
if (der[offset] !== 0x30 && der[offset] !== 0x02) return null;
5756
offset++;
5857
const fieldLen = readLength(offset);
5958
offset = fieldLen.offset + fieldLen.length;
6059
}
6160

6261
// Validity sequence (notBefore and notAfter)
63-
if (der[offset++] !== 0x30) throw new Error("Expected validity sequence");
62+
if (der[offset++] !== 0x30) return null;
6463
const validityLen = readLength(offset);
6564
offset = validityLen.offset;
6665

@@ -138,11 +137,11 @@ export const extractCommonName = (certData: string): string | null => {
138137
}
139138

140139
// Skip the outer certificate sequence
141-
if (der[offset++] !== 0x30) throw new Error("Expected sequence");
140+
if (der[offset++] !== 0x30) return null;
142141
({ offset } = readLength(offset));
143142

144143
// Skip tbsCertificate sequence
145-
if (der[offset++] !== 0x30) throw new Error("Expected tbsCertificate");
144+
if (der[offset++] !== 0x30) return null;
146145
({ offset } = readLength(offset));
147146

148147
// Check for optional version field (context-specific tag [0])
@@ -165,7 +164,7 @@ export const extractCommonName = (certData: string): string | null => {
165164
offset = skipField(offset);
166165

167166
// Subject sequence - where we find the CN
168-
if (der[offset++] !== 0x30) throw new Error("Expected subject sequence");
167+
if (der[offset++] !== 0x30) return null;
169168
const subjectLen = readLength(offset);
170169
const subjectEnd = subjectLen.offset + subjectLen.length;
171170
offset = subjectLen.offset;

0 commit comments

Comments
 (0)