Skip to content

Improve TypeORM model #19762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion javascript/ql/src/experimental/semmle/javascript/SQL.qll
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,42 @@ module ExperimentalSql {
override DataFlow::Node getAQueryArgument() { result = this.getArgument(0) }
}

/**
* A call to a TypeORM `Repository` (https://orkhan.gitbook.io/typeorm/docs/repository-api)
*/
private class RepositoryCall extends DatabaseAccess {
API::Node repository;

RepositoryCall() {
(
repository = API::moduleImport("typeorm").getMember("Repository").getInstance() or
repository = dataSource().getMember("getRepository").getReturn()
) and
this = repository.getMember(_).asSource()
}

override DataFlow::Node getAResult() {
result =
repository
.getMember([
"find", "findBy", "findOne", "findOneBy", "findOneOrFail", "findOneByOrFail",
"findAndCount", "findAndCountBy"
])
.getReturn()
.asSource()
}

override DataFlow::Node getAQueryArgument() {
result = repository.getMember("query").getParameter(0).asSink()
}
}

/** An expression that is passed to the `query` function and hence interpreted as SQL. */
class QueryString extends SQL::SqlString {
QueryString() {
this = any(QueryRunner qr).getAQueryArgument() or
this = any(QueryBuilderCall qb).getAQueryArgument()
this = any(QueryBuilderCall qb).getAQueryArgument() or
this = any(RepositoryCall rc).getAQueryArgument()
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions javascript/ql/test/experimental/TypeOrm/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,9 @@ AppDataSource.initialize().then(async () => {
qb.where(BadInput).orWhere(BadInput) // test: SQLInjectionPoint
}),
).getMany()

// Repository.query sink
await AppDataSource.getRepository(User2)
.query(BadInput) // test: SQLInjectionPoint

}).catch(error => console.log(error))
1 change: 1 addition & 0 deletions javascript/ql/test/experimental/TypeOrm/tests.expected
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ passingPositiveTests
| PASSED | SQLInjectionPoint | test.ts:210:28:210:53 | // test ... onPoint |
| PASSED | SQLInjectionPoint | test.ts:213:56:213:81 | // test ... onPoint |
| PASSED | SQLInjectionPoint | test.ts:217:56:217:81 | // test ... onPoint |
| PASSED | SQLInjectionPoint | test.ts:223:29:223:54 | // test ... onPoint |
failingPositiveTests