Skip to content

Commit

Permalink
fix extractPendingCsrs method flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Oct 8, 2023
1 parent 203ede1 commit 9702f94
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/backend/src/nest/registration/registration.functions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createUserCert } from '@quiet/identity'
import { createUserCert, keyFromCertificate } from '@quiet/identity'
import { IsBase64, IsNotEmpty, validate } from 'class-validator'
import { ErrorPayload, PermsData, SocketActionTypes, SuccessfullRegistrarionResponse } from '@quiet/types'
import { CsrContainsFields, IsCsr } from './registration.validators'
import { RegistrationEvents } from './registration.types'
import { loadCSR, CertFieldsTypes, getCertFieldValue, getReqFieldValue, parseCertificate } from '@quiet/identity'
import { CertificationRequest } from 'pkijs'
import Logger from '../common/logger'
import { load } from 'mock-fs'

const logger = Logger('registration.functions')
class UserCsrData {
Expand All @@ -29,6 +30,8 @@ export interface RegistrationResponse {
export const extractPendingCsrs = async (payload: { csrs: string[]; certificates: string[] }) => {
const certNames = new Set<string>()
const pendingNames = new Set<string>()
const parsedUniqueCsrs = new Map<string, string>()
const pendingCsrs: string[] = []

payload.certificates.forEach(cert => {
const parsedCert = parseCertificate(cert)
Expand All @@ -38,12 +41,19 @@ export const extractPendingCsrs = async (payload: { csrs: string[]; certificates
}
})

const pendingCsrs: string[] = []
for (const csr of payload.csrs.reverse()) {
const parsedCsr = await loadCSR(csr)
const pubKey = keyFromCertificate(parsedCsr)
if (!parsedUniqueCsrs.has(pubKey)) {
parsedUniqueCsrs.set(pubKey, csr)
}
}

const uniqueCsrsArray = Array.from(parsedUniqueCsrs.values()).reverse()

for (const csr of payload.csrs) {
for (const csr of uniqueCsrsArray) {
const parsedCsr = await loadCSR(csr)
const username = getReqFieldValue(parsedCsr, CertFieldsTypes.nickName)

if (username && !certNames.has(username) && !pendingNames.has(username)) {
pendingNames.add(username)
pendingCsrs.push(csr)
Expand Down

0 comments on commit 9702f94

Please sign in to comment.