Skip to content

Commit c4e1e2c

Browse files
CASSANDRA-21046 Schema annotations escape validation on CREATE and ALTER DDL statements
Patch by Jyothsna Konisa; Reviewed by Sam Tunnicliffe, Yifan Cai, Stefan Miklosovic for CASSANDRA-21046
1 parent 2b5124d commit c4e1e2c

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
5.1
2+
* Schema annotations escape validation on CREATE and ALTER DDL statements (CASSANDRA-21046)
23
* Calculate once and cache the result of ModificationStatement#requiresRead as a perf optimization (CASSANDRA-21040)
34
* Update system schema tables with new distributed keyspace on upgrade (CASSANDRA-20872)
45
* Fix issue when running cms reconfiguration with paxos repair disabled (CASSANDRA-20869)

src/java/org/apache/cassandra/cql3/statements/schema/CopyTableStatement.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ public Keyspaces apply(ClusterMetadata metadata)
234234
public void validate(ClientState state)
235235
{
236236
super.validate(state);
237+
// validate attributes to avoid silently accepting following statements
238+
// create table ... like ... with security_label='xxx';
239+
attrs.validate();
237240

238241
// If a memtable configuration is specified, validate it against config
239242
if (attrs.hasOption(TableParams.Option.MEMTABLE))

src/java/org/apache/cassandra/schema/KeyspaceParams.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public enum Option
5959
{
6060
DURABLE_WRITES,
6161
REPLICATION,
62-
FAST_PATH,
63-
COMMENT,
64-
SECURITY_LABEL;
62+
FAST_PATH;
6563

6664
@Override
6765
public String toString()
@@ -181,8 +179,8 @@ public String toString()
181179
.add(Option.DURABLE_WRITES.toString(), durableWrites)
182180
.add(Option.REPLICATION.toString(), replication)
183181
.add(Option.FAST_PATH.toString(), fastPath.toString())
184-
.add(Option.COMMENT.toString(), comment)
185-
.add(Option.SECURITY_LABEL.toString(), securityLabel)
182+
.add("COMMENT", comment)
183+
.add("SECURITY_LABEL", securityLabel)
186184
.toString();
187185
}
188186

src/java/org/apache/cassandra/schema/TableParams.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import static org.apache.cassandra.schema.TableParams.Option.MIN_INDEX_INTERVAL;
7171
import static org.apache.cassandra.schema.TableParams.Option.PENDING_DROP;
7272
import static org.apache.cassandra.schema.TableParams.Option.READ_REPAIR;
73-
import static org.apache.cassandra.schema.TableParams.Option.SECURITY_LABEL;
7473
import static org.apache.cassandra.schema.TableParams.Option.SPECULATIVE_RETRY;
7574
import static org.apache.cassandra.utils.LocalizeString.toLowerCaseLocalized;
7675

@@ -102,8 +101,7 @@ public enum Option
102101
TRANSACTIONAL_MODE,
103102
TRANSACTIONAL_MIGRATION_FROM,
104103
PENDING_DROP,
105-
AUTO_REPAIR,
106-
SECURITY_LABEL;
104+
AUTO_REPAIR;
107105

108106
@Override
109107
public String toString()
@@ -342,7 +340,7 @@ public String toString()
342340
{
343341
return MoreObjects.toStringHelper(this)
344342
.add(COMMENT.toString(), comment)
345-
.add(SECURITY_LABEL.toString(), securityLabel)
343+
.add("SECURITY_LABEL", securityLabel)
346344
.add(ADDITIONAL_WRITE_POLICY.toString(), additionalWritePolicy)
347345
.add(ALLOW_AUTO_SNAPSHOT.toString(), allowAutoSnapshot)
348346
.add(BLOOM_FILTER_FP_CHANCE.toString(), bloomFilterFpChance)

test/unit/org/apache/cassandra/cql3/validation/miscellaneous/CommentAndSecurityLabelTest.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,79 @@ public void testEmptyStringRejection()
373373
assertInvalidMessage("Cannot set security label to empty string", buildSecurityLabelStatement(ObjectType.FIELD, fieldRef, ""));
374374
}
375375

376+
@Test
377+
public void testCommentAndSecurityLabelNotAllowedInCreateKeyspace()
378+
{
379+
// Test that comment property is rejected in CREATE KEYSPACE WITH clause
380+
String createKsWithComment = "CREATE KEYSPACE ks_test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND comment = 'test comment'";
381+
assertInvalidMessage("Unknown property 'comment'", createKsWithComment);
382+
383+
// Test that security_label property is rejected in CREATE KEYSPACE WITH clause
384+
String createKsWithLabel = "CREATE KEYSPACE ks_test2 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND security_label = 'TEST_LABEL'";
385+
assertInvalidMessage("Unknown property 'security_label'", createKsWithLabel);
386+
}
387+
388+
@Test
389+
public void testCommentAndSecurityLabelNotAllowedInAlterKeyspace()
390+
{
391+
createKeyspaceWithName("ks_alter_test");
392+
393+
// Test that comment property is rejected in ALTER KEYSPACE WITH clause
394+
String alterKsWithComment = "ALTER KEYSPACE ks_alter_test WITH comment = 'test comment'";
395+
assertInvalidMessage("Unknown property 'comment'", alterKsWithComment);
396+
397+
// Test that security_label property is rejected in ALTER KEYSPACE WITH clause
398+
String alterKsWithLabel = "ALTER KEYSPACE ks_alter_test WITH security_label = 'TEST_LABEL'";
399+
assertInvalidMessage("Unknown property 'security_label'", alterKsWithLabel);
400+
}
401+
402+
@Test
403+
public void testSecurityLabelNotAllowedInCreateTable()
404+
{
405+
createKeyspaceWithName("ks_table_test");
406+
407+
// Test that security_label property is rejected in CREATE TABLE WITH clause
408+
String createTableWithLabel = "CREATE TABLE ks_table_test.t1 (id int PRIMARY KEY, name text) WITH security_label = 'TEST_LABEL'";
409+
assertInvalidMessage("Unknown property 'security_label'", createTableWithLabel);
410+
411+
// Verify that comment IS allowed in CREATE TABLE for backward compatibility
412+
String createTableWithComment = "CREATE TABLE ks_table_test.t2 (id int PRIMARY KEY, name text) WITH comment = 'test comment'";
413+
execute(createTableWithComment);
414+
assertComment(ObjectType.TABLE, "ks_table_test", "ks_table_test.t2", "test comment");
415+
}
416+
417+
@Test
418+
public void testSecurityLabelNotAllowedInAlterTable()
419+
{
420+
createKeyspaceWithName("ks_alter_table_test");
421+
createTableWithName("ks_alter_table_test", "t1");
422+
423+
// Test that security_label property is rejected in ALTER TABLE WITH clause
424+
String alterTableWithLabel = "ALTER TABLE ks_alter_table_test.t1 WITH security_label = 'TEST_LABEL'";
425+
assertInvalidMessage("Unknown property 'security_label'", alterTableWithLabel);
426+
427+
// Verify that comment IS allowed in ALTER TABLE for backward compatibility
428+
String alterTableWithComment = "ALTER TABLE ks_alter_table_test.t1 WITH comment = 'test comment'";
429+
execute(alterTableWithComment);
430+
assertComment(ObjectType.TABLE, "ks_alter_table_test", "ks_alter_table_test.t1", "test comment");
431+
}
432+
433+
@Test
434+
public void testSecurityLabelNotAllowedInCreateTableLike()
435+
{
436+
createKeyspaceWithName("ks_like_test");
437+
createTableWithName("ks_like_test", "source_table");
438+
439+
// Test that security_label property is rejected in CREATE TABLE ... LIKE ... WITH clause
440+
String createTableLikeWithLabel = "CREATE TABLE ks_like_test.target_table LIKE ks_like_test.source_table WITH security_label = 'TEST_LABEL'";
441+
assertInvalidMessage("Unknown property 'security_label'", createTableLikeWithLabel);
442+
443+
// Verify that comment IS allowed in CREATE TABLE ... LIKE for backward compatibility
444+
String createTableLikeWithComment = "CREATE TABLE ks_like_test.target_table2 LIKE ks_like_test.source_table WITH comment = 'test comment'";
445+
execute(createTableLikeWithComment);
446+
assertComment(ObjectType.TABLE, "ks_like_test", "ks_like_test.target_table2", "test comment");
447+
}
448+
376449
// Helper methods for setting comments and security labels
377450
private void setComment(ObjectType type, String objectName, String comment)
378451
{

0 commit comments

Comments
 (0)