Skip to content

Commit

Permalink
fix: only first \0 is removed in comments, only first \\ is escaped e…
Browse files Browse the repository at this point in the history
…tc. (#7532)
  • Loading branch information
AlexMesser committed Apr 3, 2021
1 parent daf3991 commit 36b14cb
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 14 deletions.
15 changes: 13 additions & 2 deletions src/driver/aurora-data-api/AuroraDataApiDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ export class AuroraDataApiDriver implements Driver {
// console.log("unsigned:", tableColumn.unsigned, columnMetadata.unsigned);
// console.log("asExpression:", tableColumn.asExpression, columnMetadata.asExpression);
// console.log("generatedType:", tableColumn.generatedType, columnMetadata.generatedType);
// console.log("comment:", tableColumn.comment, columnMetadata.comment);
// console.log("comment:", tableColumn.comment, this.escapeComment(columnMetadata.comment));
// console.log("default:", tableColumn.default, columnMetadata.default);
// console.log("enum:", tableColumn.enum, columnMetadata.enum);
// console.log("default changed:", !this.compareDefaultValues(this.normalizeDefault(columnMetadata), tableColumn.default));
Expand All @@ -745,7 +745,7 @@ export class AuroraDataApiDriver implements Driver {
|| tableColumn.unsigned !== columnMetadata.unsigned
|| tableColumn.asExpression !== columnMetadata.asExpression
|| tableColumn.generatedType !== columnMetadata.generatedType
// || tableColumn.comment !== columnMetadata.comment // todo
|| tableColumn.comment !== this.escapeComment(columnMetadata.comment)
|| !this.compareDefaultValues(this.normalizeDefault(columnMetadata), tableColumn.default)
|| (tableColumn.enum && columnMetadata.enum && !OrmUtils.isArraysEqual(tableColumn.enum, columnMetadata.enum.map(val => val + "")))
|| tableColumn.onUpdate !== columnMetadata.onUpdate
Expand Down Expand Up @@ -861,4 +861,15 @@ export class AuroraDataApiDriver implements Driver {
return columnMetadataValue === databaseValue;
}

/**
* Escapes a given comment.
*/
protected escapeComment(comment?: string) {
if (!comment) return comment;

comment = comment.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return comment;
}

}
4 changes: 2 additions & 2 deletions src/driver/aurora-data-api/AuroraDataApiQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,9 +1617,9 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu
}

comment = comment
.replace("\\", "\\\\") // MySQL allows escaping characters via backslashes
.replace(/\\/g, "\\\\") // MySQL allows escaping characters via backslashes
.replace(/'/g, "''")
.replace("\0", ""); // Null bytes aren't allowed in comments
.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return `'${comment}'`;
}
Expand Down
17 changes: 15 additions & 2 deletions src/driver/cockroachdb/CockroachDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ export class CockroachDriver implements Driver {
// console.log("width:", tableColumn.width, columnMetadata.width);
// console.log("precision:", tableColumn.precision, columnMetadata.precision);
// console.log("scale:", tableColumn.scale, columnMetadata.scale);
// console.log("comment:", tableColumn.comment, columnMetadata.comment);
// console.log("comment:", tableColumn.comment, this.escapeComment(columnMetadata.comment));
// console.log("default:", tableColumn.default, columnMetadata.default);
// console.log("default changed:", !this.compareDefaultValues(this.normalizeDefault(columnMetadata), tableColumn.default));
// console.log("isPrimary:", tableColumn.isPrimary, columnMetadata.isPrimary);
Expand All @@ -622,7 +622,7 @@ export class CockroachDriver implements Driver {
|| tableColumn.length !== columnMetadata.length
|| tableColumn.precision !== columnMetadata.precision
|| (columnMetadata.scale !== undefined && tableColumn.scale !== columnMetadata.scale)
|| tableColumn.comment !== columnMetadata.comment
|| tableColumn.comment !== this.escapeComment(columnMetadata.comment)
|| (!tableColumn.isGenerated && this.lowerDefaultValueIfNecessary(this.normalizeDefault(columnMetadata)) !== tableColumn.default) // we included check for generated here, because generated columns already can have default values
|| tableColumn.isPrimary !== columnMetadata.isPrimary
|| tableColumn.isNullable !== columnMetadata.isNullable
Expand Down Expand Up @@ -752,4 +752,17 @@ export class CockroachDriver implements Driver {
});
}

/**
* Escapes a given comment.
*/
protected escapeComment(comment?: string) {
if (!comment) return comment;

comment = comment
.replace(/'/g, "''")
.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return comment;
}

}
2 changes: 1 addition & 1 deletion src/driver/cockroachdb/CockroachQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner

comment = comment
.replace(/'/g, "''")
.replace("\0", ""); // Null bytes aren't allowed in comments
.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return `'${comment}'`;
}
Expand Down
15 changes: 13 additions & 2 deletions src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ export class MysqlDriver implements Driver {
|| tableColumn.unsigned !== columnMetadata.unsigned
|| tableColumn.asExpression !== columnMetadata.asExpression
|| tableColumn.generatedType !== columnMetadata.generatedType
|| tableColumn.comment !== columnMetadata.comment
|| tableColumn.comment !== this.escapeComment(columnMetadata.comment)
|| !this.compareDefaultValues(this.normalizeDefault(columnMetadata), tableColumn.default)
|| (tableColumn.enum && columnMetadata.enum && !OrmUtils.isArraysEqual(tableColumn.enum, columnMetadata.enum.map(val => val + "")))
|| tableColumn.onUpdate !== this.normalizeDatetimeFunction(columnMetadata.onUpdate)
Expand All @@ -774,7 +774,7 @@ export class MysqlDriver implements Driver {
// console.log("unsigned:", tableColumn.unsigned, columnMetadata.unsigned);
// console.log("asExpression:", tableColumn.asExpression, columnMetadata.asExpression);
// console.log("generatedType:", tableColumn.generatedType, columnMetadata.generatedType);
// console.log("comment:", tableColumn.comment, columnMetadata.comment);
// console.log("comment:", tableColumn.comment, this.escapeComment(columnMetadata.comment));
// console.log("default:", tableColumn.default, this.normalizeDefault(columnMetadata));
// console.log("enum:", tableColumn.enum, columnMetadata.enum);
// console.log("default changed:", !this.compareDefaultValues(this.normalizeDefault(columnMetadata), tableColumn.default));
Expand Down Expand Up @@ -959,4 +959,15 @@ export class MysqlDriver implements Driver {
}
}

/**
* Escapes a given comment.
*/
protected escapeComment(comment?: string) {
if (!comment) return comment;

comment = comment.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return comment;
}

}
4 changes: 2 additions & 2 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1817,9 +1817,9 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
}

