Potential fix for code scanning alert no. 4: Stored cross-site scripting #26
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.
Potential fix for https://github.com/in-tech-gration/website/security/code-scanning/4
General fix approach
When using data derived from file names to build URL attributes, ensure that the resulting URL cannot use dangerous schemes (
javascript:,data:, etc.) and cannot escape the intended path structure. The simplest way here is to sanitize or encodeslugbefore interpolating it into thehref, e.g., withencodeURIComponent, and to ensure it is treated purely as a path segment.Best fix for this code
We only need to change how
post.slugis used incomponents/blog-post-item.tsx. Currently:appears twice (around the image and around the title). We can avoid dangerous characters by URL-encoding the slug before interpolation:
and then:
This ensures that even if an attacker controls
post.slug, characters like:,/,",<,>etc. will be percent-encoded and cannot introduce a new scheme or break out of the attribute.encodeURIComponentis built-in in the JS runtime, so no new imports are required. Functionality is preserved from the user’s perspective as long as routes are set up consistently (which they should be if other parts of the app also treat slugs as encoded path segments).We do not modify any other files;
util/blog-utils.tscan continue to deriveslugfrom filenames, andBlogListcontinues to pass posts through unchanged.Specific changes
File:
components/blog-post-item.tsxsafeSlugconstant just after theformattedDatecomputation (or at the start of the component body after the null check).href={`/blog/${post.slug}`}withhref={`/blog/${safeSlug}`}.No new imports or helper methods are needed.
Suggested fixes powered by Copilot Autofix. Review carefully before merging.