Skip to content

Potential fix for code scanning alert no. 46: Prototype-polluting assignment#1357

Merged
sscargal merged 1 commit into
mainfrom
alert-autofix-46
Apr 20, 2026
Merged

Potential fix for code scanning alert no. 46: Prototype-polluting assignment#1357
sscargal merged 1 commit into
mainfrom
alert-autofix-46

Conversation

@sscargal
Copy link
Copy Markdown
Contributor

Potential fix for https://github.com/MemMachine/MemMachine/security/code-scanning/46

Use a prototype-less dictionary for structured and for each nested tag bucket, so dangerous keys like __proto__ are treated as ordinary data keys rather than prototype mutators.

Best fix in this file: in packages/ts-client/src/memory/format.ts, update formatSemanticMemories to:

  • initialize structured with Object.create(null) (typed as Record<string, Record<string, string>>);
  • initialize each structured[feature.tag] bucket with Object.create(null) as well.

This preserves existing functionality and output shape (JSON.stringify still produces the same JSON structure), while removing prototype-chain pollution behavior. No new imports or dependencies are required.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…ignment

Most JavaScript objects inherit the properties of the built-in Object.prototype object. Prototype pollution is a type of vulnerability in which an attacker can modify Object.prototype. Since most objects inherit from the compromised Object.prototype object, the attacker can use this to tamper with the application logic, and often escalate to remote code execution or cross-site scripting.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Steve Scargall <37674041+sscargal@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Hardens the TS client’s semantic-memory formatting against prototype pollution by switching the intermediate “structured” accumulator (and nested tag buckets) to prototype-less objects.

Changes:

  • Use Object.create(null) for the top-level structured dictionary in formatSemanticMemories.
  • Use Object.create(null) for each nested structured[feature.tag] bucket to prevent dangerous keys from mutating prototypes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +104 to 108
const structured = Object.create(null) as Record<string, Record<string, string>>
for (const feature of features) {
if (!(feature.tag in structured)) {
structured[feature.tag] = {}
structured[feature.tag] = Object.create(null) as Record<string, string>
}
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is addressing prototype-pollution hardening, but the existing unit tests for formatSemanticMemories don't appear to cover the new security-relevant behavior (e.g., tags/feature names like __proto__, constructor, or prototype). Adding a regression test for these keys would help ensure the alert fix doesn't regress and that the output remains safe to consume.

Copilot uses AI. Check for mistakes.
Comment on lines +104 to +107
const structured = Object.create(null) as Record<string, Record<string, string>>
for (const feature of features) {
if (!(feature.tag in structured)) {
structured[feature.tag] = {}
structured[feature.tag] = Object.create(null) as Record<string, string>
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The as Record<...> assertions here are avoidable and reduce type-safety (type assertions can hide mismatches). Consider keeping an explicit variable type annotation instead (e.g., const structured: Record<...> = Object.create(null) and similarly for the nested bucket) so TypeScript checks the value without needing as.

Copilot uses AI. Check for mistakes.
@sscargal sscargal marked this pull request as ready for review April 20, 2026 22:08
@sscargal sscargal added this to the v0.3.6 milestone Apr 20, 2026
@sscargal sscargal merged commit 15380b7 into main Apr 20, 2026
54 of 57 checks passed
@sscargal sscargal deleted the alert-autofix-46 branch April 20, 2026 22:44
edwinyyyu pushed a commit to edwinyyyu/MemMachine that referenced this pull request Apr 21, 2026
…ignment (MemMachine#1357)

Most JavaScript objects inherit the properties of the built-in Object.prototype object. Prototype pollution is a type of vulnerability in which an attacker can modify Object.prototype. Since most objects inherit from the compromised Object.prototype object, the attacker can use this to tamper with the application logic, and often escalate to remote code execution or cross-site scripting.

Signed-off-by: Steve Scargall <37674041+sscargal@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants