codeant-ai-for-open-source[bot] commented on code in PR #39469:
URL: https://github.com/apache/superset/pull/39469#discussion_r3502748047


##########
superset-frontend/src/utils/generateAuthDbPassword.ts:
##########
@@ -0,0 +1,288 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { t } from '@apache-superset/core/translation';
+/**
+ * Client-side generator aligned with default ``AUTH_DB_CONFIG`` / Python
+ * ``superset.utils.auth_db_password`` (minimum length, character classes, 
common list).
+ * Keep ``AUTH_DB_COMMON_PASSWORDS`` in sync with ``_COMMON_PASSWORDS`` in 
that module.
+ */
+export const AUTH_DB_PASSWORD_MIN_LENGTH = 12;
+export interface AuthDbPasswordPolicy {
+  password_min_length: number;
+  password_require_uppercase: boolean;
+  password_require_lowercase: boolean;
+  password_require_digit: boolean;
+  password_require_special: boolean;
+  password_common_list_check: boolean;
+}
+
+export const AUTH_DB_DEFAULT_PASSWORD_POLICY: AuthDbPasswordPolicy = {
+  password_min_length: AUTH_DB_PASSWORD_MIN_LENGTH,
+  password_require_uppercase: true,
+  password_require_lowercase: true,
+  password_require_digit: true,
+  password_require_special: true,
+  password_common_list_check: true,
+};
+
+/**
+ * Lowercased entries; keep in sync with ``_COMMON_PASSWORDS`` in
+ * ``superset/utils/auth_db_password.py``. Sync is enforced by
+ * ``tests/unit_tests/utils/test_auth_db_password.py``.
+ */
+export const AUTH_DB_COMMON_PASSWORDS = new Set(
+  [
+    'password',
+    'password1',
+    'password123',
+    '123456',
+    '12345678',
+    '123456789',
+    'qwerty',
+    'abc123',
+    'monkey',
+    'letmein',
+    'trustno1',
+    'dragon',
+    'baseball',
+    'iloveyou',
+    'master',
+    'sunshine',
+    'ashley',
+    'bailey',
+    'shadow',
+    'superman',
+    'qazwsx',
+    'michael',
+    'football',
+    'welcome',
+    'jesus',
+    'ninja',
+    'mustang',
+    'password1!',
+    'admin',
+    'admin123',
+    'root',
+    'toor',
+    'guest',
+    'p@ssw0rd',
+    'Passw0rd',
+    'Password1',
+    'Password123',
+    'Welcome1',
+    'Qwerty123',
+  ].map(s => s.toLowerCase()),
+);
+
+const UPPER = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
+const LOWER = 'abcdefghijkmnopqrstuvwxyz';
+const DIGIT = '23456789';
+const SPECIAL = '!@#$%^&*-_=+';
+const ALPHANUM = UPPER + LOWER + DIGIT;
+
+export interface AuthDbPasswordPolicyChecks {
+  minLength: boolean;
+  uppercase: boolean;
+  lowercase: boolean;
+  digit: boolean;
+  special: boolean;
+  commonPassword: boolean;
+}
+
+function getCodePointLength(text: string): number {
+  return Array.from(text).length;
+}
+
+/** Mirrors ``int(cfg.get("password_min_length", default))`` in 
auth_db_password.py. */
+function resolveAuthDbPasswordMinLength(
+  passwordMinLength: number | undefined | null,
+): number {
+  if (passwordMinLength === undefined || passwordMinLength === null) {
+    return AUTH_DB_PASSWORD_MIN_LENGTH;
+  }
+  const parsed = Number(passwordMinLength);
+  if (!Number.isFinite(parsed)) {
+    return AUTH_DB_PASSWORD_MIN_LENGTH;
+  }
+  return Math.trunc(parsed);
+}
+
+function secureRandomInt(maxExclusive: number): number {
+  if (maxExclusive <= 0) {
+    throw new Error('secureRandomInt: maxExclusive must be positive');
+  }
+  const cryptoObj = globalThis.crypto;
+  if (!cryptoObj?.getRandomValues) {
+    throw new Error(
+      'secureRandomInt: a Web Crypto implementation with getRandomValues ' +
+        'is required to generate passwords',
+    );
+  }
+  const maxUint32 = 0xffffffff;
+  const limit = maxUint32 - (maxUint32 % maxExclusive);
+  const buf = new Uint32Array(1);
+  let value: number;
+  do {
+    cryptoObj.getRandomValues(buf);
+    value = buf[0]!;
+  } while (value >= limit);
+  return value % maxExclusive;
+}
+
+function pick(pool: string): string {
+  return pool[secureRandomInt(pool.length)]!;
+}
+
+function shuffleInPlace(chars: string[]): void {
+  for (let i = chars.length - 1; i > 0; i -= 1) {
+    const j = secureRandomInt(i + 1);
+    const t = chars[i]!;
+    chars[i] = chars[j]!;
+    chars[j] = t;
+  }
+}
+
+function getRequiredCharacterPools(policy: AuthDbPasswordPolicy): string[] {
+  const pools: string[] = [];
+  if (policy.password_require_uppercase) {
+    pools.push(UPPER);
+  }
+  if (policy.password_require_lowercase) {
+    pools.push(LOWER);
+  }
+  if (policy.password_require_digit) {
+    pools.push(DIGIT);
+  }
+  if (policy.password_require_special) {
+    pools.push(SPECIAL);
+  }
+  return pools;
+}
+
+function getGenerationPool(policy: AuthDbPasswordPolicy): string {
+  let pool = '';
+  if (policy.password_require_uppercase) {
+    pool += UPPER;
+  }
+  if (policy.password_require_lowercase) {
+    pool += LOWER;
+  }
+  if (policy.password_require_digit) {
+    pool += DIGIT;
+  }
+  if (policy.password_require_special) {
+    pool += SPECIAL;
+  }
+  return pool || ALPHANUM + SPECIAL;
+}
+
+/** Returns rule-by-rule checks for default AUTH_DB password policy. */
+export function getAuthDbPasswordPolicyChecks(
+  password: string,
+  policy: AuthDbPasswordPolicy = AUTH_DB_DEFAULT_PASSWORD_POLICY,
+): AuthDbPasswordPolicyChecks {
+  const minLength = resolveAuthDbPasswordMinLength(policy.password_min_length);
+  return {
+    minLength: getCodePointLength(password) >= minLength,
+    uppercase: !policy.password_require_uppercase || /[A-Z]/.test(password),
+    lowercase: !policy.password_require_lowercase || /[a-z]/.test(password),
+    digit: !policy.password_require_digit || /\p{Nd}/u.test(password),
+    special:
+      !policy.password_require_special || /[^\p{L}\p{N}\s]/u.test(password),
+    commonPassword:
+      !policy.password_common_list_check ||
+      !AUTH_DB_COMMON_PASSWORDS.has(password.toLowerCase().trim()),
+  };

Review Comment:
   **Suggestion:** Client-side policy checks do not include bcrypt’s 72-byte 
UTF-8 limit that the backend enforces, so some passwords are marked valid in 
the UI but are rejected by the API. Add a byte-length rule (conditioned on 
bcrypt hashing) to keep frontend validation aligned with server-side 
enforcement. [incomplete implementation]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ AUTH_DB password change rejects UI-approved long passwords.
   - ⚠️ Password strength indicator misleads about backend acceptance.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure Superset with AUTH_DB authentication (default `AUTH_TYPE = 
AUTH_DB` in
   `superset/config.py:463`) so `/api/v1/me/password` is enabled and 
`AUTH_DB_CONFIG` uses
   the default bcrypt hash algorithm 
(`superset/utils/auth_db_password.py:28-42`, `144-166`).
   
   2. Open the password reset UI, which renders `UserInfoModal`
   (`superset-frontend/src/features/userInfo/UserInfoModal.tsx:45-52`) and, 
when not in edit
   mode, fetches the password policy from `GET /api/v1/me/password/policy`
   (`UserInfoModal.tsx:57-76`, backend at `superset/views/users/api.py:72-107`) 
to populate
   `passwordPolicy` used by the frontend validator.
   
   3. In the modal, enter a very long password that satisfies all visible rules 
but exceeds
   bcrypt’s 72-byte UTF-8 limit (for example an 80-character ASCII string with 
uppercase,
   lowercase, digits, and specials); the new-password field’s validator calls
   `getPasswordPolicyError(password)` (`UserInfoModal.tsx:162-178`), which 
delegates to
   `getAuthDbPasswordPolicyError` and `getAuthDbPasswordPolicyChecks` in
   `superset-frontend/src/utils/generateAuthDbPassword.ts:195-212`, where 
checks only cover
   minLength, character classes, and common-password list—no byte-length rule.
   
   4. Submit the form, which issues `PUT /api/v1/me/password` 
(`UserInfoModal.tsx:90-107`);
   the backend parses the payload via `_load_password_change_body` and enforces 
policy with
   `validate_auth_db_password(body["new_password"])` 
(`superset/views/users/api.py:19-26`),
   and `validate_auth_db_password` includes `_bcrypt_byte_limit_errors` that 
reject passwords
   longer than `BCRYPT_MAX_PASSWORD_BYTES` 
(`superset/utils/auth_db_password.py:191-260`),
   causing a 400 error with a backend byte-limit message even though the 
frontend indicator
   and validator previously accepted the same password.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b543a3d8dc8a47799ec78d443ec31c9b&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b543a3d8dc8a47799ec78d443ec31c9b&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/utils/generateAuthDbPassword.ts
   **Line:** 99:211
   **Comment:**
        *Incomplete Implementation: Client-side policy checks do not include 
bcrypt’s 72-byte UTF-8 limit that the backend enforces, so some passwords are 
marked valid in the UI but are rejected by the API. Add a byte-length rule 
(conditioned on bcrypt hashing) to keep frontend validation aligned with 
server-side enforcement.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39469&comment_hash=4ba26a587902aaa94bd15d8210c5cf8f55d36ae25414198408bd97e9231a32ab&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39469&comment_hash=4ba26a587902aaa94bd15d8210c5cf8f55d36ae25414198408bd97e9231a32ab&reaction=dislike'>👎</a>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to