permify icon indicating copy to clipboard operation
permify copied to clipboard

fix(dsl): prevent lexer panic on string ending in trailing backslash

Open ManthanNimodiya opened this issue 5 months ago β€’ 3 comments

Bug

The schema DSL lexer panics with a slice-out-of-range error when lexing an unterminated string that ends in a trailing backslash. Minimal repro:

NewParser(""\").Parse() panic: runtime error: slice bounds out of range [:3] with length 2 pkg/dsl/lexer/lexer.go:231

Reached from the SchemaWrite API via Parse(); the gRPC recovery interceptor catches it, so it errors the request instead of crashing the server, but it's still an uncontrolled panic on untrusted input.

Root cause

In lexString, when a \ is the last character of the input, l.readChar() (skipping the backslash) sets l.ch = 0 but the code still executes position = l.position + 1, advancing position one past len(input). The loop then exits on l.ch == 0, and the final str += l.input[position:l.position] slices with a start index greater than the string length, panicking.

Fix

Detect l.ch == 0 immediately after skipping the backslash and break early with position = l.position, treating it as an unterminated string instead of continuing to advance past the input bounds.

Testing

Added a regression test in lexer_test.go asserting the lexer doesn't panic on ""\", and verified the exact reporter NewParser(""\").Parse()) now returns cleanly. Full pkg/dsl/... suite still passes.

Closes #3004

Summary by CodeRabbit

  • Bug Fixes

    • Fixed a crash in the DSL parser when processing string literals with unterminated escape sequences.
  • Tests

    • Added test coverage to verify the parser handles malformed strings gracefully without panicking.

ManthanNimodiya avatar Jun 19 '26 01:06 ManthanNimodiya

Review Change Stack

πŸ“ Walkthrough

Walkthrough

The fix adds an end-of-input guard inside lexString in the DSL lexer: when a backslash is encountered and l.ch is 0 (EOF), the lexer updates its position and exits the escape loop instead of attempting to parse a non-existent escape character. A Ginkgo regression test confirms no panic occurs on the two-byte input "\.

Changes

Lexer panic fix for trailing backslash

Layer / File(s) Summary
lexString EOF guard and regression test
pkg/dsl/lexer/lexer.go, pkg/dsl/lexer/lexer_test.go
lexString checks l.ch == 0 after a \ and breaks out of the escape loop instead of slicing past end-of-input; a Ginkgo test verifies the fix by iterating tokens on "\ until EOF and asserting no panic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

A backslash sat at the end of the line, No char came after β€” a panic by design! But the rabbit hopped in with a nil-check so neat, Now the lexer just shrugs and accepts the defeat. No more slice-out-of-range, no more fright β€” πŸ‡ Unterminated strings handled just right!

πŸš₯ Pre-merge checks | βœ… 5
βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title 'fix(dsl): prevent lexer panic on string ending in trailing backslash' clearly and specifically describes the main change: preventing a panic in the DSL lexer for strings with trailing backslashes.
Linked Issues check βœ… Passed The changes fully address issue #3004: the fix detects unterminated strings ending with a backslash and breaks early to prevent out-of-bounds slicing, while the regression test verifies the lexer no longer panics on the problematic input.
Out of Scope Changes check βœ… Passed All changes are directly scoped to fixing the reported bug: modifications to lexString function logic and addition of a regression test, with no extraneous changes detected.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jun 19 '26 01:06 coderabbitai[bot]

@omer-topal @ucatbas please have a look and lmk for any required changes

ManthanNimodiya avatar Jun 19 '26 01:06 ManthanNimodiya

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 74.69%. Comparing base (70b1110) to head (08a3dd0). :warning: Report is 26 commits behind head on master.

:x: Your project check has failed because the head coverage (74.69%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.

:exclamation: There is a different number of reports uploaded between BASE (70b1110) and HEAD (08a3dd0). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (70b1110) HEAD (08a3dd0)
3 1
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3007      +/-   ##
==========================================
- Coverage   82.58%   74.69%   -7.89%     
==========================================
  Files          74       83       +9     
  Lines        8300     9215     +915     
==========================================
+ Hits         6854     6882      +28     
- Misses        910     1798     +888     
+ Partials      536      535       -1     

:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jun 19 '26 01:06 codecov[bot]