Escape pipes in goal names (#2814)

* Escape pipes in goal names

* Apply suggestions from code review

Use ~S sigil for better readability

Co-authored-by: Vini Brasil <vini@hey.com>

---------

Co-authored-by: Vini Brasil <vini@hey.com>
bounce-rate-entry-page-poc
Uku Taht 4 years ago committed by GitHub
parent b646652071
commit 71e4d228c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,6 +8,7 @@ import PropBreakdown from './prop-breakdown'
import numberFormatter from '../../util/number-formatter'
import * as api from '../../api'
import * as url from '../../util/url'
import { escapeFilterValue } from '../../util/filters'
import LazyLoader from '../../components/lazy-loader'
const MOBILE_UPPER_WIDTH = 767
@ -79,7 +80,7 @@ export default class Conversions extends React.Component {
maxWidthDeduction={this.getBarMaxWidth()}
plot="unique_conversions"
>
<Link to={url.setQuery('goal', goal.name)} className="block px-2 py-1.5 hover:underline relative z-9 break-all lg:truncate dark:text-gray-200">{goal.name}</Link>
<Link to={url.setQuery('goal', escapeFilterValue(goal.name))} className="block px-2 py-1.5 hover:underline relative z-9 break-all lg:truncate dark:text-gray-200">{goal.name}</Link>
</Bar>
<div className="dark:text-gray-200">
<span className="inline-block w-20 font-medium text-right">{numberFormatter(goal.unique_conversions)}</span>

@ -33,7 +33,7 @@ try {
const ESCAPED_PIPE = '\\|'
function escapeFilterValue(value) {
export function escapeFilterValue(value) {
return value.replaceAll(NON_ESCAPED_PIPE_REGEX, ESCAPED_PIPE)
}

@ -57,7 +57,7 @@ defmodule Plausible.Stats.Filters do
{is_contains, val} = parse_contains_prefix(val)
is_list = Regex.match?(@non_escaped_pipe_regex, val)
is_wildcard = String.contains?(key, ["page", "goal"]) && String.match?(val, ~r/\*/)
val = if is_list, do: parse_member_list(val), else: val
val = if is_list, do: parse_member_list(val), else: remove_escape_chars(val)
val = if key == "goal", do: wrap_goal_value(val), else: val
cond do

@ -74,6 +74,18 @@ defmodule Plausible.Stats.FiltersTest do
end
end
describe "escaping pipe character" do
test "in simple is filter" do
%{"goal" => ~S(Foo \| Bar)}
|> assert_parsed(%{"event:goal" => {:is, {:event, "Foo | Bar"}}})
end
test "in member filter" do
%{"page" => ~S(/|\|)}
|> assert_parsed(%{"event:page" => {:member, ["/", "|"]}})
end
end
describe "is not filter type" do
test "simple is not filter" do
%{"page" => "!/"}

Loading…
Cancel
Save