Skip to content

Commit 78010e7

Browse files
committed
fix(sdk): keep literal globstar suffix matches newline-safe
Follow-up to the leading-globstar literal-suffix fix (#1933), addressing a review comment on #1934: the suffix branch compiled to a plain regex where '.' does not cross newlines and (in Python) '$' also matches just before a trailing newline, so a filename containing a newline was matched wrongly in both directions. - Python: compile the suffix branch with re.DOTALL and anchor with '\Z', not '$'. - JS: add the 's' (dotAll) flag; '$' without 'm' already anchors to the absolute end. - Add string-level PatternMatcher unit tests in both SDKs covering normal, embedded-newline and trailing-newline names (deterministic, no filesystem). - Extend the changeset with the newline semantics.
1 parent d8881c1 commit 78010e7

5 files changed

Lines changed: 97 additions & 3 deletions

File tree

‎.changeset/hot-weeks-marry.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
'@e2b/python-sdk': patch
44
---
55

6-
Fix Dockerignore patterns with a leading `**` and a literal suffix, such as `**.txt`, to match filenames at any depth when copying template files. Preserve regex matching when the suffix contains additional wildcard syntax.
6+
Fix Dockerignore patterns with a leading `**` and a literal suffix, such as `**.txt`, to match filenames at any depth when copying template files. Preserve regex matching when the suffix contains additional wildcard syntax. The suffix match is newline-safe: filenames containing newlines still match on the literal suffix, and a trailing newline does not satisfy it.

‎packages/js-sdk/src/template/dockerignore.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ function compile(pattern: string): RegExp {
4545
!WILDCARD_CHARS.test(suffix) &&
4646
!suffix.includes(']')
4747
) {
48-
return new RegExp('^.*' + escapeRegex(suffix) + '$')
48+
// 's' (dotAll): a filename can contain newlines and JS '.' does not
49+
// match them by default, so without the flag '**.txt' misses
50+
// 'a\n.txt'. ($ without 'm' already anchors to the absolute end.)
51+
return new RegExp('^.*' + escapeRegex(suffix) + '$', 's')
4952
}
5053

5154
let regex = '^'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { PatternMatcher } from '../../src/template/dockerignore'
3+
4+
describe('PatternMatcher', () => {
5+
describe('leading globstar with a literal suffix', () => {
6+
test.each([
7+
'a.txt',
8+
'keep1.txt',
9+
'src/nested.txt',
10+
'src/generated/deep.txt',
11+
'weird\nname\nx.txt',
12+
])('matches %s', (path) => {
13+
expect(new PatternMatcher(['**.txt']).matches(path)).toBe(true)
14+
})
15+
16+
test.each(['keep.txt.bak', 'keep1.txtx', 'a.txt\n', 'notes.md'])(
17+
'does not match %s',
18+
(path) => {
19+
expect(new PatternMatcher(['**.txt']).matches(path)).toBe(false)
20+
}
21+
)
22+
23+
test('keeps regex characters in the suffix literal', () => {
24+
const matcher = new PatternMatcher(['**(1).txt'])
25+
expect(matcher.matches('root(1).txt')).toBe(true)
26+
expect(matcher.matches('root1.txt')).toBe(false)
27+
})
28+
29+
test('is newline-safe in both directions', () => {
30+
// A filename can contain newlines; JS '.' does not match them without
31+
// the dotAll flag, and '$' (without 'm') anchors to the absolute end.
32+
const matcher = new PatternMatcher(['**.txt'])
33+
expect(matcher.matches('a\n.txt')).toBe(true)
34+
expect(matcher.matches('a.txt\n')).toBe(false)
35+
})
36+
})
37+
})

‎packages/python-sdk/e2b/template/dockerignore.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ def _compile(pattern: str) -> "re.Pattern[str]":
3535
and not _WILDCARD_CHARS.search(suffix)
3636
and "]" not in suffix
3737
):
38-
return re.compile("^.*" + re.escape(suffix) + "$")
38+
# ``re.DOTALL`` and ``\Z`` (not ``$``): a filename can contain
39+
# newlines, Python's ``$`` also matches just before a trailing one, and
40+
# '.' does not cross newlines by default — so a literal suffix match
41+
# needs dot-all matching anchored at the absolute end of the path.
42+
return re.compile("^.*" + re.escape(suffix) + r"\Z", re.DOTALL)
3943

4044
regex = "^"
4145
i, n = 0, len(pattern)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""String-level unit tests for the .dockerignore ``PatternMatcher``.
2+
3+
These cover the pattern semantics directly, without walking a filesystem, so
4+
they run the same on every platform.
5+
"""
6+
7+
import pytest
8+
9+
from e2b.template.dockerignore import PatternMatcher
10+
11+
12+
class TestLeadingGlobstarLiteralSuffix:
13+
"""A leading ``**`` followed only by literals is a suffix match."""
14+
15+
@pytest.mark.parametrize(
16+
"path",
17+
[
18+
"a.txt",
19+
"keep1.txt",
20+
"src/nested.txt",
21+
"src/generated/deep.txt",
22+
"weird\nname\nx.txt",
23+
],
24+
)
25+
def test_matches_a_path_ending_in_the_suffix(self, path: str) -> None:
26+
assert PatternMatcher(["**.txt"]).matches(path)
27+
28+
@pytest.mark.parametrize(
29+
"path",
30+
[
31+
"keep.txt.bak",
32+
"keep1.txtx",
33+
"a.txt\n",
34+
"notes.md",
35+
],
36+
)
37+
def test_does_not_match_without_the_suffix(self, path: str) -> None:
38+
assert not PatternMatcher(["**.txt"]).matches(path)
39+
40+
def test_regex_characters_in_the_suffix_stay_literal(self) -> None:
41+
matcher = PatternMatcher(["**(1).txt"])
42+
assert matcher.matches("root(1).txt")
43+
assert not matcher.matches("root1.txt")
44+
45+
def test_a_newline_inside_the_name_does_not_hide_the_suffix(self) -> None:
46+
"""Filenames may contain newlines; the match must remain a literal
47+
suffix match in both directions."""
48+
matcher = PatternMatcher(["**.txt"])
49+
assert matcher.matches("a\n.txt")
50+
assert not matcher.matches("a.txt\n")

0 commit comments

Comments
 (0)