diff --git a/libs/common/src/vault/search/bitwarden-query-grammar.ne b/libs/common/src/vault/search/bitwarden-query-grammar.ne index e97170520dc..a855c1394fe 100644 --- a/libs/common/src/vault/search/bitwarden-query-grammar.ne +++ b/libs/common/src/vault/search/bitwarden-query-grammar.ne @@ -17,6 +17,7 @@ let lexer = moo.compile({ func_is: 'is:', // function parameter separator access: ':', + contains_string: /contains:(?:"(?:\\["\\]|[^\n"\\])*"|(?:\\["\\]|[^\s\(\):])+)/, // string match, includes quoted strings with escaped quotes and backslashes string: /(?:"(?:\\["\\]|[^\n"\\])*"|(?:\\["\\]|[^\s\(\):])+)/, }) @@ -27,7 +28,7 @@ let lexer = moo.compile({ search -> _ OR _ {% function(d) { return { type: 'search', d: d[1], start: d[1].start, end: d[1].end, length: d[1].length } } %} PARENTHESES -> %lparen _ OR _ %rparen {% function(d) { const start = d[0].offset; const end = d[4].offset; return { type: 'parentheses', inner: d[2], d:d, start, end, length: end - start + 1 } } %} - | TERM {% id %} + | EXPRESSION {% id %} AND -> AND _ %AND _ PARENTHESES {% function(d) { return { type: 'and', left: d[0], right: d[4], d:d, start: d[0].start, end: d[4].end, length: d[4].end - d[0].start + 1 } } %} | AND _ PARENTHESES {% function(d) { return { type: 'and', left: d[0], right: d[2], d:d, start: d[0].start, end: d[2].end, length: d[2].end - d[0].start + 1 }} %} @@ -36,11 +37,9 @@ AND -> AND _ %AND _ PARENTHESES {% function(d) { return { type: 'and', lef OR -> OR _ %OR _ AND {% function(d) { return { type: 'or', left: d[0], right: d[4], d:d, start: d[0].start, end: d[4].end, length: d[4].end - d[0].start + 1 } } %} | AND {% id %} -TERM -> - # naked string search term, search all fields - %string {% function(d) { const start = d[0].offset; const end = d[0].offset + d[0].value.length; return { type: 'term', value: d[0].value, d: d[0], start, end, length: d[0].value.length } } %} +EXPRESSION -> TERM # specified field search term - | %string %access %string {% function(d) { const start = d[0].offset; const end = d[2].offset + d[2].value.length; return { type: 'field term', field: d[0], term: d[2], d: d, start, end, length: end - start + 1 } } %} + | TERM %access TERM {% function(d) { const start = d[0].offset; const end = d[2].offset + d[2].value.length; return { type: 'field term', field: d[0], term: d[2], d: d, start, end, length: end - start + 1 } } %} # only items with attachments | %func_has "attachment" {% function(d) { const start = d[0].offset; const length = 14; return { type: 'hasAttachment', d: d, start, end: d[0].offset + length, length } } %} # only items with URIs @@ -50,14 +49,20 @@ TERM -> # only items assigned to a collection | %func_has "collection" {% function(d) { const start = d[0].offset; const length = 14; return { type: 'hasCollection', d:d, start, end: d[0].offset + length, length } } %} # only items assigned to a specified folder - | %func_in "folder" %access %string {% function(d) { const start = d[0].offset; const end = d[3].offset + d[3].value.length; return { type: 'inFolder', folder: d[3], d:d, start, end, length: end - start } } %} + | %func_in "folder" %access TERM {% function(d) { const start = d[0].offset; const end = d[3].offset + d[3].value.length; return { type: 'inFolder', folder: d[3], d:d, start, end, length: end - start } } %} # only items assigned to a specified collection - | %func_in "collection" %access %string {% function(d) { const start = d[0].offset; const end = d[3].offset + d[3].value.length; return { type: 'inCollection', collection: d[3], d:d, start, end, length: end - start + 1 } } %} + | %func_in "collection" %access TERM {% function(d) { const start = d[0].offset; const end = d[3].offset + d[3].value.length; return { type: 'inCollection', collection: d[3], d:d, start, end, length: end - start + 1 } } %} # only items assigned to a specified organization - | %func_in "org" %access %string {% function(d) { const start = d[0].offset; const end = d[3].offset + d[3].value.length; return { type: 'inOrg', org: d[3], d:d, start, end, length: end - start + 1 } } %} + | %func_in "org" %access TERM {% function(d) { const start = d[0].offset; const end = d[3].offset + d[3].value.length; return { type: 'inOrg', org: d[3], d:d, start, end, length: end - start + 1 } } %} # only items marked as favorites | %func_is "favorite" {% function(d) { const start = d[0].offset; const length = 11; return { type: 'isFavorite', d:d, start, end: d[0].offset + length, length } } %} # Boolean NOT operator | %NOT _ PARENTHESES {% function(d) { const start = d[0].offset; return { type: 'not', value: d[2], d:d, start, end: d[2].end, length: d[2].end - d[0].offset + 1 } } %} +TERM -> + # naked string search term, search all fields + %string {% function(d) { const start = d[0].offset; const end = d[0].offset + d[0].value.length; return { type: 'term', value: d[0].value.replace(/^"/,"").replace(/"$/,"").replace(/\"/,'"'), d: d[0], start, end, length: d[0].value.length } } %} + | %contains_string {% function(d) { const start = d[0].offset; const end = d[0].offset + d[0].value.length; return { type: 'term', value: d[0].value.replace(/^contains:"?/,"*").replace(/"?$/,"*").replace(/\"/,'"'), d: d[0], start, end, length: d[0].value.length } } %} + + _ -> %WS:* {% function(d) {return null } %} diff --git a/libs/common/src/vault/search/bitwarden-query-grammar.ts b/libs/common/src/vault/search/bitwarden-query-grammar.ts index 21ec050d6dd..7b7f671ed70 100644 --- a/libs/common/src/vault/search/bitwarden-query-grammar.ts +++ b/libs/common/src/vault/search/bitwarden-query-grammar.ts @@ -9,12 +9,13 @@ declare var lparen: any; declare var rparen: any; declare var AND: any; declare var OR: any; -declare var string: any; declare var access: any; declare var func_has: any; declare var func_in: any; declare var func_is: any; declare var NOT: any; +declare var string: any; +declare var contains_string: any; declare var WS: any; const moo = require("moo"); @@ -34,6 +35,7 @@ let lexer = moo.compile({ func_is: "is:", // function parameter separator access: ":", + contains_string: /contains:(?:"(?:\\["\\]|[^\n"\\])*"|(?:\\["\\]|[^\s\(\):])+)/, // string match, includes quoted strings with escaped quotes and backslashes string: /(?:"(?:\\["\\]|[^\n"\\])*"|(?:\\["\\]|[^\s\(\):])+)/, }); @@ -90,7 +92,7 @@ const grammar: Grammar = { return { type: "parentheses", inner: d[2], d: d, start, end, length: end - start + 1 }; }, }, - { name: "PARENTHESES", symbols: ["TERM"], postprocess: id }, + { name: "PARENTHESES", symbols: ["EXPRESSION"], postprocess: id }, { name: "AND", symbols: ["AND", "_", lexer.has("AND") ? { type: "AND" } : AND, "_", "PARENTHESES"], @@ -138,22 +140,10 @@ const grammar: Grammar = { }, }, { name: "OR", symbols: ["AND"], postprocess: id }, + { name: "EXPRESSION", symbols: ["TERM"] }, { - name: "TERM", - symbols: [lexer.has("string") ? { type: "string" } : string], - postprocess: function (d) { - const start = d[0].offset; - const end = d[0].offset + d[0].value.length; - return { type: "term", value: d[0].value, d: d[0], start, end, length: d[0].value.length }; - }, - }, - { - name: "TERM", - symbols: [ - lexer.has("string") ? { type: "string" } : string, - lexer.has("access") ? { type: "access" } : access, - lexer.has("string") ? { type: "string" } : string, - ], + name: "EXPRESSION", + symbols: ["TERM", lexer.has("access") ? { type: "access" } : access, "TERM"], postprocess: function (d) { const start = d[0].offset; const end = d[2].offset + d[2].value.length; @@ -169,7 +159,7 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [lexer.has("func_has") ? { type: "func_has" } : func_has, { literal: "attachment" }], postprocess: function (d) { const start = d[0].offset; @@ -178,7 +168,7 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [lexer.has("func_has") ? { type: "func_has" } : func_has, { literal: "uri" }], postprocess: function (d) { const start = d[0].offset; @@ -187,7 +177,7 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [lexer.has("func_has") ? { type: "func_has" } : func_has, { literal: "folder" }], postprocess: function (d) { const start = d[0].offset; @@ -196,7 +186,7 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [lexer.has("func_has") ? { type: "func_has" } : func_has, { literal: "collection" }], postprocess: function (d) { const start = d[0].offset; @@ -205,12 +195,12 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [ lexer.has("func_in") ? { type: "func_in" } : func_in, { literal: "folder" }, lexer.has("access") ? { type: "access" } : access, - lexer.has("string") ? { type: "string" } : string, + "TERM", ], postprocess: function (d) { const start = d[0].offset; @@ -219,12 +209,12 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [ lexer.has("func_in") ? { type: "func_in" } : func_in, { literal: "collection" }, lexer.has("access") ? { type: "access" } : access, - lexer.has("string") ? { type: "string" } : string, + "TERM", ], postprocess: function (d) { const start = d[0].offset; @@ -240,12 +230,12 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [ lexer.has("func_in") ? { type: "func_in" } : func_in, { literal: "org" }, lexer.has("access") ? { type: "access" } : access, - lexer.has("string") ? { type: "string" } : string, + "TERM", ], postprocess: function (d) { const start = d[0].offset; @@ -254,7 +244,7 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [lexer.has("func_is") ? { type: "func_is" } : func_is, { literal: "favorite" }], postprocess: function (d) { const start = d[0].offset; @@ -263,7 +253,7 @@ const grammar: Grammar = { }, }, { - name: "TERM", + name: "EXPRESSION", symbols: [lexer.has("NOT") ? { type: "NOT" } : NOT, "_", "PARENTHESES"], postprocess: function (d) { const start = d[0].offset; @@ -277,6 +267,41 @@ const grammar: Grammar = { }; }, }, + { + name: "TERM", + symbols: [lexer.has("string") ? { type: "string" } : string], + postprocess: function (d) { + const start = d[0].offset; + const end = d[0].offset + d[0].value.length; + return { + type: "term", + value: d[0].value.replace(/^"/, "").replace(/"$/, "").replace(/\"/, '"'), + d: d[0], + start, + end, + length: d[0].value.length, + }; + }, + }, + { + name: "TERM", + symbols: [lexer.has("contains_string") ? { type: "contains_string" } : contains_string], + postprocess: function (d) { + const start = d[0].offset; + const end = d[0].offset + d[0].value.length; + return { + type: "term", + value: d[0].value + .replace(/^contains:"?/, "*") + .replace(/"?$/, "*") + .replace(/\"/, '"'), + d: d[0], + start, + end, + length: d[0].value.length, + }; + }, + }, { name: "_$ebnf$1", symbols: [] }, { name: "_$ebnf$1",