Skip to content
Open
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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-05-01 - XSS in WebView
**Vulnerability:** Use of `injectJavaScript` to pass user-provided base64 images to WebView in `HomeScreen.tsx` and `ShiftScreen.tsx` introduces XSS risk.
**Learning:** React Native developers often use `injectJavaScript` with string interpolation for passing data, which can execute arbitrary JS if the string isn't sanitized perfectly. The memory correctly points out we should use `postMessage` instead.
**Prevention:** Avoid `injectJavaScript` for data transfer. Use `postMessage` from React Native to WebView, and set up a `message` listener inside the WebView's HTML string.
22 changes: 13 additions & 9 deletions src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const HOME_REST_TIMING = { startHour: 12, startMinute: 0, endHour: 14, endMinute
const engineHtml = `<!DOCTYPE html><html lang="it"><head>
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@5/dist/tesseract.min.js"></script></head>
<body style="background-color:transparent;"><script>
document.addEventListener("message", function(event) {
if (window.runTesseract && event.data) {
window.runTesseract(event.data);
}
});
window.addEventListener("message", function(event) {
if (window.runTesseract && event.data) {
window.runTesseract(event.data);
}
});

window.runTesseract = async function(base64JsonStr) {
try {
const images = JSON.parse(base64JsonStr);
Expand Down Expand Up @@ -283,15 +294,8 @@ export default function HomeScreen({ isFocused }: { isFocused?: boolean }) {
setProcessing(true); setOcrText('');
const base64List = result.assets.map(a => `data:image/jpeg;base64,${a.base64}`);
const base64Json = JSON.stringify(base64List);
// Use postMessage pattern to avoid script-injection risks with injectJavaScript
webViewRef.current?.injectJavaScript(`
if(window.runTesseract){
window.runTesseract(${JSON.stringify(base64Json)});
} else {
window.ReactNativeWebView.postMessage(JSON.stringify({success:false,error:'OCR non pronto'}));
}
true;
`);
// Use postMessage pattern to avoid script-injection risks with injectJavaScript
webViewRef.current?.postMessage(base64Json);
}
} catch (e) { if (__DEV__) console.error('[imagePicker]', e); setProcessing(false); }
};
Expand Down
22 changes: 13 additions & 9 deletions src/screens/ShiftScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,8 @@ export default function ShiftScreen() {
const base64List = result.assets.map(a => `data:image/jpeg;base64,${a.base64}`);
const base64Json = JSON.stringify(base64List).replace(/'/g, "\\'");

const jsCode = `
if (window.runTesseract) {
window.runTesseract('${base64Json}');
} else {
window.ReactNativeWebView.postMessage(JSON.stringify({ success: false, error: "Motore OCR non pronto." }));
}
true;
`;
webViewRef.current?.injectJavaScript(jsCode);
// Use postMessage pattern to avoid script-injection risks with injectJavaScript
webViewRef.current?.postMessage(base64Json);
}
} catch (e) {
Alert.alert("Errore OCR", "Impossibile elaborare l'immagine.");
Expand Down Expand Up @@ -217,6 +210,17 @@ export default function ShiftScreen() {
</head>
<body style="background-color: transparent;">
<script>
document.addEventListener("message", function(event) {
if (window.runTesseract && event.data) {
window.runTesseract(event.data);
}
});
window.addEventListener("message", function(event) {
if (window.runTesseract && event.data) {
window.runTesseract(event.data);
}
});

window.runTesseract = async function(base64JsonStr) {
try {
const images = JSON.parse(base64JsonStr);
Expand Down
Loading