Repository navigation
fix: preserve omitted auth config fields on partial updates - #7755
Merged
Merged
Conversation
shaohuzhang1
commented
Nov 9, 2026
| value = {**(portal.auth_config or {}), **value} | ||
| setattr(portal, field, value) | ||
| for field_name in ["logo"]: | ||
| if field_name in instance: |
Contributor
Author
There was a problem hiding this comment.
Here is an analysis of the provided code snippet with identified irregularities, potential issues, and optimization suggestions:
1. Security Risk: Insecure Direct Object Reference (IDOR) / Mass Assignment
- Issue: The code iterates over
instance.items()and appliessetattrifhasattr(portal, field)is true. Even thoughfile_fieldsexcludes some keys, this pattern is generally dangerous. It allows an attacker or malformed input to modify any attribute on theportalobject that isn't explicitly blocklisted. Ifauth_configcontains sensitive keys (e.g.,is_admin,permissions), merging user-provided dicts without sanitization could lead to privilege escalation or configuration tampering. - Suggestion: Maintain a whitelist of allowed fields instead of a blacklist. Only iterate over fields you explicitly intend to update.
ALLOWED_FIELDS = {"name", "description", "theme", "auth_config"} # example for field, value in instance.items(): if field not in ALLOWED_FIELDS: continue if hasattr(portal, field): # Your logic here setattr(portal, field, value)
2. Logic Error: Conditional Field Handling After General Loop
- Issue: The special handling for
"logo"appears to be in a separate loop (for field_name in ["logo"]) after the mainsetattrloop. However,"logo"is excluded from the main loop viafile_fields. This is okay, but if there are other fields needing special processing later, the structure is fragile. More importantly,auth_configis handled inline, whilelogois handled separately. This inconsistency makes maintenance harder. - Suggestion: Group all special-case handling together or handle all updates uniformly unless the field requires entirely different logic (e.g., file uploads vs. JSON config).
3. Deep Merge Issue: Shallow Dictionary Merge
- Issue: The line
{**(portal.auth_config or {}), **value}performs a shallow merge. Ifauth_configis a nested dictionary (common for auth configurations, e.g.,{"oidc": {"client_id": "...", "secret": "..."}}), updating{"oidc": {"enabled": True}}will wipe out existing keys insideoidc(likeclient_idorsecret). - Suggestion: Use a deep merge utility or
deepmergelibrary if partial updates to nested configs are intended.import deepmerge ... if field == "auth_config" and isinstance(value, dict): current = portal.auth_config or {} portal.auth_config = deepmerge.always_merger.merge(current, value)
4. Type Safety & Validation
- Issue: There’s no validation that
valueis acceptable before setting it. Especially forauth_config, ensure the final merged dict is valid before persisting. An invalid merge could break authentication. - Suggestion: Validate the resulting
auth_configafter merging.
5. Minor Code Style & Clarity
- Suggestion: Extract the special-case logic into a helper method or a more explicit conditional structure for readability.
if field == "auth_config" and isinstance(value, dict): merged_config = {**(portal.auth_config or {}), **value} # Optional: validate merged_config here setattr(portal, field, merged_config) else: setattr(portal, field, value)
✅ Recommended Refactored Code
def edit(self, instance, with_valid=True):
# Whitelist of fields that can be directly set
ALLOWED_DIRECT_FIELDS = {"name", "description", "title"} # Add your actual fields here
FILE_FIELDS = {"logo", "id", "create_time", "update_time"}
for field, value in instance.items():
if field in FILE_FIELDS:
continue
if field == "auth_config" and isinstance(value, dict):
# Note shallow merge caveat - use deep merge if needed
current_config = portal.auth_config or {}
new_config = {**current_config, **value}
# TODO: Add validation step here
portal.auth_config = new_config
elif field in ALLOWED_DIRECT_FIELDS:
setattr(portal, field, value)
else:
# Log warning or ignore unexpected fields
logger.warning(f"Ignoring unknown field in edit payload: {field}")
# Handle special case for logo separately if needed
if "logo" in instance:
# Existing logo handling logic
passKey Takeaways:
- Replace blacklisting with whitelisting.
- Consider deep merging for nested dictionaries like
auth_config. - Avoid using generic
setattrloops without strict control over allowed fields.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: preserve omitted auth config fields on partial updates