-
Notifications
You must be signed in to change notification settings - Fork 0
Prevent users from manipulating comments (LEAN-5385) #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asouqi
wants to merge
15
commits into
master
Choose a base branch
from
LEAN-5385
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
400ee5c
Prevent users from manipulating comments (LEAN-5385)
asouqi c73a2f2
Merge branch 'master' into LEAN-5385
asouqi b0b14e8
Prevent users from manipulating comments (LEAN-5385)
asouqi 200cd1f
Merge remote-tracking branch 'origin/LEAN-5385' into LEAN-5385
asouqi 29bd14f
review update
asouqi 3c90741
Merge remote-tracking branch 'origin/master' into LEAN-5385
asouqi 12bcf28
review update
asouqi 4c67cb7
review update
asouqi 2069390
review update
asouqi aa7fb10
review update
asouqi c7393c4
Merge remote-tracking branch 'origin/master' into LEAN-5385
asouqi 51281e5
review updates
asouqi c3a800e
review updates
asouqi 40a7180
revert
asouqi 8b28659
review update
asouqi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /*! | ||
| * © 2026 Atypon Systems LLC | ||
| * | ||
| * Licensed 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 { | ||
| AccessContext, | ||
| getNodeAccessPolicy, | ||
| ManuscriptNode, | ||
| ExposedSlice, | ||
| } from '@manuscripts/transform' | ||
| import { AttrStep, ReplaceAroundStep, ReplaceStep, Step } from 'prosemirror-transform' | ||
|
|
||
| export class StepAccessService { | ||
| validate(step: Step, doc: ManuscriptNode, context: AccessContext) { | ||
| if (step instanceof ReplaceAroundStep) { | ||
| const gap = doc.slice(step.gapFrom, step.gapTo) | ||
| const slice = ( | ||
| step.slice as ExposedSlice<typeof step.slice, typeof step.slice.content> | ||
| ).insertAt(step.insert, gap.content) | ||
| return this.validateReplaceStep(new ReplaceStep(step.from, step.to, slice), doc, context) | ||
| } | ||
|
|
||
| if (step instanceof ReplaceStep) { | ||
| return this.validateReplaceStep(step, doc, context) | ||
| } | ||
|
|
||
| if (step instanceof AttrStep) { | ||
| return this.validateAttrStep(step, doc, context) | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| private validateReplaceStep(step: ReplaceStep, doc: ManuscriptNode, context: AccessContext) { | ||
| if (this.isStepUpdateNodeAttr(step, doc)) { | ||
| const node = step.slice.content.firstChild! | ||
| const nodeDB = doc.slice(step.from, step.to).content.firstChild! | ||
| return !this.findDiff(nodeDB, node).find((attr) => !this.attrPolicy(nodeDB, attr, context)) | ||
| } | ||
|
|
||
| let hasAccess = context.actions.editArticle | ||
|
|
||
| doc.slice(step.from, step.to).content.descendants((node) => { | ||
| const deletePolicy = getNodeAccessPolicy(node.type)?.delete | ||
| if (deletePolicy && !deletePolicy(node, context)) { | ||
| hasAccess = false | ||
| return false | ||
| } | ||
| }) | ||
|
|
||
| step.slice.content.descendants((node) => { | ||
| const insertPolicy = getNodeAccessPolicy(node.type)?.insert | ||
| if (insertPolicy && !insertPolicy(node, context)) { | ||
| hasAccess = false | ||
| return false | ||
| } | ||
| }) | ||
|
|
||
| return hasAccess | ||
| } | ||
|
|
||
| private validateAttrStep(step: AttrStep, doc: ManuscriptNode, context: AccessContext) { | ||
| const node = doc.nodeAt(step.pos) | ||
| return this.attrPolicy(node, step.attr, context) | ||
| } | ||
|
|
||
| private isStepUpdateNodeAttr(step: ReplaceStep, doc: ManuscriptNode) { | ||
| const stepContent = step.slice.content | ||
| const sliceContent = doc.slice(step.from, step.to).content | ||
| return ( | ||
| stepContent.size === sliceContent.size && | ||
| stepContent.childCount === 1 && | ||
| sliceContent.childCount === 1 && | ||
| stepContent.firstChild!.content.eq(sliceContent.firstChild!.content) | ||
| ) | ||
| } | ||
|
|
||
| private findDiff(nodeA: ManuscriptNode, nodeB: ManuscriptNode) { | ||
| const keys: string[] = [] | ||
| Object.entries(nodeB.attrs).map(([key, value]) => { | ||
| if (!nodeA.hasMarkup(nodeA.type, { ...nodeA.attrs, [key]: value })) { | ||
| keys.push(key) | ||
| } | ||
| }) | ||
| return keys | ||
| } | ||
|
|
||
| private attrPolicy(node: ManuscriptNode | null, attr: string, context: AccessContext) { | ||
| const policy = node?.type && getNodeAccessPolicy(node.type)?.attrs | ||
|
|
||
| if (policy) { | ||
| // we could have a policy that applied to all attribute changes | ||
|
mbartenev-atypon marked this conversation as resolved.
|
||
| if (typeof policy === 'function') { | ||
| return policy(node, context) | ||
| } else { | ||
| // apply policy per-attribute | ||
| return !!policy[attr]?.(node, context) | ||
| } | ||
| } | ||
|
|
||
| return context.actions.editMetadata | ||
| } | ||
| } | ||
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
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
115 changes: 115 additions & 0 deletions
115
test/suites/unit/DomainLayer/V2/StepAccessService.spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /*! | ||
| * © 2026 Atypon Systems LLC | ||
| * | ||
| * Licensed 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 { AccessContext, schema } from '@manuscripts/transform' | ||
| import { Transform } from 'prosemirror-transform' | ||
|
|
||
| import { DIContainer } from '../../../../../src/DIContainer/DIContainer' | ||
| import { TEST_TIMEOUT } from '../../../../utilities/testSetup' | ||
|
|
||
| jest.setTimeout(TEST_TIMEOUT) | ||
|
|
||
| let accessContext: AccessContext | ||
| let tr: Transform | ||
|
|
||
| beforeEach(async () => { | ||
| ;(DIContainer as any)._sharedContainer = null | ||
| await DIContainer.init() | ||
| accessContext = { | ||
| userId: 'MPUserProfile:01', | ||
| actions: { | ||
| handleSuggestion: true, | ||
| rejectOwnSuggestion: true, | ||
| handleOwnComments: true, | ||
| handleOthersComments: true, | ||
| resolveOwnComment: true, | ||
| resolveOthersComment: true, | ||
| createComment: true, | ||
| canEditFiles: true, | ||
| editArticle: true, | ||
| formatArticle: true, | ||
| editMetadata: true, | ||
| editCitationsAndRefs: true, | ||
| seeEditorToolbar: true, | ||
| seeReferencesButtons: true, | ||
| }, | ||
| } | ||
| const emptyDoc = schema.nodes.doc.createAndFill()! | ||
| tr = new Transform(emptyDoc) | ||
| tr.insert(10, schema.nodeFromJSON(comment)) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| const comment = { | ||
| type: 'comment', | ||
| attrs: { | ||
| id: 'MPCommentAnnotation:29D4335B', | ||
| contents: 'comment content', | ||
| target: 'MPParagraphElement:06D94BD3', | ||
| resolved: false, | ||
| userID: 'MPUserProfile:01', | ||
| originalText: '', | ||
| }, | ||
| } | ||
|
|
||
| describe('StepAccessService', () => { | ||
| describe('validate', () => { | ||
| it('has no access to resolve other comment', () => { | ||
| accessContext.userId = 'MPUserProfile:02' | ||
| accessContext.actions.resolveOthersComment = false | ||
| tr.setNodeMarkup(10, undefined, { ...comment.attrs, resolved: true }) | ||
| const hasAccessToStep = DIContainer.sharedContainer.stepAccessService.validate( | ||
| tr.steps[1], | ||
| tr.docs[1], | ||
| accessContext | ||
| ) | ||
| expect(hasAccessToStep).toEqual(false) | ||
| }) | ||
| it('has no access to resolve own comment', () => { | ||
| accessContext.actions.resolveOwnComment = false | ||
| tr.setNodeMarkup(10, undefined, { ...comment.attrs, resolved: true }) | ||
| const hasAccessToStep = DIContainer.sharedContainer.stepAccessService.validate( | ||
| tr.steps[1], | ||
| tr.docs[1], | ||
| accessContext | ||
| ) | ||
| expect(hasAccessToStep).toEqual(false) | ||
| }) | ||
| it('has no access to create a comment', () => { | ||
| accessContext.actions.createComment = false | ||
| tr.insert(10, schema.nodeFromJSON(comment)) | ||
| const hasAccessToStep = DIContainer.sharedContainer.stepAccessService.validate( | ||
| tr.steps[1], | ||
| tr.docs[1], | ||
| accessContext | ||
| ) | ||
| expect(hasAccessToStep).toEqual(false) | ||
| }) | ||
| it('has no access to delete a comment', () => { | ||
| accessContext.actions.handleOwnComments = false | ||
| tr.delete(10, 11) | ||
| const hasAccessToStep = DIContainer.sharedContainer.stepAccessService.validate( | ||
| tr.steps[1], | ||
| tr.docs[1], | ||
| accessContext | ||
| ) | ||
| expect(hasAccessToStep).toEqual(false) | ||
| }) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.