Skip to content

Commit cf7cb27

Browse files
committed
harden snapshot names
1 parent 16c0d12 commit cf7cb27

3 files changed

Lines changed: 141 additions & 2 deletions

File tree

src/java/org/apache/cassandra/config/CassandraRelevantProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ public enum CassandraRelevantProperties
511511
SNAPSHOT_CLEANUP_PERIOD_SECONDS("cassandra.snapshot.ttl_cleanup_period_seconds", "60"),
512512
/** minimum allowed TTL for snapshots */
513513
SNAPSHOT_MIN_ALLOWED_TTL_SECONDS("cassandra.snapshot.min_allowed_ttl_seconds", "60"),
514+
/**
515+
* Validates names of to-be-taken snapshots, defaults to true.
516+
*/
517+
SNAPSHOT_NAME_VALIDATION("cassandra.snapshot.validation", "false"),
514518
SSL_ENABLE("ssl.enable"),
515519
SSL_STORAGE_PORT("cassandra.ssl_storage_port"),
516520
START_GOSSIP("cassandra.start_gossip", "true"),

src/java/org/apache/cassandra/db/ColumnFamilyStore.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.apache.cassandra.cache.RowCacheSentinel;
7979
import org.apache.cassandra.concurrent.ExecutorPlus;
8080
import org.apache.cassandra.concurrent.FutureTask;
81+
import org.apache.cassandra.config.CassandraRelevantProperties;
8182
import org.apache.cassandra.config.DatabaseDescriptor;
8283
import org.apache.cassandra.config.DurationSpec;
8384
import org.apache.cassandra.db.commitlog.CommitLog;
@@ -178,9 +179,11 @@
178179
import org.apache.cassandra.utils.concurrent.Refs;
179180
import org.apache.cassandra.utils.concurrent.UncheckedInterruptedException;
180181

182+
import static java.lang.String.format;
181183
import static org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory;
182184
import static org.apache.cassandra.config.DatabaseDescriptor.getFlushWriters;
183185
import static org.apache.cassandra.db.commitlog.CommitLogPosition.NONE;
186+
import static org.apache.cassandra.schema.SchemaConstants.FILENAME_LENGTH;
184187
import static org.apache.cassandra.utils.Clock.Global.currentTimeMillis;
185188
import static org.apache.cassandra.utils.Clock.Global.nanoTime;
186189
import static org.apache.cassandra.utils.FBUtilities.now;
@@ -2138,6 +2141,8 @@ public TableSnapshot snapshotWithoutMemtable(String snapshotName, Predicate<SSTa
21382141
throw new IllegalStateException(String.format("can not take ephemeral snapshot (%s) while ttl is specified too", snapshotName));
21392142
}
21402143

2144+
validateSnapshotName(snapshotName);
2145+
21412146
if (rateLimiter == null)
21422147
rateLimiter = DatabaseDescriptor.getSnapshotRateLimiter();
21432148

@@ -2160,6 +2165,41 @@ public TableSnapshot snapshotWithoutMemtable(String snapshotName, Predicate<SSTa
21602165
return createSnapshot(snapshotName, ephemeral, ttl, snapshottedSSTables, creationTime);
21612166
}
21622167

