github-advanced-security[bot] commented on code in PR #3336: URL: https://github.com/apache/tinkerpop/pull/3336#discussion_r2968119499
########## gremlin-javascript/src/main/javascript/gremlin-javascript/lib/language/translator/GroovyTranslateVisitor.ts: ########## @@ -0,0 +1,295 @@ +/* + * 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. + */ + +// Context parameters are typed as `any` because the generated grammar files +// require accepting arbitrary context types at runtime. + +import TranslateVisitor from './TranslateVisitor.js'; + +/** + * Converts a Gremlin traversal string into a Groovy source code representation. + * Mirrors the Java GroovyTranslateVisitor. + */ +export default class GroovyTranslateVisitor extends TranslateVisitor { + + constructor(graphTraversalSourceName: string = 'g') { + super(graphTraversalSourceName); + } + + visitIntegerLiteral(ctx: any): void { + const integerLiteral: string = ctx.getText().toLowerCase(); + const lastChar = integerLiteral[integerLiteral.length - 1]; + + if (!/[a-z]/.test(lastChar)) { + this.sb.push(integerLiteral); + return; + } + + const num = integerLiteral.slice(0, -1); + switch (lastChar) { + case 'b': + this.sb.push('(byte)'); + this.sb.push(num); + break; + case 's': + this.sb.push('(short)'); + this.sb.push(num); + break; + case 'n': + // Groovy BigInteger uses the 'g' suffix + this.sb.push(num); + this.sb.push('g'); + break; + default: + // i and l suffixes and anything else kept as-is + this.sb.push(integerLiteral); + } + } + + visitFloatLiteral(ctx: any): void { + if (ctx.infLiteral() != null) { this.visit(ctx.infLiteral()); return; } + if (ctx.nanLiteral() != null) { this.visit(ctx.nanLiteral()); return; } + + const floatLiteral: string = ctx.getText().toLowerCase(); + const lastChar = floatLiteral[floatLiteral.length - 1]; + + if (!/[a-z]/.test(lastChar)) { + this.sb.push(floatLiteral); + return; + } + + const num = floatLiteral.slice(0, -1); + switch (lastChar) { + case 'f': + case 'd': + // keep f/d suffix as-is + this.sb.push(floatLiteral); + break; + case 'm': + // Groovy BigDecimal: if no decimal point use new BigDecimal(NL), else just the number + if (!floatLiteral.includes('.')) { + this.sb.push('new BigDecimal('); + this.sb.push(num); + this.sb.push('L)'); + } else { + this.sb.push(num); + } + break; + default: + this.sb.push(floatLiteral); + } + } + + visitInfLiteral(ctx: any): void { + if (ctx.SignedInfLiteral() != null && ctx.SignedInfLiteral().getText() === '-Infinity') { + this.sb.push('Double.NEGATIVE_INFINITY'); + } else { + this.sb.push('Double.POSITIVE_INFINITY'); + } + } + + visitUuidLiteral(ctx: any): void { + if (ctx.stringLiteral() == null) { + this.sb.push('UUID.randomUUID()'); + return; + } + this.sb.push('UUID.fromString('); + this.sb.push(ctx.stringLiteral().getText()); + this.sb.push(')'); + } + + visitNullLiteral(ctx: any): void { + if (ctx.parent?.constructor?.name === 'GenericMapNullableArgumentContext') { + this.sb.push('null as Map'); + } else { + this.sb.push(ctx.getText()); + } + } + + visitGenericSetLiteral(ctx: any): void { + this.sb.push('['); + const literals: any[] = ctx.genericLiteral(); + for (let i = 0; i < literals.length; i++) { + this.visit(literals[i]); + if (i < literals.length - 1) this.sb.push(', '); + } + this.sb.push('] as Set'); + } + + visitStringLiteral(ctx: any): void { + // Preserve original quote style and escape Groovy GString interpolation + this.sb.push(ctx.getText().replace(/\$/g, '\\$')); Review Comment: ## Incomplete string escaping or encoding This does not escape backslash characters in the input. [Show more details](https://github.com/apache/tinkerpop/security/code-scanning/15) ########## gremlin-javascript/src/main/javascript/gremlin-javascript/lib/language/translator/GroovyTranslateVisitor.ts: ########## @@ -0,0 +1,295 @@ +/* + * 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. + */ + +// Context parameters are typed as `any` because the generated grammar files +// require accepting arbitrary context types at runtime. + +import TranslateVisitor from './TranslateVisitor.js'; + +/** + * Converts a Gremlin traversal string into a Groovy source code representation. + * Mirrors the Java GroovyTranslateVisitor. + */ +export default class GroovyTranslateVisitor extends TranslateVisitor { + + constructor(graphTraversalSourceName: string = 'g') { + super(graphTraversalSourceName); + } + + visitIntegerLiteral(ctx: any): void { + const integerLiteral: string = ctx.getText().toLowerCase(); + const lastChar = integerLiteral[integerLiteral.length - 1]; + + if (!/[a-z]/.test(lastChar)) { + this.sb.push(integerLiteral); + return; + } + + const num = integerLiteral.slice(0, -1); + switch (lastChar) { + case 'b': + this.sb.push('(byte)'); + this.sb.push(num); + break; + case 's': + this.sb.push('(short)'); + this.sb.push(num); + break; + case 'n': + // Groovy BigInteger uses the 'g' suffix + this.sb.push(num); + this.sb.push('g'); + break; + default: + // i and l suffixes and anything else kept as-is + this.sb.push(integerLiteral); + } + } + + visitFloatLiteral(ctx: any): void { + if (ctx.infLiteral() != null) { this.visit(ctx.infLiteral()); return; } + if (ctx.nanLiteral() != null) { this.visit(ctx.nanLiteral()); return; } + + const floatLiteral: string = ctx.getText().toLowerCase(); + const lastChar = floatLiteral[floatLiteral.length - 1]; + + if (!/[a-z]/.test(lastChar)) { + this.sb.push(floatLiteral); + return; + } + + const num = floatLiteral.slice(0, -1); + switch (lastChar) { + case 'f': + case 'd': + // keep f/d suffix as-is + this.sb.push(floatLiteral); + break; + case 'm': + // Groovy BigDecimal: if no decimal point use new BigDecimal(NL), else just the number + if (!floatLiteral.includes('.')) { + this.sb.push('new BigDecimal('); + this.sb.push(num); + this.sb.push('L)'); + } else { + this.sb.push(num); + } + break; + default: + this.sb.push(floatLiteral); + } + } + + visitInfLiteral(ctx: any): void { + if (ctx.SignedInfLiteral() != null && ctx.SignedInfLiteral().getText() === '-Infinity') { + this.sb.push('Double.NEGATIVE_INFINITY'); + } else { + this.sb.push('Double.POSITIVE_INFINITY'); + } + } + + visitUuidLiteral(ctx: any): void { + if (ctx.stringLiteral() == null) { + this.sb.push('UUID.randomUUID()'); + return; + } + this.sb.push('UUID.fromString('); + this.sb.push(ctx.stringLiteral().getText()); + this.sb.push(')'); + } + + visitNullLiteral(ctx: any): void { + if (ctx.parent?.constructor?.name === 'GenericMapNullableArgumentContext') { + this.sb.push('null as Map'); + } else { + this.sb.push(ctx.getText()); + } + } + + visitGenericSetLiteral(ctx: any): void { + this.sb.push('['); + const literals: any[] = ctx.genericLiteral(); + for (let i = 0; i < literals.length; i++) { + this.visit(literals[i]); + if (i < literals.length - 1) this.sb.push(', '); + } + this.sb.push('] as Set'); + } + + visitStringLiteral(ctx: any): void { + // Preserve original quote style and escape Groovy GString interpolation + this.sb.push(ctx.getText().replace(/\$/g, '\\$')); + } + + visitStringNullableLiteral(ctx: any): void { + if (ctx.getText() === 'null') { + this.sb.push('null'); + } else { + // Preserve original quote style and escape Groovy GString interpolation + this.sb.push(ctx.getText().replace(/\$/g, '\\$')); Review Comment: ## Incomplete string escaping or encoding This does not escape backslash characters in the input. [Show more details](https://github.com/apache/tinkerpop/security/code-scanning/16) -- 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]
