
成果範例
Code review: Python order-import script for a small online shop
What the code does
- Reads
orders.csvfrom the shop export. - For each order line, finds the product by SKU in
stock.xlsxand subtracts the quantity. - Writes the updated stock back and logs totals to
import_log.txt.
The structure is clear and functions are well named. Most issues are about input that does not look the way the script expects.
Bugs and weak spots
| # | Line | Severity | Issue |
|---|---|---|---|
| 1 | 48 | High | The same CSV can be imported twice; there is no check for already processed order IDs. This is the most likely cause of negative stock. |
| 2 | 72 | High | int(row["qty"]) crashes on empty or "2.0" values from refunded orders. |
| 3 | 95 | Medium | SKU lookup is case-sensitive: "gt-101" and "GT-101" are treated as different products. |
| 4 | 130 | Medium | The file is opened without encoding="utf-8", so product names with accents break on Windows. |
| 5 | 201 | Low | Bare except: hides real errors and makes the log useless. |
Corrected fragments
Issue 1: skip orders already imported
processed = load_processed_ids("processed_orders.txt")
for row in reader:
if row["order_id"] in processed:
continue
# ... existing logic ...
processed.add(row["order_id"])
save_processed_ids("processed_orders.txt", processed)
Why: each order is applied once, even if you run the script on the same file again.
Issue 2: read quantity safely
raw = (row.get("qty") or "").strip()
try:
qty = int(float(raw))
except ValueError:
log.warning("Order %s: bad quantity %r, skipped", row["order_id"], raw)
continue
Why: refunded or partial lines are skipped with a clear note instead of stopping the whole import.
Issue 3: normalise SKUs
sku = row["sku"].strip().upper()
Apply the same .strip().upper() when building the stock dictionary.
Style and readability
process()is 90 lines long; split it intoread_orders,apply_to_stock,write_report.- Replace magic column numbers (
row[7]) with header names.
Please test each fix on a copy of your files: we read the code but did not run it.
Client: owner of a tea shop who wrote a 310-line Python script that imports orders from a CSV export into the stock spreadsheet Problem reported: "Sometimes stock goes negative, and the script crashes on some files."
客戶與所有細節皆為虛構。
關於此服務
許多資安漏洞就直接顯現在程式碼裡:以使用者輸入組成的 SQL 或 shell 指令、未經跳脫就輸出的內容、用快速雜湊儲存的密碼、只確認使用者是誰卻不檢查可存取什麼的檢查,以及寫在原始碼中的金鑰。我們以 OWASP Top 10 作為檢查清單閱讀您的程式碼,說明每個弱點以及可能被如何利用,並依風險排序。每項發現都附有採用您語言安全寫法的修正片段。
包含內容
- 您會收到什麼在訂單頁面提供的書面審查:依風險排序的弱點並逐項說明,以及採用安全寫法的修正片段。進階版另附針對專案其餘部分的簡短強化檢查清單。
- 注入攻擊SQL、shell 與樣板注入
- 輸出跳脫跨站腳本風險
- 存取檢查誰能看到與修改什麼
- 密碼與機密雜湊處理,金鑰不放在程式碼中
- 依序排列的發現先修最嚴重的
服務流程
- 1需求說明您貼上程式碼中的敏感部分
- 2閱讀我們對照 OWASP Top 10 檢查
- 3審查您會收到依序排列的發現與修正
需要您提供的資料
以文字或截圖貼上、已將機密換成預留文字的處理輸入、登入、檔案或付款的程式碼,以及語言與框架和應用程式的使用者。
我們會詢問您:
- 使用哪種語言與框架?
- 哪些部分處理使用者輸入、登入或付款?
- 誰在使用這個應用程式?
- 是有特定事件或警示促成這次審查嗎?
比較方案
付款保障
下單時付款,但只有在驗收成果後,款項才會撥付。
- 固定價格付款後,方案價格不會變動。
- 款項先行保留付款時,金額只會先保留,在您驗收後才會撥付給服務。
- 交付前檢查每份成果在交到您手上之前都會經過品質檢查,您也可以在驗收前於訂單頁面查看成果。
- 免費修改在需求說明範圍內,可依方案允許的次數提出修改。
- 3 天後自動驗收若您在交付後 3 天內未驗收也未提出修改,成果將自動驗收。若您在交付後於訂單聊天室留言,訂單會暫停自動驗收;我們回覆後,可能會重新起算 3 天,並會在聊天室中告知。
- 重新製作或全額退款若成果不符合您的需求說明且修改後仍未改善,或製作時間超過交付期限的兩倍,請在訂單聊天室留言:我們將重新製作,或全額退還款項。
常見問題
這是滲透測試或資安認證嗎?
不是。我們閱讀您提供的程式碼,不會攻擊或掃描您的線上系統,因此在需要正式稽核的情況下,這無法取代正式稽核。
我應該提供密碼或金鑰嗎?
絕對不要。請換成預留文字;如果真實的金鑰已經出現在程式碼中,請更換該金鑰。
你們會執行我的程式碼嗎?
不會。我們會仔細閱讀並推論其行為,但不會執行,因此請在您自己的專案中測試每個建議的修正。
沒有找到您要的答案?