2168+
private void validateSnapshotName(String snapshotName)
2169+
{
2170+
if (snapshotName.contains(File.pathSeparator()))
2171+
{
2172+
throw new IllegalArgumentException("Snapshot name cannot contain " + File.pathSeparator());
2173+
}
2174+
2175+
if (snapshotName.equals(".") || snapshotName.equals(".."))
2176+
{
2177+
throw new IllegalArgumentException("Snapshot name '" + snapshotName + "' is reserved");
2178+
}
2179+
2180+
if (!CassandraRelevantProperties.SNAPSHOT_NAME_VALIDATION.getBoolean())
2181+
return;
2182+
2183+
// the length of valid snapshot name has to be less than or equal to FILENAME_LEGTH - that is 255 -
2184+
// we are following the max length as it is in SchemaConstants for table name.
2185+
if (snapshotName.length() > SchemaConstants.FILENAME_LENGTH)
2186+
{
2187+
throw new IllegalArgumentException(format("Snapshot name must not be more than %d characters long for " +
2188+
"resolved snapshot name (got %d characters for \"%s\")",
2189+
FILENAME_LENGTH, snapshotName.length(), snapshotName));
2190+
}
2191+
2192+
// Allowed characters follow the AWS S3 "Safe characters" set documented at
2193+
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-guidelines :
2194+
// 0-9 a-z A-Z ! - _ . * ' ( )
2195+
// The path separator '/' is intentionally excluded,
2196+
// which is what blocks traversal attempts such as "../../mysnapshot"
2197+
if (!Pattern.compile("[a-zA-Z0-9!_.*'()-]+").matcher(snapshotName).matches())
2198+
{
2199+
throw new IllegalArgumentException("Snapshot name contains illegal characters: " + snapshotName);
2200+
}
2201+
}
2202+
21632203
protected TableSnapshot createSnapshot(String tag, boolean ephemeral, DurationSpec.IntSecondsBound ttl, Set<SSTableReader> sstables, Instant creationTime) {
21642204
Set<File> snapshotDirs = sstables.stream()
21652205
.map(s -> Directories.getSnapshotDirectory(s.descriptor, tag).toAbsolute())

test/unit/org/apache/cassandra/db/SnapshotTest.java

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,32 @@
2121
import java.nio.file.Files;
2222
import java.nio.file.StandardOpenOption;
2323

24+
import org.junit.Before;
2425
import org.junit.Test;
2526

27+
import org.apache.cassandra.config.CassandraRelevantProperties;
2628
import org.apache.cassandra.cql3.CQLTester;
29+
import org.apache.cassandra.distributed.shared.WithProperties;
2730
import org.apache.cassandra.io.sstable.format.SSTableFormat.Components;
2831
import org.apache.cassandra.io.sstable.format.SSTableReader;
2932
import org.apache.cassandra.io.util.File;
33+
import org.apache.cassandra.schema.SchemaConstants;
34+
35+
import static org.assertj.core.api.Assertions.assertThatCode;
36+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3037

3138
public class SnapshotTest extends CQLTester
3239
{
33-
@Test
34-
public void testEmptyTOC() throws Throwable
40+
@Before
41+
public void setUpTable() throws Throwable
3542
{
3643
createTable("create table %s (id int primary key, k int)");
3744
execute("insert into %s (id, k) values (1,1)");
45+
}
46+
47+
@Test
48+
public void testEmptyTOC() throws Throwable
49+
{
3850
getCurrentColumnFamilyStore().forceBlockingFlush(ColumnFamilyStore.FlushReason.UNIT_TESTS);
3951
for (SSTableReader sstable : getCurrentColumnFamilyStore().getLiveSSTables())
4052
{
@@ -43,4 +55,87 @@ public void testEmptyTOC() throws Throwable
4355
}
4456
getCurrentColumnFamilyStore().snapshot("hello");
4557
}
58+
59+
@Test
60+
public void testSnapshotNameValidation()
61+
{
62+
ColumnFamilyStore cfs = getCurrentColumnFamilyStore();
63+
String sep = File.pathSeparator();
64+
65+
try (WithProperties p = new WithProperties().set(CassandraRelevantProperties.SNAPSHOT_NAME_VALIDATION, true))
66+
{
67+
// Previously-allowed alphanumerics, '-' and '_' must still be accepted.
68+
assertThatCode(() -> cfs.snapshot("atag")).doesNotThrowAnyException();
69+
assertThatCode(() -> cfs.snapshot("a-tag")).doesNotThrowAnyException();
70+
assertThatCode(() -> cfs.snapshot("a_tag")).doesNotThrowAnyException();
71+
assertThatCode(() -> cfs.snapshot("a_tag_1and_something2-more")).doesNotThrowAnyException();
72+
assertThatCode(() -> cfs.snapshot(repeat('a', SchemaConstants.FILENAME_LENGTH))).doesNotThrowAnyException();
73+
74+
// AWS S3 "Safe characters" accepted by the relaxed allowlist:
75+
// ! . * ' ( )
76+
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-guidelines
77+
assertThatCode(() -> cfs.snapshot("snap.2026-05-20")).doesNotThrowAnyException();
78+
assertThatCode(() -> cfs.snapshot("important!")).doesNotThrowAnyException();
79+
assertThatCode(() -> cfs.snapshot("backup*")).doesNotThrowAnyException();
80+
assertThatCode(() -> cfs.snapshot("o'snap")).doesNotThrowAnyException();
81+
assertThatCode(() -> cfs.snapshot("snap(1)")).doesNotThrowAnyException();
82+
assertThatCode(() -> cfs.snapshot("!._-*'()")).doesNotThrowAnyException();
83+
// Dots embedded in a name are not traversal: with '/' excluded, "a..tag" is just a literal directory.
84+
assertThatCode(() -> cfs.snapshot("a..tag")).doesNotThrowAnyException();
85+
86+
String tooLong = repeat('a', SchemaConstants.FILENAME_LENGTH + 1);
87+
assertThatThrownBy(() -> cfs.snapshot(tooLong))
88+
.isInstanceOf(IllegalArgumentException.class)
89+
.hasMessage("Snapshot name must not be more than 255 characters long for " +
90+
"resolved snapshot name (got 256 characters for \"" + tooLong + "\")");
91+
92+
// '/' is not in the S3-safe set; this is what kills traversal attempts like "../../mysnapshot".
93+
assertThatThrownBy(() -> cfs.snapshot("a" + sep + "tag"))
94+
.isInstanceOf(IllegalArgumentException.class)
95+
.hasMessage("Snapshot name cannot contain " + sep);
96+
97+
// Other characters outside the S3-safe set must still be rejected.
98+
assertThatThrownBy(() -> cfs.snapshot("a tag"))
99+
.isInstanceOf(IllegalArgumentException.class)
100+
.hasMessage("Snapshot name contains illegal characters: a tag");
101+
assertThatThrownBy(() -> cfs.snapshot("a:tag"))
102+
.isInstanceOf(IllegalArgumentException.class)
103+
.hasMessage("Snapshot name contains illegal characters: a:tag");
104+
105+
// "." and ".." pass the charset check but resolve to the snapshots/ dir itself
106+
// and its parent (the live table dir) respectively, so they must be rejected as reserved.
107+
assertThatThrownBy(() -> cfs.snapshot("."))
108+
.isInstanceOf(IllegalArgumentException.class)
109+
.hasMessage("Snapshot name '.' is reserved");
110+
assertThatThrownBy(() -> cfs.snapshot(".."))
111+
.isInstanceOf(IllegalArgumentException.class)
112+
.hasMessage("Snapshot name '..' is reserved");
113+
}
114+
115+
try (WithProperties p = new WithProperties().set(CassandraRelevantProperties.SNAPSHOT_NAME_VALIDATION, false))
116+
{
117+
// Previously-rejected characters are now accepted: space, ':', and other non-S3-safe chars.
118+
assertThatCode(() -> cfs.snapshot("a tag")).doesNotThrowAnyException();
119+
assertThatCode(() -> cfs.snapshot("a:tag")).doesNotThrowAnyException();
120+
121+
// Path separator and "." / ".." rejections are unconditional — they guard against
122+
// traversal regardless of the toggle.
123+
assertThatThrownBy(() -> cfs.snapshot("a" + sep + "tag"))
124+
.isInstanceOf(IllegalArgumentException.class)
125+
.hasMessage("Snapshot name cannot contain " + sep);
126+
assertThatThrownBy(() -> cfs.snapshot("."))
127+
.isInstanceOf(IllegalArgumentException.class)
128+
.hasMessage("Snapshot name '.' is reserved");
129+
assertThatThrownBy(() -> cfs.snapshot(".."))
130+
.isInstanceOf(IllegalArgumentException.class)
131+
.hasMessage("Snapshot name '..' is reserved");
132+
}
133+
}
134+
135+
private static String repeat(char c, int times)
136+
{
137+
char[] chars = new char[times];
138+
java.util.Arrays.fill(chars, c);
139+
return new String(chars);
140+
}
46141
}

0 commit comments

Comments
 (0)