-
Notifications
You must be signed in to change notification settings - Fork 1
fix: [AH-2866]: Deletion statements updated at all 3 registry,package and version level #89
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2024 Harness, Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Safely decodes HTML entities in a string without XSS vulnerability. | ||
| * Uses DOMParser which is safer than innerHTML as it doesn't execute scripts. | ||
| * | ||
| * @param text - The text containing HTML entities to decode | ||
| * @returns The decoded text with HTML entities converted to their character equivalents | ||
| * | ||
| * @example | ||
| * decodeHtmlEntities('github.com/rs/xid') // returns 'github.com/rs/xid' | ||
| * decodeHtmlEntities('<script>alert(1)</script>') // returns '<script>alert(1)</script>' (safe, no execution) | ||
| */ | ||
| export const decodeHtmlEntities = (text: string): string => { | ||
| if (!text || typeof text !== 'string') { | ||
| return text | ||
| } | ||
|
|
||
| // Use DOMParser which is safer than innerHTML | ||
| // It parses the content as text/html but doesn't execute scripts | ||
| const parser = new DOMParser() | ||
| const doc = parser.parseFromString(text, 'text/html') | ||
|
|
||
| // Extract the text content which automatically decodes entities | ||
| return doc.documentElement.textContent || text | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,22 +23,38 @@ import { useConfirmationDialog, useGetSpaceRef } from '@ar/hooks' | |||||||||||||||||||
| import { useStrings } from '@ar/frameworks/strings' | ||||||||||||||||||||
| import { encodeRef } from '@ar/hooks/useGetSpaceRef' | ||||||||||||||||||||
| import DeleteModalContent from '@ar/components/Form/DeleteModalContent' | ||||||||||||||||||||
| import { RepositoryPackageType } from '@ar/common/types' | ||||||||||||||||||||
| import { decodeHtmlEntities } from '@ar/common/utils' | ||||||||||||||||||||
|
|
||||||||||||||||||||
| interface useDeleteVersionModalProps { | ||||||||||||||||||||
| repoKey: string | ||||||||||||||||||||
| artifactKey: string | ||||||||||||||||||||
| versionKey: string | ||||||||||||||||||||
| artifactType?: ArtifactType | ||||||||||||||||||||
| packageType?: RepositoryPackageType | ||||||||||||||||||||
| digest?: string | ||||||||||||||||||||
| onSuccess: () => void | ||||||||||||||||||||
| } | ||||||||||||||||||||
| export default function useDeleteVersionModal(props: useDeleteVersionModalProps) { | ||||||||||||||||||||
| const { repoKey, onSuccess, artifactKey, versionKey, artifactType } = props | ||||||||||||||||||||
| const { repoKey, onSuccess, artifactKey, versionKey, artifactType, packageType, digest } = props | ||||||||||||||||||||
| const { getString } = useStrings() | ||||||||||||||||||||
| const { showSuccess, showError, clear } = useToaster() | ||||||||||||||||||||
| const spaceRef = useGetSpaceRef(repoKey) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const { mutateAsync: deleteVersion } = useDeleteArtifactVersionMutation() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // For OCI packages (Docker/Helm), use digest; for non-OCI packages, use version | ||||||||||||||||||||
| const isDockerPackage = packageType === RepositoryPackageType.DOCKER | ||||||||||||||||||||
| const isHelmPackage = packageType === RepositoryPackageType.HELM | ||||||||||||||||||||
| const isOCIPackage = isDockerPackage || isHelmPackage | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Determine which value to use for confirmation | ||||||||||||||||||||
| const shouldUseDigest = isOCIPackage && digest | ||||||||||||||||||||
| const confirmationValue = shouldUseDigest ? digest : versionKey | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Decode HTML entities to display properly | ||||||||||||||||||||
| const decodedConfirmationValue = decodeHtmlEntities(confirmationValue) | ||||||||||||||||||||
|
Comment on lines
+52
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: cat -n web/src/ar/pages/version-details/hooks/useDeleteVersionModal.tsx | head -100Repository: abhinavcode/harness Length of output: 4524 🏁 Script executed: find . -name "tsconfig.json" -o -name "tsconfig*.json" | head -5Repository: abhinavcode/harness Length of output: 172 🏁 Script executed: cat web/tsconfig.jsonRepository: abhinavcode/harness Length of output: 862
TypeScript does not narrow Use an inline condition so TypeScript can narrow 🛠️ Proposed fix- const shouldUseDigest = isOCIPackage && digest
- const confirmationValue = shouldUseDigest ? digest : versionKey
+ const confirmationValue = (isOCIPackage && digest) ? digest : versionKey📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| const handleDeleteVersion = async (): Promise<void> => { | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| const response = await deleteVersion({ | ||||||||||||||||||||
|
|
@@ -69,12 +85,13 @@ export default function useDeleteVersionModal(props: useDeleteVersionModalProps) | |||||||||||||||||||
| contentText: ( | ||||||||||||||||||||
| <DeleteModalContent | ||||||||||||||||||||
| entity="version" | ||||||||||||||||||||
| value={versionKey} | ||||||||||||||||||||
| value={decodedConfirmationValue} | ||||||||||||||||||||
| onSubmit={handleDeleteVersion} | ||||||||||||||||||||
| onClose={handleCloseDialog} | ||||||||||||||||||||
| content={getString('versionDetails.deleteVersionModal.contentText')} | ||||||||||||||||||||
| placeholder={getString('versionDetails.deleteVersionModal.inputPlaceholder')} | ||||||||||||||||||||
| inputLabel={getString('versionDetails.deleteVersionModal.inputLabel')} | ||||||||||||||||||||
| inputLabelValue={decodedConfirmationValue} | ||||||||||||||||||||
| /> | ||||||||||||||||||||
| ), | ||||||||||||||||||||
| customButtons: <></>, | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSDoc second example has an incorrect return value.
When
'<script>alert(1)</script>'is passed toDOMParser.parseFromString, the HTML tokenizer decodes the entities first, producing<script>alert(1)</script>, which the parser materialises as an actual<script>DOM element.doc.documentElement.textContentthen returns'alert(1)'— the text content of the script node — not'<script>alert(1)</script>'as the comment claims.The security note ("safe, no execution") is correct, but the stated return value is misleading. More broadly, any decoded string that forms valid HTML elements (e.g.
<b>text</b>) would have the tag markup stripped from the result, which could cause a display mismatch if package keys ever contain<,>encoded as HTML entities.📝 Corrected JSDoc example
📝 Committable suggestion
🤖 Prompt for AI Agents