github-advanced-security[bot] commented on code in PR #20507:
URL: https://github.com/apache/druid/pull/20507#discussion_r4211169858
##########
web-console/src/components/json-input/json-input.tsx:
##########
@@ -34,16 +34,29 @@
return Hjson.parse(str);
}
-export function extractRowColumnFromHjsonError(
- error: Error,
-): { row: number; column: number } | undefined {
+function lineColumnFromHjsonMessage(message: string): LineColumn | undefined {
// Message would be something like:
// `Found '}' where a key name was expected at line 26,7`
- // Use this to extract the row and column (subtract 1) and jump the cursor
to the right place on click
- const m = /line (\d+),(\d+)/.exec(error.message);
+ const m = /line (\d+),(\d+)/.exec(message);
if (!m) return;
- return { row: Number(m[1]) - 1, column: Number(m[2]) - 1 };
+ return { line: Number(m[1]), column: Number(m[2]) };
+}
+
+export function extractLineColumnFromHjsonError(error: Error): LineColumn |
undefined {
+ // Use this to extract the line and column and jump the cursor to the right
place on click
+ return lineColumnFromHjsonMessage(error.message);
+}
+
+/**
+ * The error to mark in the editor for an Hjson error message, undefined if
the message has no position
+ */
+export function getHjsonEditorError(message: string): EditorError | undefined {
+ const position = lineColumnFromHjsonMessage(message);
+ if (!position) return;
+
+ // The mark shows where the error is, so the position (and the text after
it, following ">>>") is left out
+ return { position, message: message.replace(/\s+at line \d+,\d+[\s\S]*$/,
'') };
Review Comment:
## CodeQL / Polynomial regular expression used on uncontrolled data
This [regular expression](1) that depends on [library input](2) may run slow
on strings with many repetitions of ' '.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12069)
##########
web-console/src/editor-languages/editor-languages.spec.ts:
##########
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { CompletionResult, CompletionSource } from
'@codemirror/autocomplete';
+import { CompletionContext } from '@codemirror/autocomplete';
+import type { Language } from '@codemirror/language';
+import type { Extension } from '@codemirror/state';
+import { EditorState } from '@codemirror/state';
+import { highlightTree, tags } from '@lezer/highlight';
+
+import { codeEditorHighlightStyle } from
'../components/code-editor/code-editor-theme';
+
+import { dsql, getDsqlLanguage } from './dsql';
+import { hjson, hjsonLanguage } from './hjson';
+
+const dsqlLanguage = getDsqlLanguage();
+
+function tokenize(language: Language, text: string): [type: string, value:
string][] {
+ const tokens: [string, string][] = [];
+ language.parser.parse(text).iterate({
+ enter(node) {
+ if (node.type.isTop) return;
+ tokens.push([node.name, text.slice(node.from, node.to)]);
+ },
+ });
+ return tokens;
+}
+
+function tokenOf(language: Language, text: string, value: string): string |
undefined {
+ return tokenize(language, text).find(t => t[1] === value)?.[0];
+}
+
+describe('editor languages', () => {
+ it('highlights DruidSQL', () => {
+ const sql = `--:ISSUE: bad
+SELECT COUNT(*), "col", CAST(x AS VARCHAR), TRUE FROM t WHERE y <> 'lit' AND z
= 3.5 -- comment
+/* multi
+line */ SELECT`;
+ expect(tokenOf(dsqlLanguage, sql, '--:ISSUE: bad')).toEqual('Issue');
+ expect(tokenOf(dsqlLanguage, sql, 'SELECT')).toEqual('Keyword');
+ expect(tokenOf(dsqlLanguage, sql, 'COUNT')).toEqual('FunctionName');
+ expect(tokenOf(dsqlLanguage, sql, '"col"')).toEqual('QuotedIdentifier');
+ expect(tokenOf(dsqlLanguage, sql, 'VARCHAR')).toEqual('TypeName');
+ expect(tokenOf(dsqlLanguage, sql, 'TRUE')).toEqual('Constant');
+ expect(tokenOf(dsqlLanguage, sql, "'lit'")).toEqual('String');
+ expect(tokenOf(dsqlLanguage, sql, '3.5')).toEqual('Number');
+ expect(tokenOf(dsqlLanguage, sql, '<')).toEqual('Operator');
+ expect(tokenOf(dsqlLanguage, sql, '-- comment')).toEqual('LineComment');
+ expect(tokenOf(dsqlLanguage, sql, '/* multi\nline
*/')).toEqual('BlockComment');
+ expect(tokenOf(dsqlLanguage, sql, 'x')).toEqual('Identifier');
+ expect(tokenOf(dsqlLanguage, sql, '(x AS VARCHAR)')).toEqual('Parens');
+ });
+
+ it('highlights unterminated DruidSQL tokens', () => {
+ expect(tokenOf(dsqlLanguage, 'SELECT /* not closed', '/* not
closed')).toEqual('BlockComment');
+ // An unterminated literal or quoted identifier lasts until the end of the
line
+ expect(tokenize(dsqlLanguage, `SELECT 'x AND y\nFROM "t`)).toEqual([
+ ['Keyword', 'SELECT'],
+ ['String', "'x AND y"],
+ ['Keyword', 'FROM'],
+ ['QuotedIdentifier', '"t'],
+ ]);
+ // A sign is an operator
+ expect(tokenize(dsqlLanguage, 'SELECT a-1')).toEqual([
+ ['Keyword', 'SELECT'],
+ ['Identifier', 'a'],
+ ['Operator', '-'],
+ ['Number', '1'],
+ ]);
+ });
+
+ it('highlights the available functions', () => {
+ const availableSqlFunctions = new Map([['MY_FN', { args: ['x'],
isAggregate: false }]]);
+ expect(tokenOf(dsqlLanguage, 'SELECT MY_FN(x)',
'MY_FN')).toEqual('Identifier');
+ expect(tokenOf(getDsqlLanguage(availableSqlFunctions), 'SELECT MY_FN(x)',
'MY_FN')).toEqual(
+ 'FunctionName',
+ );
+ expect(tokenOf(getDsqlLanguage(availableSqlFunctions), 'SELECT my_fn(x)',
'my_fn')).toEqual(
+ 'FunctionName',
+ );
+
expect(getDsqlLanguage(availableSqlFunctions)).toBe(getDsqlLanguage(availableSqlFunctions));
+ });
+
+ it('highlights Hjson', () => {
+ const hjson = `{
+ # comment
+ key: "value\\n"
+ n: -1.5e3
+ flag: true
+ list: [1, "two"]
+ unquoted: some text
+ multi: '''
+ lines
+ '''
+}`;
+ expect(tokenOf(hjsonLanguage, hjson, '# comment')).toEqual('LineComment');
+ expect(tokenOf(hjsonLanguage, hjson, 'key')).toEqual('PropertyName');
+ expect(tokenOf(hjsonLanguage, hjson, '"value\\n"')).toEqual('String');
+ expect(tokenOf(hjsonLanguage, hjson, '\\n')).toEqual('Escape');
+ expect(tokenOf(hjsonLanguage, hjson, '-1.5e3')).toEqual('Number');
+ expect(tokenOf(hjsonLanguage, hjson, 'true')).toEqual('True');
+ expect(tokenOf(hjsonLanguage, hjson, '[1, "two"]')).toEqual('Array');
+ expect(tokenOf(hjsonLanguage, hjson, '"two"')).toEqual('String');
+ expect(tokenOf(hjsonLanguage, hjson, 'some
text')).toEqual('QuotelessString');
+ expect(tokenOf(hjsonLanguage, hjson, "'''\n lines\n
'''")).toEqual('MultilineString');
+ });
+
+ it('ends an unterminated Hjson string at the end of the line', () => {
+ const hjson = '{\n "a": "not closed\n "b": 1\n}';
+ expect(tokenOf(hjsonLanguage, hjson, '"not closed\n')).toEqual('String');
+ expect(tokenOf(hjsonLanguage, hjson, '"b"')).toEqual('PropertyName');
+ expect(tokenOf(hjsonLanguage, hjson, '1')).toEqual('Number');
+ });
+
+ it('highlights JSON', () => {
+ const json = `{"auditTime": "2023-07-31T18:15:19.302Z", "url":
"http://x:80", "n": 1, "z": null}`;
+ expect(tokenOf(hjsonLanguage, json,
'"auditTime"')).toEqual('PropertyName');
+ expect(tokenOf(hjsonLanguage, json,
'"2023-07-31T18:15:19.302Z"')).toEqual('String');
+ expect(tokenOf(hjsonLanguage, json, '"http://x:80"')).toEqual('String');
+ expect(tokenOf(hjsonLanguage, json, '1')).toEqual('Number');
+ expect(tokenOf(hjsonLanguage, json, 'null')).toEqual('Null');
+ expect(tokenize(hjsonLanguage, json).some(([type]) => type ===
'⚠')).toBe(false);
+ });
+
+ it('highlights quoteless Hjson values as a whole', () => {
+ const hjson = `ip: 127.0.0.1
+time: 2023-07-31T18:15:19.302Z
+host: localhost:8091
+url: http://example.com
+count: 3
+count2: 3 apples
+flag: true, other: 1
+page: Talk:Main`;
+ for (const value of [
+ '127.0.0.1',
+ '2023-07-31T18:15:19.302Z',
+ 'localhost:8091',
+ 'http://example.com',
+ '3 apples',
+ 'Talk:Main',
+ ]) {
+ expect(tokenOf(hjsonLanguage, hjson, value)).toEqual('QuotelessString');
+ }
+ expect(tokenOf(hjsonLanguage, hjson, '3')).toEqual('Number');
+ expect(tokenOf(hjsonLanguage, hjson, 'true')).toEqual('True');
+ });
+
+ it('highlights Hjson without the root braces', () => {
+ const hjson = `queryType: scan
+dataSource: wikipedia`;
+ expect(tokenOf(hjsonLanguage, hjson, 'queryType')).toEqual('PropertyName');
+ expect(tokenOf(hjsonLanguage, hjson,
'dataSource')).toEqual('PropertyName');
+ expect(tokenOf(hjsonLanguage, hjson,
'wikipedia')).toEqual('QuotelessString');
+ expect(tokenOf(hjsonLanguage, 'just text', 'just
text')).toEqual('QuotelessString');
+ });
+
+ it('styles Hjson keys like keywords and leaves the literals plain', () => {
+ const json = '{"key": null, "flag": true, "s": "x"}';
+ const classes = new Map<string, string>();
+ highlightTree(hjsonLanguage.parser.parse(json), codeEditorHighlightStyle,
(from, to, cls) => {
+ classes.set(json.slice(from, to), cls);
+ });
+
expect(classes.get('"key"')).toEqual(codeEditorHighlightStyle.style([tags.keyword]));
+
expect(classes.get('"x"')).toEqual(codeEditorHighlightStyle.style([tags.string]));
+ expect(classes.has('null')).toBe(false);
+ expect(classes.has('true')).toBe(false);
+ });
+
+ it('highlights a key that is being typed', () => {
+ expect(tokenize(hjsonLanguage, '{\n "a": 1\n
que')).toContainEqual(['PropertyName', 'que']);
+ });
+
+ describe('completions', () => {
+ // Completes at the end of the text (marked with a |) the way the editor
would
+ function complete(
+ extension: Extension,
+ textWithCursor: string,
+ readOnly = false,
+ ): string[] | undefined {
+ const pos = textWithCursor.indexOf('|');
+ const state = EditorState.create({
+ doc: textWithCursor.replace('|', ''),
Review Comment:
## CodeQL / Incomplete string escaping or encoding
This replaces only the first occurrence of '|'.
[Show more
details](https://github.com/apache/druid/security/code-scanning/12070)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]