comment = comment
.replace("\\", "\\\\") // MySQL allows escaping characters via backslashes
.replace(/\\/g, "\\\\") // MySQL allows escaping characters via backslashes
.replace(/'/g, "''")
.replace("\0", ""); // Null bytes aren't allowed in comments
.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return `'${comment}'`;
}
Expand Down
15 changes: 13 additions & 2 deletions src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ export class PostgresDriver implements Driver {
|| tableColumn.isArray !== columnMetadata.isArray
|| tableColumn.precision !== columnMetadata.precision
|| (columnMetadata.scale !== undefined && tableColumn.scale !== columnMetadata.scale)
|| tableColumn.comment !== columnMetadata.comment
|| tableColumn.comment !== this.escapeComment(columnMetadata.comment)
|| (!tableColumn.isGenerated && this.lowerDefaultValueIfNecessary(this.normalizeDefault(columnMetadata)) !== tableColumn.default) // we included check for generated here, because generated columns already can have default values
|| tableColumn.isPrimary !== columnMetadata.isPrimary
|| tableColumn.isNullable !== columnMetadata.isNullable
Expand All @@ -901,7 +901,7 @@ export class PostgresDriver implements Driver {
// console.log("isArray:", tableColumn.isArray, columnMetadata.isArray);
// console.log("precision:", tableColumn.precision, columnMetadata.precision);
// console.log("scale:", tableColumn.scale, columnMetadata.scale);
// console.log("comment:", tableColumn.comment, columnMetadata.comment);
// console.log("comment:", tableColumn.comment, this.escapeComment(columnMetadata.comment));
// console.log("enumName:", tableColumn.enumName, columnMetadata.enumName);
// console.log("enum:", tableColumn.enum && columnMetadata.enum && !OrmUtils.isArraysEqual(tableColumn.enum, columnMetadata.enum.map(val => val + "")));
// console.log("isPrimary:", tableColumn.isPrimary, columnMetadata.isPrimary);
Expand Down Expand Up @@ -1106,4 +1106,15 @@ export class PostgresDriver implements Driver {
return value
}

/**
* Escapes a given comment.
*/
protected escapeComment(comment?: string) {
if (!comment) return comment;

comment = comment.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return comment;
}

}
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner

comment = comment
.replace(/'/g, "''")
.replace("\0", ""); // Null bytes aren't allowed in comments
.replace(/\u0000/g, ""); // Null bytes aren't allowed in comments

return `'${comment}'`;
}
Expand Down
53 changes: 53 additions & 0 deletions test/github-issues/7521/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {Column, Entity, PrimaryGeneratedColumn} from "../../../../src";

@Entity()
export class Post {

@PrimaryGeneratedColumn()
id: number;

@Column({
comment: `E.g. 'foo', 'bar', or 'baz' etc.`
})
col1: string;

@Column({
comment: `E.g. '''foo, 'bar''', or baz' etc.`
})
col2: string;

@Column({
comment: `E.g. "foo", "bar", or "baz" etc.`
})
col3: string;

@Column({
comment: "foo\\bar, bar\\baz, foo\\\\baz"
})
col4: string;

@Column({
comment: "foo: \0, bar: \0\0\0"
})
col5: string;

@Column({
comment: `"foo", ""bar""`
})
col6: string;

@Column({
comment: "\"foo\", \"\"bar\"\""
})
col7: string;

@Column({
comment: "foo \r \n \b \t \Z \% \_ bar"
})
col8: string;

@Column({
comment: "foo \\r \\n \\b \\t \\Z \\% \\_ bar"
})
col9: string;
}
55 changes: 55 additions & 0 deletions test/github-issues/7521/issue-7521.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import "reflect-metadata";
import {Connection} from "../../../src";
import {createTestingConnections, closeTestingConnections} from "../../utils/test-utils";
import {Post} from "./entity/Post";

describe("github issues > #7521 Only first \0 is removed in comments, only first \\ is escaped etc.", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
enabledDrivers: ["postgres", "cockroachdb", "mysql"],
schemaCreate: false,
dropSchema: true,
entities: [Post],
}));
after(() => closeTestingConnections(connections));

it("should recognize model changes", () => Promise.all(connections.map(async connection => {
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.greaterThan(0);
sqlInMemory.downQueries.length.should.be.greaterThan(0);
})));

it("should not generate queries when no model changes", () => Promise.all(connections.map(async connection => {
await connection.driver.createSchemaBuilder().build();
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.equal(0);
sqlInMemory.downQueries.length.should.be.equal(0);
})));

it("should properly escape quotes in comments", () => Promise.all(connections.map(async connection => {
const queryRunner = connection.createQueryRunner();

let table = await queryRunner.getTable("post");
const col1 = table!.findColumnByName("col1")!;
const col2 = table!.findColumnByName("col2")!;
const col3 = table!.findColumnByName("col3")!;
const col4 = table!.findColumnByName("col4")!;
const col5 = table!.findColumnByName("col5")!;
const col6 = table!.findColumnByName("col6")!;
const col7 = table!.findColumnByName("col7")!;
const col8 = table!.findColumnByName("col8")!;
const col9 = table!.findColumnByName("col9")!;

col1.comment!.should.be.equal(`E.g. 'foo', 'bar', or 'baz' etc.`)
col2.comment!.should.be.equal(`E.g. '''foo, 'bar''', or baz' etc.`)
col3.comment!.should.be.equal(`E.g. "foo", "bar", or "baz" etc.`)
col4.comment!.should.be.equal("foo\\bar, bar\\baz, foo\\\\baz")
col5.comment!.should.be.equal(`foo: , bar: `)
col6.comment!.should.be.equal(`"foo", ""bar""`)
col7.comment!.should.be.equal("\"foo\", \"\"bar\"\"")
col8.comment!.should.be.equal("foo \r \n \b \t \Z \% \_ bar")
col9.comment!.should.be.equal("foo \\r \\n \\b \\t \\Z \\% \\_ bar")

await queryRunner.release()
})));
});

0 comments on commit 36b14cb

Please sign in to comment.