-
Notifications
You must be signed in to change notification settings - Fork 0
[stripe] hardcoded admin env vars #16
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { SystemRoles } = require('librechat-data-provider'); | ||
const { logger } = require('~/config'); | ||
|
||
/** | ||
* Helper function to check if a user is a hardcoded admin (username-only) | ||
* | ||
* @param {string} username - Username to check | ||
* @returns {boolean} - True if user is a hardcoded admin | ||
*/ | ||
function isHardcodedAdmin(username) { | ||
if (!username) { | ||
logger.error('[Stripe:isHardcodedAdmin] message=No username found'); | ||
return false; | ||
} | ||
|
||
// example: HARDCODED_ADMIN_USERNAMES=username1,username2,username3 | ||
const hardcodedAdminUsernames = process.env.HARDCODED_ADMIN_USERNAMES; | ||
if (hardcodedAdminUsernames) { | ||
const adminUsernames = hardcodedAdminUsernames.split(',').map(username => username.trim().toLowerCase()); | ||
if (username && adminUsernames.includes(username.toLowerCase())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Modifies the user object to ensure that only hardcoded admins have admin role | ||
* | ||
* @param {Object} user - User object | ||
* @returns {Object} - User object with modified role | ||
*/ | ||
function ensureHardcodedAdminRole(user) { | ||
if (!user) { | ||
logger.error('[Stripe:ensureHardcodedAdminRole] message=No user found'); | ||
return user; | ||
} | ||
|
||
if (isHardcodedAdmin(user.username)) { | ||
user.role = SystemRoles.ADMIN; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we log here as well? It seems like checkAdminAccess is logged, so might be double-logging in the normal case, but we also use this function directly below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kept user null check and changed isHardcodedAdmin to accept a username so no redundant null check logs |
||
} else { | ||
// User is NOT a hardcoded admin → force role to USER (even if DB says ADMIN) | ||
user.role = SystemRoles.USER; | ||
} | ||
|
||
return user; | ||
} | ||
|
||
/** | ||
* Checks if a user should have admin access | ||
* Fallback to database role check when hardcoded admin is not enabled | ||
* | ||
* @param {Object} user - User object | ||
* @returns {boolean} - True if user should have admin access | ||
*/ | ||
function checkAdminAccess(user) { | ||
if (!user) { | ||
logger.error('[Stripe:checkAdminAccess] message=No user found'); | ||
return false; | ||
} | ||
if (process.env.HARDCODED_ADMIN_USERNAMES) { | ||
logger.info('[Stripe:checkAdminAccess] message=Hardcoded admin mode enabled'); | ||
return isHardcodedAdmin(user.username); | ||
} | ||
// Fallback to database role check when hardcoded admin is not enabled | ||
return user.role === SystemRoles.ADMIN; | ||
} | ||
|
||
module.exports = { | ||
ensureHardcodedAdminRole, | ||
checkAdminAccess, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Maybe add something to comment like:
e.g. HARDCODED_ADMIN_USERNAMES=colinlin,mattmueller