Conversation
| email, | ||
| username, | ||
| name, | ||
| password: Math.random().toString(36).slice(-8) + Math.random().toString(36).slice(-8), // Dummy password |
Check failure
Code scanning / CodeQL
Insecure randomness
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 4 days ago
In general, the fix is to replace uses of Math.random() for generating passwords with a cryptographically secure random generator. In Node.js, that means using crypto.randomBytes (or crypto.randomInt) to derive the password characters. We must keep the observable behavior similar (i.e., still provide an 16‑character random password string, or close to that), but strengthen the entropy source.
Concretely, in apps/server-nestjs/src/modules/gitlab/gitlab.service.ts, update the createUser method so that the password property no longer concatenates Math.random().toString(36).... Instead, import randomBytes from Node’s built‑in crypto module at the top of the file and define a small helper function (within the shown snippet) that generates an 16‑character base‑36‑style random string using randomBytes. Then call that helper in place of the current expression. This preserves the rough format (alphanumeric string) and length while ensuring the randomness is cryptographically secure. Only new import(s) from standard Node libraries will be added, and existing logic, parameters, and return types of createUser will remain unchanged.
| @@ -6,6 +6,7 @@ | ||
| import { GitlabClientService } from './gitlab-client.service' | ||
| import { INFRA_GROUP_PATH, MIRROR_REPO_NAME } from './gitlab.constant' | ||
| import { join } from 'node:path' | ||
| import { randomBytes } from 'node:crypto' | ||
|
|
||
| @Injectable() | ||
| export class GitlabService { | ||
| @@ -204,11 +205,12 @@ | ||
|
|
||
| async createUser(email: string, username: string, name: string) { | ||
| // Note: This requires admin token usually | ||
| const password = randomBytes(12).toString('base64').slice(0, 16) | ||
| return this.client.Users.create({ | ||
| email, | ||
| username, | ||
| name, | ||
| password: Math.random().toString(36).slice(-8) + Math.random().toString(36).slice(-8), // Dummy password | ||
| password, // Dummy password | ||
| skipConfirmation: true, | ||
| }) | ||
| } |
| @@ -0,0 +1,63 @@ | |||
| import { Inject, Injectable, Logger } from '@nestjs/common' | |||
| @@ -0,0 +1,80 @@ | |||
| import { Inject, Injectable, Logger } from '@nestjs/common' | |||
| @@ -0,0 +1,50 @@ | |||
| import { Inject, Injectable, Logger } from '@nestjs/common' | |||
| @@ -0,0 +1,43 @@ | |||
| import { Inject, Injectable, Logger } from '@nestjs/common' | |||
690fc10 to
1dbdd69
Compare
32aba4d to
3f82528
Compare
ee9aa88 to
ab83dd2
Compare
f9a38ee to
9e306ed
Compare
76ee371 to
8d6f11f
Compare
846c206 to
c6e9a1f
Compare
2e33ab7 to
a806baf
Compare
Signed-off-by: William Phetsinorath <william.phetsinorath-open@interieur.gouv.fr>
Signed-off-by: William Phetsinorath <william.phetsinorath-open@interieur.gouv.fr>
Signed-off-by: William Phetsinorath <william.phetsinorath-open@interieur.gouv.fr>
Signed-off-by: William Phetsinorath <william.phetsinorath-open@interieur.gouv.fr>
|
Merged into #1958 |
Signed-off-by: William Phetsinorath william.phetsinorath-open@interieur.gouv.fr
Stack created with Sapling. Best reviewed with ReviewStack.