FIX: validate n-gram size and reference text in PlagiarismScorer - #2972
Open
RohithPariki wants to merge 2 commits into
Open
RohithPariki wants to merge 2 commits into
RohithPariki wants to merge 2 commits into
Conversation
RohithPariki
force-pushed
the
fix/validate-plagiarism-scorer-inputs
branch
from
October 3, 2026 13:52
0b1d474 to
1cc3ddc
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2971
Problem
In
pyrit/score/float_scale/plagiarism_scorer.py:PlagiarismScoreraccepted non-positive integer values forn(such asn=0,n=-1, or floats like2.5, booleans likeFalse/True).n=0,_ngram_setreturned{()}becauserange(len(tokens) + 1)iterates over empty slicestokens[i:i]. ForPlagiarismMetric.JACCARD, the intersection and union of{()}with itself evaluate to identical sets, yielding1.0(100% plagiarism) for completely unrelated text.n < 0(e.g.n=-1), Python negative slicing produced spurious sets causing unintended overlap scores.nbypassed initialization and failed during scoring or generated invalid calculations (Falseacts as0).reference_textwas not validated during initialization. If initialized with an empty string"", whitespace-only" ", or strings without word tokens (e.g."!@#$%"),self._tokenize(reference_text)produces an empty list (reference_len=0), silently returning0.0for all candidate responses even if they are identical copies of the reference text.metricwas not validated at construction time to be an instance ofPlagiarismMetric.Root Cause
__init__inPlagiarismScorerdid not validatereference_text,metric, ornbefore assigning them._ngram_set(tokens, n)assumedn >= 1._plagiarism_scorerelied onreference_len == 0early return instead of enforcing valid reference tokens upon scorer instantiation.Solution
PlagiarismScorer.__init__:reference_textis a non-empty string and strips to ensure non-whitespace.reference_textwithself._tokenizeand ensures it contains at least one word token. RaisesValueErrorwith clear guidance otherwise.metricis an instance ofPlagiarismMetric.nis an integer (isinstance(n, int) and not isinstance(n, bool)) and>= 1. RaisesValueErrorotherwise._plagiarism_score:n >= 1and not a boolean.reference_text(empty, whitespace, non-string, punctuation-only / no word tokens).n(0, negative numbers, floats, booleans, non-integer types).nvalues (1,2,5,10).Testing
PlagiarismScorertest suites:python -m pytest tests/unit/score/test_plagiarism_scorer.py -q # 79 passedRisk
Very low. This strictly hardens input validation on scorer construction and helper boundaries against silent incorrect metrics or false-positive 1.0 scores without modifying the core similarity logic for valid inputs.