Skip to content

fix: guard against non-string values in frontmatter aliases - #377

Merged
RAIT-09 merged 2 commits into
RAIT-09:devfrom
chmzs:fix/non-string-aliases-crash
Aug 5, 2026
Merged

fix: guard against non-string values in frontmatter aliases#377
RAIT-09 merged 2 commits into
RAIT-09:devfrom
chmzs:fix/non-string-aliases-crash

Conversation

@chmzs

@chmzs chmzs commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix TypeError: n.toLowerCase is not a function crash in searchNotes() when frontmatter aliases contain non-string values.

Root Cause

searchNotes() spreads the aliases array directly into prepareFuzzySearch search fields. When an element is not a string (e.g., null, a number, or a YAML object), prepareFuzzySearch calls .toLowerCase() on it and throws TypeError.

Trigger in Reporter's Vault

The bug was triggered by template files with YAML syntax errors:

aliases:
  - "{ VALUE:英文名 }":

The trailing colon makes YAML parse this as [{ "{ VALUE:英文名 }": null }] — an array containing objects, not strings. When the plugin scans these template files, prepareFuzzySearch receives objects instead of strings and crashes.

Fix

Add a runtime typeof === "string" filter in both places that consume frontmatter aliases:

  • searchNotes() — filters aliasArray before spreading into fuzzy search fields
  • convertToMetadata() — filters the aliases array in the return value

This is a defensive fix: it prevents crashes regardless of how non-string values enter the aliases array (malformed YAML, plugin-generated notes, etc.).

Reproduction

Create a note with frontmatter: aliases: [null, "validAlias"] — console shows continuous TypeError.

Verification

  • All 47 existing tests pass
  • Two-line change, minimal risk

RAIT-09 and others added 2 commits June 25, 2026 00:45
When frontmatter aliases contain non-string values (null, numbers,
objects), prepareFuzzySearch throws TypeError on .toLowerCase().

Apply type guard filter in both searchNotes() and convertToMetadata()
to skip non-string array elements.

Repro: a note with aliases: [null, "validAlias"] in frontmatter
causes continuous console errors.
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Vault alias arrays are now filtered to string values in fuzzy note searches and metadata conversion, preventing non-string entries from entering either output.

Changes

Alias sanitization

Layer / File(s) Summary
Filter aliases for search and metadata
src/services/vault-service.ts
searchNotes and convertToMetadata now exclude non-string alias entries.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: rait-09

Poem

A rabbit checks each alias bright,
Keeps string carrots in the list just right.
Fuzzy searches hop with care,
Metadata gets clean fare.
No odd values nibble there!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the fix for non-string frontmatter aliases.
Description check ✅ Passed The description clearly explains the root cause, fix, reproduction, and verification, but it omits several template sections.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/services/vault-service.ts`:
- Around line 547-551: Update the aliases conversion expression in the metadata
conversion flow to only wrap scalar values when typeof aliases is "string";
preserve filtering for arrays and return undefined for all other scalar values.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cf1cd032-ecbc-49ea-850f-e26811d6677f

📥 Commits

Reviewing files that changed from the base of the PR and between 89e2d75 and 5b46f69.

📒 Files selected for processing (1)
  • src/services/vault-service.ts

Comment on lines 547 to 551
aliases: Array.isArray(aliases)
? aliases
? aliases.filter((a) => typeof a === 'string')
: aliases
? [aliases]
: undefined,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject non-string scalar aliases during metadata conversion.

Array aliases are filtered, but a truthy scalar such as 123 is still returned as [123], violating NoteMetadata.aliases?: string[] and allowing invalid values downstream. Use typeof aliases === "string" for the scalar branch; otherwise return undefined.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/services/vault-service.ts` around lines 547 - 551, Update the aliases
conversion expression in the metadata conversion flow to only wrap scalar values
when typeof aliases is "string"; preserve filtering for arrays and return
undefined for all other scalar values.

@RAIT-09 RAIT-09 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM! — thanks for tracking down the root cause and fixing both spots. Sorry for the slow review, grad school has been keeping me busy.

I'll handle the remaining CodeRabbit nit and formatting after merging — nothing needed on your end.

@RAIT-09
RAIT-09 merged commit 5d8a1c5 into RAIT-09:dev Aug 5, 2026
1 of 2 checks passed
pull Bot pushed a commit to jingyi-zhao-01/obsidian-agent-client that referenced this pull request Aug 6, 2026
…lizer

Follow-up to RAIT-09#377: a truthy non-string scalar (e.g. aliases: 42) still
leaked into NoteMetadata. Centralize frontmatter alias normalization in
one helper used by both searchNotes and convertToMetadata.
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.

2 participants