[DRAFT PR] Critical code review for Inji Web Release 0.14.0#840
Merged
Prafulrakhade merged 0 commit intomasterfrom Sep 17, 2025
Merged
[DRAFT PR] Critical code review for Inji Web Release 0.14.0#840Prafulrakhade merged 0 commit intomasterfrom
Prafulrakhade merged 0 commit intomasterfrom
Conversation
| VelocityContext velocityContext = new VelocityContext(data); | ||
|
|
||
| StringWriter writer = new StringWriter(); | ||
| Velocity.evaluate(velocityContext, writer, "Credential Template", credentialTemplate); |
Check failure
Code scanning / CodeQL
Server-side template injection
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 months ago
To fix this vulnerability, ensure that template code provided to Velocity.evaluate is not attacker-controlled. The best-practice is to never accept templates from dynamic, user-facing APIs; use only trusted, static templates bundled with your application.
- In
Utilities.getCredentialSupportedTemplateString, restrict the loading of templates to only those available within a trusted directory in the classpath/package, or maintain a hardcoded whitelist of valid template filenames. - Never fetch template content over REST from a user-specified URL or based on user input. If dynamic templates are truly necessary, strictly enforce a whitelist.
- In
CredentialPDFGeneratorService.renderVCInCredentialTemplate, throw an exception or use a safe default if the template string comes from an unsafe source.
Steps to implement the fix:
- Edit
Utilities.getCredentialSupportedTemplateStringto remove fetching templates from REST API, and restrict template selection to secure local resources only (e.g., viaClassPathResourceor checked file path). - In local mode, retain the check against path traversal and only allow templates from the bounded, trusted directory.
- In all other cases, always return a safe default template.
Suggested changeset
2
src/main/java/io/mosip/mimoto/service/CredentialPDFGeneratorService.java
| @@ -202,6 +202,10 @@ | ||
|
|
||
| private ByteArrayInputStream renderVCInCredentialTemplate(Map<String, Object> data, String issuerId, String credentialConfigurationId) { | ||
| String credentialTemplate = utilities.getCredentialSupportedTemplateString(issuerId, credentialConfigurationId); | ||
| // Fail-fast if the template is null or unexpectedly empty | ||
| if (credentialTemplate == null || credentialTemplate.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Credential template is missing or invalid."); | ||
| } | ||
| Properties props = new Properties(); | ||
| props.setProperty("resource.loader", "class"); | ||
| props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
src/main/java/io/mosip/mimoto/util/Utilities.java
Outside changed files
| @@ -187,24 +187,21 @@ | ||
| } | ||
| public String getCredentialSupportedTemplateString(String issuerId, String credentialConfigurationId) { | ||
| String templateFileName = String.format("%s-%s-template.html", issuerId, credentialConfigurationId); | ||
| if(activeProfile.contains("local")) { | ||
| Path basePath = Paths.get("templates").toAbsolutePath().normalize(); | ||
| Path resolvedPath = basePath.resolve(templateFileName).normalize(); | ||
| Path basePath = Paths.get("templates").toAbsolutePath().normalize(); | ||
| Path resolvedPath = basePath.resolve(templateFileName).normalize(); | ||
|
|
||
| if (!resolvedPath.startsWith(basePath)) { | ||
| throw new SecurityException("Attempted path traversal attack: " + resolvedPath); | ||
| } | ||
| if (!resolvedPath.startsWith(basePath)) { | ||
| throw new SecurityException("Attempted path traversal attack: " + resolvedPath); | ||
| } | ||
|
|
||
| Resource credentialTemplateResource = new ClassPathResource(resolvedPath.toString()); | ||
| try { | ||
| return Files.readString(credentialTemplateResource.getFile().toPath()); | ||
| } catch (IOException e) { | ||
| log.error(ExceptionUtils.getStackTrace(e)); | ||
| } | ||
| return credentialTemplateHtmlString; | ||
| Resource credentialTemplateResource = new ClassPathResource(resolvedPath.toString()); | ||
| try { | ||
| return Files.readString(credentialTemplateResource.getFile().toPath()); | ||
| } catch (IOException e) { | ||
| log.error(ExceptionUtils.getStackTrace(e)); | ||
| } | ||
| String specificCredentialPDFTemplate = getJson("", templateFileName); | ||
| return !StringUtils.isEmpty(specificCredentialPDFTemplate)? specificCredentialPDFTemplate : getJson("", credentialTemplatePath); | ||
| // Return safe default template if not found | ||
| return credentialTemplateHtmlString; | ||
| } | ||
|
|
||
| public static String[] handleExceptionWithErrorCode(Exception exception, String flowErrorCode) { |
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
| <organizationUrl>https://github.com/mosip/mimoto</organizationUrl> | ||
| </developer> | ||
| </developers> | ||
|
|
Contributor
Author
There was a problem hiding this comment.
version should be 0.19.0-SNAPSHOT
Contributor
Author
There was a problem hiding this comment.
@mohanachandran-s api test version should be 0.19.0-SNAPSHOT
cc: @Gurpreet41082
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
src/main/java/io/mosip/mimoto/service/WalletLockStatusService.java
Outdated
Show resolved
Hide resolved
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
src/main/java/io/mosip/mimoto/service/impl/VcSdJwtCredentialFormatHandler.java
Outdated
Show resolved
Hide resolved
swatigoel
commented
Aug 20, 2025
src/main/java/io/mosip/mimoto/service/impl/VcSdJwtCredentialFormatHandler.java
Show resolved
Hide resolved
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
swatigoel
commented
Aug 20, 2025
5b0167b to
090badb
Compare
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.
No description provided.