shahryarjb
Hello, I have read the Ecto query and I see this line:
You should be very careful when allowing user sent data to be used as part of LIKE query, since they allow to perform LIKE-injections.
and Im afraid of this line because I don’t know Postgress without Ecto and how can I prevent LIKE-injections, Do I need to sanitize the like input like use regex? or my sample code doesn’t need anything and it is right?
my code:
def search_codes(sub_brand_id, pagenumber, search_term) do
search_string = "%#{search_term}%"
query = from u in ErrorSchema,
join: c in assoc(u, :error_brands),
join: j in assoc(u, :error_sub_brands),
join: g in assoc(u, :error_categories),
where: u.status == true,
where: u.sub_brand_id == ^sub_brand_id,
where: ilike(u.title, ^search_string),
or_where: ilike(u.error_code, ^search_string),
order_by: [desc: u.inserted_at],
select: %{
id: u.id,
title: u.title,
short_description: u.short_description,
seo_alias_link: u.seo_alias_link,
brand_image: c.image,
brand_title: c.title,
brand_id: c.id,
category_title: g.title,
category_id: g.id,
model_title: j.title,
model_id: j.id
}
Repo.paginate(query, %{page: pagenumber, page_size: 20})
end
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
There’s a link in the docs describing the problem and an example solution in ruby, which basically does a regex replace of
/([\%_])/with\\1.1player
AFAIK the vulnerability lies not in an attacker being able to craft arbitrary SQL, since the input value is being quoted by Ecto, but some LIKE patterns can be quite heavy on the database and cause DoS if used maliciously.
Have a look here: LIKE injection - The GitHub Blog
In short, you might want to quote or strip the wildcard characters before passing them to Ecto.
shahryarjb
is there sample code with elixir ?, why do 2
/([\%_])/ with \\1regex exist ? regex has a input!!OvermindDL1
Plus in most cases
LIKEis the wrong tool for the job, postgresql has a fantastic language-knowing full-text search system that is easy to use and much better and faster than LIKE.shahryarjb
I wanted yo use full-text search, but Ecto has no function for this!! and I don’t know how to write postgress
OvermindDL1
Ah but Ecto does, that’s the
fragmentcall.Like I have a line in my code of
fragment("to_tsvector(?) @@ to_tsquery(?)", ...args...), pretty simple overall, and postgres’ docs on it are fantastic. If you need speed don’t forget to add an index for it.amnu3387
You can create a migration for adding a tsv field to your table, as in the following example:
This will create a tsv field and populate it from the existing field
full_nameand also index it. It then creates a trigger so that whenever you insert or update an existing record, it recomputes the ts vector.Then you can define a macro, e.g.:
I also have a helper function to split text input into a tsvector sequence, which also replaces invalid characters (only for my use case you might need a different set)
And I use it, similarly to:
shahryarjb
would you mind showing your schema and cast table for
add :full_name_tsv, :tsvector? pleasamnu3387
I don’t manipulate the tsv field at the app level so I just don’t cast it in the change sets, since I set the trigger at the db level to re-update the
full_name_tsv.For the field type in the schema definition you need to create a simple wrapper. In my case I don’t do anything at all with it.
And then,
field :full_name_tsv, TSVectorTypefabian
LIKE and the Postgres fulltext search have some important differences, you should carefully consider what you actually need. The fulltext search processes the text and e.g. by default removes the ending of words (stemming) and also removes certain special characters.
You can speed up LIKE queries with a trigram index, speed is not necessarily a reason to use the fulltext search. It really depends on what kind of searches you want to allow.