Skip to content
Path traversal Disclosed report Intermediate

Null character at fnmatch

Ruby's fnmatch and fnmatch? can return true for a shorter string when the pattern contains a null byte, which may lead to unintended file operations.

ooooooo_q Follow

Originally disclosed on HackerOne

  • Disclosed after 11 months

I confirmed that it will behave unintentionally when null characters are entered in patterns with fnmatch,fnmatch?.

LOG
$ ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin16]

$ irb
irb(main):001:0> require 'pathname'
=> true

# should not be true
irb(main):002:0> File.fnmatch("x\0yz", 'x')
=> true
irb(main):003:0> File.fnmatch?("abc\0", 'abc')
=> true
irb(main):004:0> Pathname('x').fnmatch("x\0yz")
=> true
irb(main):005:0> Pathname('abc').fnmatch?("abc\0")
=> true

# It does not work as a delimiter
irb(main):006:0> File.fnmatch("x\0yz", 'yz')
=> false
irb(main):007:0> Pathname('yz').fnmatch("x\0yz")
=> false

# error
irb(main):008:0> File.fnmatch('x', "x\0")
Traceback (most recent call last):
        3: from /usr/local/bin/irb:11:in `<main>'
        2: from (irb):8
        1: from (irb):8:in `fnmatch'
ArgumentError (path name contains null byte)
irb(main):009:0> Pathname("x\0").fnmatch('x')
Traceback (most recent call last):
        4: from /usr/local/bin/irb:11:in `<main>'
        3: from (irb):9
        2: from (irb):9:in `Pathname'
        1: from (irb):9:in `initialize'
ArgumentError (pathname contains null byte)

Impact

When using these methods, unintended file operation may be performed.

The original report as published on HackerOne, by ooooooo_q. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the pattern string is treated as a C-style string that stops at the first null byte, so matching ignores everything after it.

Impact

Code that relies on these methods for file matching may perform unintended file operations when a pattern contains a null byte.

What the attacker needed

Pattern contains a null byte

How it was found

  1. Open an interactive Ruby session and load the pathname library.
  2. Call the file matching method with a pattern that contains a null byte followed by extra characters.
  3. Observe that it returns true for a shorter string that should not match.
  4. Call the same method with a null byte in the path argument instead and observe an error.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A null byte is a zero character. Ruby's file-name matching can return true for a shorter name when the pattern contains a null byte, so the wrong file could be chosen.

What is Path traversal?

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.

More by ooooooo_q

View all

53 more disclosed reports, best first.

More from Ruby

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.