-
Notifications
You must be signed in to change notification settings - Fork 172
Java upload tab #2945
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
Java upload tab #2945
Conversation
Pull Request Test Coverage Report for Build 9049753998Details
💛 - Coveralls |
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.
Will this be safe (considering one can upload any file)? Any potential social engineering attack vector? Is it possible to access the JS environment from Java (and e.g. steal browser cookies)?
At the moment most of the standard library is not yet implemented, there are no native functions that access network. Ideally we only need the run tab, but due to the limitations of the type checker and compiler this was requested as a feature. |
Yes, maybe this feature should only be available in the command line tool and not in sourceacademy.org? I agree that for now it's ok. Maybe we include it for the project presentation next week and then remove it? |
I am planning to use localhost for presentation. I think mei tin is using the localhost approach as well. Is there a way we only enable it for dev and not production? do we have feature flags? |
Not sure, does
No we don't (at least not yet for now…) |
looks like it does, added the check. |
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.
Hi, thanks for working on this! I have some comments below:
class SideContentUpload extends React.Component<OwnProps> { | ||
public render() { | ||
return ( | ||
<div> | ||
<p> | ||
Bypass the compiler and type checker by uploading class files to run in the JVM. | ||
<br /> | ||
<br /> | ||
Only .class files are accepted. Code in the editor will be ignored when running while this | ||
tab is active. | ||
<br /> | ||
<br /> | ||
Compile the files with the following command: | ||
<code>javac *.java -target 8 -source 8</code> | ||
<br /> | ||
<br /> | ||
Avoid running class files downloaded from unknown sources. | ||
<br /> | ||
<br /> | ||
<strong>Main class must be named Main and uploaded as Main.class.</strong> | ||
</p> | ||
<input | ||
type="file" | ||
multiple | ||
accept=".class" | ||
onChange={e => { | ||
const ret: { [key: string]: string } = {}; | ||
const promises = []; | ||
for (const file of e.target.files ?? []) { | ||
if (file.name.endsWith('.class')) { | ||
promises.push( | ||
getBase64(file, (b64: string) => { | ||
ret[file.name] = b64; | ||
}) | ||
); | ||
} | ||
} | ||
Promise.all(promises).then(() => { | ||
this.props.onUpload(ret); | ||
}); | ||
}} | ||
/> | ||
</div> | ||
); | ||
} | ||
} |
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.
We are trying to move away from React component classes into functional components instead. Could you refactor this?
<p> | ||
Bypass the compiler and type checker by uploading class files to run in the JVM. | ||
<br /> | ||
<br /> | ||
Only .class files are accepted. Code in the editor will be ignored when running while this | ||
tab is active. | ||
<br /> | ||
<br /> | ||
Compile the files with the following command: | ||
<code>javac *.java -target 8 -source 8</code> | ||
<br /> | ||
<br /> | ||
Avoid running class files downloaded from unknown sources. | ||
<br /> | ||
<br /> | ||
<strong>Main class must be named Main and uploaded as Main.class.</strong> | ||
</p> |
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.
Semantically, does it make sense to have everything in a single paragraph with so many line breaks, instead of multiple p
/pre
tags?
multiple | ||
accept=".class" | ||
onChange={e => { | ||
const ret: { [key: string]: string } = {}; |
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.
This type to represent a file is used a lot. Can we move this to a type alias to ensure single source of truth?
|
||
const makeUploadTabFrom = ( | ||
location: SideContentLocation, | ||
onUpload: (files: { [key: string]: any }) => void |
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.
Or perhaps this type could be aliased instead (or both).
src/commons/utils/JavaHelper.ts
Outdated
@@ -106,7 +91,34 @@ export async function javaRun( | |||
} | |||
}; | |||
|
|||
if (isUsingCse) return await runJavaCseMachine(javaCode, targetStep, context); | |||
// #endregion |
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.
Can we remove this comment?
const { workspaceLocation } = action.payload; | ||
if (workspaceLocation === 'playground' || workspaceLocation === 'sicp') { | ||
state[workspaceLocation].usingUpload = action.payload.usingUpload; | ||
} | ||
}) | ||
.addCase(uploadFiles, (state, action) => { | ||
const { workspaceLocation } = action.payload; |
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.
workspaceLocation
should not be accessed via destructuring of action.payload
. Refer to the other functions for how (i.e. getWorkspaceLocation
).
* Memoize change handler * Fix layout and UI bugs * Use Blueprint file input instead of HTML input component
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.
Hi, I've fixed some of the bugs and typings as per my above comments but I still have some comments below:
Also, could you resolve the merge conflicts?
Thanks!
/** | ||
* This component is responsible for uploading Java class files to bypass the compiler. | ||
*/ | ||
const SideContentUpload: React.FC<Props> = ({ onUpload, workspaceLocation }) => { |
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.
The workspaceLocation
prop is not used. Is this intentional?
<FileInput | ||
inputProps={{ multiple: true, accept: '.class' }} | ||
onInputChange={handleFileUpload} | ||
/> |
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.
Can we keep track of the state by updating the text
prop? See https://blueprintjs.com/docs/#core/components/file-input
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.
Not quite sure what you mean by this. Do we want to show some kind of text to indicate what files are uploaded like "4 files uploaded"?
Added this change, if that is what you meant.
…nto java-jvm-tab
…nto java-jvm-tab
…nto java-jvm-tab
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.
LGTM, thanks and sorry for the delayed review!
* add class file upload tab * unused code param * fix imports * update reducer pattern * update upload text * fix: tsc missing properties * disable upload tab on prod * fix comments * Fix component typing * Fix incorrect workspace location in reducer * Refactor SideContentUpload component * Memoize change handler * Fix layout and UI bugs * Use Blueprint file input instead of HTML input component * Fix typing * remove location prop * Show upload count * Fix format * Improve typing * Update typing --------- Co-authored-by: Richard Dominick <[email protected]>
Description
Allows users to bypass compiler + type checker in local development by uploading class files to run directly.
Supports multiple file uploads.
Type of change
How to test
Compile java file(s) with
javac *.java -target 8 -source 8
and uploadChecklist