Skip to content

TypeError: object null is not iterable #501

Closed

Description

@manunio

Describe the bug
While Fuzzing parseDocument threw uncaught exception at

const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/) as string[]

To Reproduce

> const yaml = await import("yaml")
undefined
> const data = '!!�������p: >�: >\n|\x18\x00\x00pro��)�������������������: !!\r: !!: >\n\x07\x04\x00ke'
undefined
> const docs = yaml.parseDocument(data)
Uncaught:
TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))
    at Directives.tagName (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/doc/directives.js:121:36)
    at Object.composeScalar (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/compose-scalar.js:13:26)
    at composeNode (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/compose-node.js:24:34)
    at Object.resolveBlockMap (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/resolve-block-map.js:56:15)
    at resolveCollection (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/compose-collection.js:13:27)
    at Object.composeCollection (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/compose-collection.js:47:16)
    at Object.composeNode (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/compose-node.js:31:38)
    at Object.composeDoc (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/compose-doc.js:33:23)
    at Composer.next (/home/maxx/dev/security/oss-fuzz-projects/yaml/dist/compose/composer.js:149:40)
    at next (<anonymous>)

Expected behaviour
parseDocument to not throw exception.

Versions (please complete the following information):

  • Environment: Node.js v18.18.2
  • yaml: v2.3.3

Additional context
Possible fix for this will be a check against null

diff --git a/src/doc/directives.ts b/src/doc/directives.ts
index add18fc..896c584 100644
--- a/src/doc/directives.ts
+++ b/src/doc/directives.ts
@@ -143,7 +143,12 @@ export class Directives {
       return verbatim
     }

-    const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/) as string[]
+    const matched = source.match(/^(.*!)([^!]*)$/) as string[]
+    if (!matched) {
+      onError(`The ${source} tag is invalid`)
+      return null
+    }
+    const [, handle, suffix] = matched
     if (!suffix) onError(`The ${source} tag has no suffix`)
     const prefix = this.tags[handle]
     if (prefix) {

but i was not able to reproduce this with single error message in doc.errors,

Activity

  1. added
    bug
    on Nov 2, 2023
  2. eemeli commented on Nov 3, 2023

    @eemeli
    Owner

    Ah, that was interesting. Managed to first shorten the input that's required to replicate the issue to just ': | !\r!', and then found the right fix: adding the s dot-all flag to the regexp on the line that's throwing the error, so that its . matches the \r.

  3. eemeli commented on Nov 3, 2023

    @eemeli
    Owner

    Fix released as 2.3.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions