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-04-20 - XSS in WebView injectJavaScript
**Vulnerability:** XSS vulnerability through string interpolation in WebView `injectJavaScript`.
**Learning:** Avoid using `injectJavaScript` with string interpolation for data transfer. It can lead to script injection attacks if the data is not properly sanitized.
**Prevention:** Use `webViewRef.current.postMessage(data)` and set up corresponding `message` event listeners in the WebView's JavaScript context for both `window` and `document` (`window.addEventListener('message', ...)` and `document.addEventListener('message', ...)`) to ensure cross-platform compatibility between iOS and Android.
18 changes: 9 additions & 9 deletions src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ const weatherMap: Record<number, { text: string; icon: string }> = {
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>
window.runTesseract = async function(base64JsonStr) {
const handleMessage = async function(event) {
const base64JsonStr = event.data;
if (!window.Tesseract) {
window.ReactNativeWebView.postMessage(JSON.stringify({ success: false, error: 'OCR non pronto' }));
return;
}
try {
const images = JSON.parse(base64JsonStr);
let combinedText = '';
Expand All @@ -55,6 +60,8 @@ window.runTesseract = async function(base64JsonStr) {
window.ReactNativeWebView.postMessage(JSON.stringify({ success: false, error: e.message || e.toString() }));
}
};
window.addEventListener('message', handleMessage);
document.addEventListener('message', handleMessage);
</script></body></html>`;

function PinnedFlightCardComponent({ item, colors }: { item: any; colors: any }) {
Expand Down Expand Up @@ -288,14 +295,7 @@ export default function HomeScreen() {
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;
`);
webViewRef.current?.postMessage(base64Json);
}
} catch (e) { if (__DEV__) console.error('[imagePicker]', e); setProcessing(false); }
};
Expand Down
21 changes: 10 additions & 11 deletions src/screens/ShiftScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ export default function ShiftScreen() {
setOcrText('');

const base64List = result.assets.map(a => `data:image/jpeg;base64,${a.base64}`);
const base64Json = JSON.stringify(base64List).replace(/'/g, "\\'");
const base64Json = JSON.stringify(base64List);

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);
webViewRef.current?.postMessage(base64Json);
}
} catch (e) {
Alert.alert("Errore OCR", "Impossibile elaborare l'immagine.");
Expand Down Expand Up @@ -216,7 +208,12 @@ export default function ShiftScreen() {
</head>
<body style="background-color: transparent;">
<script>
window.runTesseract = async function(base64JsonStr) {
const handleMessage = async function(event) {
const base64JsonStr = event.data;
if (!window.Tesseract) {
window.ReactNativeWebView.postMessage(JSON.stringify({ success: false, error: 'OCR non pronto' }));
return;
}
try {
const images = JSON.parse(base64JsonStr);
let combinedText = '';
Expand All @@ -229,6 +226,8 @@ export default function ShiftScreen() {
window.ReactNativeWebView.postMessage(JSON.stringify({ success: false, error: e.message || e.toString() }));
}
};
window.addEventListener('message', handleMessage);
document.addEventListener('message', handleMessage);
</script>
</body>
</html>
Expand Down
Loading