Skip to content
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

Check for suspicious gradle-wrapper.jar #1440

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ New in 0.62.0:
- `autoApply`: Always automatically update the imports and package declarations.
- `preview`: Always preview the changes before applying.
- `prompt`: Ask user to confirm whether to bypass refactor preview.
* `java.imports.gradle.wrapper.checksums`: Defines allowed/disallowed SHA-256 checksums of Gradle Wrappers.

Semantic Highlighting
===============
Expand Down
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,32 @@
"description": "Specifies whether to update imports and package declarations when renaming files from File Explorer.",
"default": "prompt",
"scope": "window"
},
"java.imports.gradle.wrapper.checksums": {
"type": "array",
"items": {
"type": "object",
"default": {},
"required": [
"sha256"
],
"properties": {
"sha256": {
"type": "string",
"label": "SHA-256 checksum."
},
"allowed": {
"type": "boolean",
"default": true,
"label": "Is allowed?"
}
},
"additionalProperties": false,
"uniqueItems": true
},
"description": "Defines allowed/disallowed SHA-256 checksums of Gradle Wrappers",
"default": null,
"scope": "application"
}
}
},
Expand Down
9 changes: 8 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import * as refactorAction from './refactorAction';
import * as pasteAction from './pasteAction';
import * as net from 'net';
import { getJavaConfiguration, deleteDirectory } from './utils';
import { onConfigurationChange, excludeProjectSettingsFiles, getJavaServerMode, ServerMode } from './settings';
import { onConfigurationChange, excludeProjectSettingsFiles, getJavaServerMode, ServerMode, setGradleWrapperChecksum } from './settings';
import { logger, initializeLogFile } from './log';
import glob = require('glob');
import { SnippetCompletionProvider } from './snippetCompletionProvider';
Expand Down Expand Up @@ -132,6 +132,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {

enableJavadocSymbols();

const GRADLE_CHECKSUM = "gradle/checksum/prompt";
return requirements.resolveRequirements(context).catch(error => {
// show error
window.showErrorMessage(error.message, error.label).then((selection) => {
Expand Down Expand Up @@ -181,6 +182,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
moveRefactoringSupport: true,
clientHoverProvider: true,
clientDocumentSymbolProvider: true,
gradleChecksumWrapperPromptSupport: true
},
triggerFiles,
},
Expand Down Expand Up @@ -373,6 +375,10 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
commands.executeCommand(params.command, ...params.arguments);
});

context.subscriptions.push(commands.registerCommand(GRADLE_CHECKSUM, (wrapper: string, sha256: string) => {
setGradleWrapperChecksum(wrapper, sha256);
}));

context.subscriptions.push(commands.registerCommand(Commands.SHOW_JAVA_REFERENCES, (uri: string, position: LSPosition, locations: LSLocation[]) => {
commands.executeCommand(Commands.SHOW_REFERENCES, Uri.parse(uri), languageClient.protocol2CodeConverter.asPosition(position), locations.map(languageClient.protocol2CodeConverter.asLocation));
}));
Expand Down Expand Up @@ -631,6 +637,7 @@ function setProjectConfigurationUpdate(languageClient: LanguageClient, uri: Uri,
projectConfigurationUpdate(languageClient, uri);
}
}

function isJavaConfigFile(path: String) {
return path.endsWith('pom.xml') || path.endsWith('.gradle');
}
Expand Down
45 changes: 43 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const EXCLUDE_FILE_CONFIG = 'configuration.checkProjectSettingsExclusions';
export const ORGANIZE_IMPORTS_ON_PASTE = 'actionsOnPaste.organizeImports'; // java.actionsOnPaste.organizeImports

let oldConfig: WorkspaceConfiguration = getJavaConfiguration();
const gradleWrapperPromptDialogs = [];

export function onConfigurationChange(languageClient: LanguageClient, context: ExtensionContext) {
return workspace.onDidChangeConfiguration(params => {
Expand Down Expand Up @@ -123,13 +124,13 @@ export function getJavaEncoding(): string {
}

export async function checkJavaPreferences(context: ExtensionContext) {
const allow = 'Allow';
const disallow = 'Disallow';
let javaHome = workspace.getConfiguration().inspect<string>('java.home').workspaceValue;
let isVerified = javaHome === undefined || javaHome === null;
if (isVerified) {
javaHome = getJavaConfiguration().get('home');
}
const allow = 'Allow';
const disallow = 'Disallow';
const key = getKey(IS_WORKSPACE_JDK_ALLOWED, context.storagePath, javaHome);
const globalState = context.globalState;
if (!isVerified) {
Expand Down Expand Up @@ -204,3 +205,43 @@ export function getJavaServerMode(): ServerMode {
return workspace.getConfiguration().get('java.server.launchMode')
|| ServerMode.HYBRID;
}

export function setGradleWrapperChecksum(wrapper: string, sha256?: string) {
snjeza marked this conversation as resolved.
Show resolved Hide resolved
const opened = gradleWrapperPromptDialogs.filter(v => (v === sha256));
if (opened !== null && opened.length > 0) {
return;
}
gradleWrapperPromptDialogs.push(sha256);
const allow = 'Trust';
const disallow = 'Do not trust';
window.showErrorMessage(`"Security Warning! The gradle wrapper '${wrapper}'" [sha256 '${sha256}'] [could be malicious](https://github.com/redhat-developer/vscode-java/wiki/Gradle-Support#suspicious.wrapper). Should it be trusted?";`, disallow, allow)
.then(async selection => {
let allowed;
if (selection === allow) {
allowed = true;
} else if (selection === disallow) {
allowed = false;
} else {
unregisterGradleWrapperPromptDialog(sha256);
return false;
}
const key = "java.imports.gradle.wrapper.checksums";
let property: any = workspace.getConfiguration().inspect<string>(key).globalValue;
if (!Array.isArray(property)) {
property = [];
}
const entry = property.filter(p => (p.sha256 === sha256));
if (entry === null || entry.length === 0) {
property.push({ sha256: sha256, allowed: allowed });
workspace.getConfiguration().update(key, property, ConfigurationTarget.Global);
}
unregisterGradleWrapperPromptDialog(sha256);
});
}

function unregisterGradleWrapperPromptDialog(sha256: string) {
const index = gradleWrapperPromptDialogs.indexOf(sha256);
if (index > -1) {
gradleWrapperPromptDialogs.splice(index, 1);
}
}