-
Notifications
You must be signed in to change notification settings - Fork 8.2k
fix: form-api解析fieldMappingTime结果时支持多级路径转换 #6720
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
Open
1455668754
wants to merge
2
commits into
vbenjs:main
Choose a base branch
from
1455668754:fix-fieldMappingTime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−20
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -511,40 +511,118 @@ | |
|
||
fieldMappingTime.forEach( | ||
([field, [startTimeKey, endTimeKey], format = 'YYYY-MM-DD']) => { | ||
// 处理多级路径的辅助函数 | ||
const setNestedValue = ( | ||
obj: Record<string, any>, | ||
path: string, | ||
value: any, | ||
): void => { | ||
const keys = path.split('.'); | ||
if (keys.length === 1) { | ||
// 单级路径直接赋值 | ||
obj[path] = value; | ||
} else { | ||
// 多级路径创建嵌套对象 | ||
let target: Record<string, any> = obj; | ||
for (let i = 0; i < keys.length - 1; i++) { | ||
const key = keys[i]; | ||
if (key !== undefined) { | ||
// 添加类型检查 | ||
if ( | ||
!(key in target) || | ||
typeof target[key] !== 'object' || | ||
target[key] === null | ||
) { | ||
target[key] = {}; | ||
} | ||
target = target[key] as Record<string, any>; | ||
} | ||
} | ||
const lastKey = keys[keys.length - 1]; | ||
if (lastKey !== undefined) { | ||
target[lastKey] = value; | ||
} | ||
} | ||
}; | ||
|
||
Comment on lines
+515
to
+547
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. Harden nested path helpers against prototype pollution and invalid segments Guard against "proto", "prototype", "constructor" and empty segments. This also addresses the CodeQL warning. Apply this diff: - const setNestedValue = (
+ const setNestedValue = (
obj: Record<string, any>,
path: string,
value: any,
): void => {
- const keys = path.split('.');
+ const keys = path.split('.').filter(Boolean);
+ // reject unsafe or empty paths
+ if (
+ keys.length === 0 ||
+ keys.some((k) => k === '__proto__' || k === 'prototype' || k === 'constructor')
+ ) {
+ console.warn('[form-api] Ignoring unsafe path:', path);
+ return;
+ }
if (keys.length === 1) {
// 单级路径直接赋值
obj[path] = value;
} else {
// 多级路径创建嵌套对象
let target: Record<string, any> = obj;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (key !== undefined) {
// 添加类型检查
- if (
- !(key in target) ||
- typeof target[key] !== 'object' ||
- target[key] === null
- ) {
+ if (
+ !(key in target) ||
+ typeof target[key] !== 'object' ||
+ target[key] === null ||
+ Array.isArray(target[key])
+ ) {
target[key] = {};
}
target = target[key] as Record<string, any>;
}
}
const lastKey = keys[keys.length - 1];
if (lastKey !== undefined) {
target[lastKey] = value;
}
}
};
// 处理多级路径的删除函数
- const deleteNestedProperty = (
+ const deleteNestedProperty = (
obj: Record<string, any>,
path: string,
): void => {
- const keys = path.split('.');
+ const keys = path.split('.').filter(Boolean);
+ // reject unsafe or empty paths
+ if (
+ keys.length === 0 ||
+ keys.some((k) => k === '__proto__' || k === 'prototype' || k === 'constructor')
+ ) {
+ console.warn('[form-api] Ignoring unsafe path:', path);
+ return;
+ }
if (keys.length === 1) {
// 单级路径直接删除
Reflect.deleteProperty(obj, path);
} else {
// 多级路径导航到目标对象并删除属性
let target: Record<string, any> = obj;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (key !== undefined) {
// 添加类型检查
if (
!(key in target) ||
typeof target[key] !== 'object' ||
target[key] === null
) {
return;
}
target = target[key] as Record<string, any>;
}
}
const lastKey = keys[keys.length - 1];
if (lastKey !== undefined) {
Reflect.deleteProperty(target, lastKey);
}
}
}; Also applies to: 549-579 |
||
// 处理多级路径的删除函数 | ||
const deleteNestedProperty = ( | ||
obj: Record<string, any>, | ||
path: string, | ||
): void => { | ||
const keys = path.split('.'); | ||
if (keys.length === 1) { | ||
// 单级路径直接删除 | ||
Reflect.deleteProperty(obj, path); | ||
} else { | ||
// 多级路径导航到目标对象并删除属性 | ||
let target: Record<string, any> = obj; | ||
for (let i = 0; i < keys.length - 1; i++) { | ||
const key = keys[i]; | ||
if (key !== undefined) { | ||
// 添加类型检查 | ||
if ( | ||
!(key in target) || | ||
typeof target[key] !== 'object' || | ||
target[key] === null | ||
) { | ||
return; | ||
} | ||
target = target[key] as Record<string, any>; | ||
} | ||
} | ||
const lastKey = keys[keys.length - 1]; | ||
if (lastKey !== undefined) { | ||
Reflect.deleteProperty(target, lastKey); | ||
} | ||
} | ||
}; | ||
|
||
// 类型安全检查 | ||
if (!field || !startTimeKey || !endTimeKey) { | ||
return; | ||
} | ||
|
||
if (startTimeKey && endTimeKey && values[field] === null) { | ||
Reflect.deleteProperty(values, startTimeKey); | ||
Reflect.deleteProperty(values, endTimeKey); | ||
// delete values[startTimeKey]; | ||
// delete values[endTimeKey]; | ||
deleteNestedProperty(values, startTimeKey); | ||
deleteNestedProperty(values, endTimeKey); | ||
} | ||
|
||
if (!values[field]) { | ||
Reflect.deleteProperty(values, field); | ||
// delete values[field]; | ||
deleteNestedProperty(values, field); | ||
return; | ||
} | ||
|
||
const timeRange = values[field]; | ||
if (!Array.isArray(timeRange) || timeRange.length < 2) { | ||
return; | ||
} | ||
|
||
const [startTime, endTime] = values[field]; | ||
const [startTime, endTime] = timeRange; | ||
if (format === null) { | ||
values[startTimeKey] = startTime; | ||
values[endTimeKey] = endTime; | ||
setNestedValue(values, startTimeKey, startTime); | ||
setNestedValue(values, endTimeKey, endTime); | ||
} else if (isFunction(format)) { | ||
values[startTimeKey] = format(startTime, startTimeKey); | ||
values[endTimeKey] = format(endTime, endTimeKey); | ||
setNestedValue(values, startTimeKey, format(startTime, startTimeKey)); | ||
setNestedValue(values, endTimeKey, format(endTime, endTimeKey)); | ||
} else { | ||
const [startTimeFormat, endTimeFormat] = Array.isArray(format) | ||
? format | ||
: [format, format]; | ||
|
||
values[startTimeKey] = startTime | ||
? formatDate(startTime, startTimeFormat) | ||
: undefined; | ||
values[endTimeKey] = endTime | ||
? formatDate(endTime, endTimeFormat) | ||
: undefined; | ||
setNestedValue( | ||
values, | ||
startTimeKey, | ||
startTime ? formatDate(startTime, startTimeFormat) : undefined, | ||
); | ||
setNestedValue( | ||
values, | ||
endTimeKey, | ||
endTime ? formatDate(endTime, endTimeFormat) : undefined, | ||
); | ||
} | ||
// delete values[field]; | ||
Reflect.deleteProperty(values, field); | ||
|
||
deleteNestedProperty(values, field); | ||
}, | ||
); | ||
return values; | ||
|
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
Oops, something went wrong.
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.
Check warning
Code scanning / CodeQL
Prototype-polluting function Medium